DdaApiUtils.java Source code

Java tutorial

Introduction

Here is the source code for DdaApiUtils.java

Source

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.entity.StringEntity;

public class DdaApiUtils {
    public static void queryLibrariesEndpoint(String providerCode, String baseUrl) throws Exception {
        String url = (baseUrl + providerCode + "/libraries");
        CloseableHttpResponse response = HttpUtils.processHttpGetRequest(url);
        String output = JsonUtils.parseJsonArray(response);

        System.out.println("Request URL is: " + url);
        System.out.println("Channel IDs currently configured for provider " + providerCode + ": ");
        System.out.println(output);
        System.out.println();
    }

    public static void queryDataRequestsEndpoint(String providerCode, String baseUrl) throws Exception {
        String url = (baseUrl + providerCode + "/data/requests");
        CloseableHttpResponse response = HttpUtils.processHttpGetRequest(url);
        String output = JsonUtils.parseJsonArray(response);

        System.out.println("Request URL is: " + url);
        System.out.println("Outstanding requests for data from provider " + providerCode + ": ");
        System.out.println(output);
        System.out.println();
    }

    public static void queryHoldingsAddReplaceEndpoint(String channelId, String databaseCode, String providerCode,
            String baseUrl) throws Exception {
        String url = (baseUrl + providerCode + "/libraries/" + channelId + "/" + databaseCode
                + "/holdings/addReplace?seqnum=1");
        System.out.println();
        System.out.println("Request URL is: " + url);

        StringEntity jsonPacket = new StringEntity("[{ \"id\": \"10778603\", \"owned\": true }]");

        CloseableHttpResponse response = HttpUtils.processHttpPostRequest(url, jsonPacket);
        int serverResponse = HttpUtils.getResponseStatusCode(response);
        String reasonPhrase = HttpUtils.getReasonPhrase(response);

        System.out.println();
        if (serverResponse == 200) {
            System.out.println("Update holdings request for provider " + providerCode + " was successful.");
        } else {
            System.out.println("Update holdings for provider " + providerCode + " was NOT successful.");
            System.out.println("Server response was: " + serverResponse);
            System.out.println("Server reason phrase was: " + reasonPhrase);
        }
    }

    public static void queryNotifyDataAvailableEndpoint(String providerCode, String baseUrl) throws Exception {
        String url = (baseUrl + providerCode + "/data/notifyAvailable");
        System.out.println("Request URL is: " + url);

        StringEntity jsonPacket = new StringEntity(
                "[{\"seqnum\": 123, \"url\": \"ftp://provider/export.zip\", \"type\": \"TITLE_DATA_FULL\" }]");

        CloseableHttpResponse response = HttpUtils.processHttpPostRequest(url, jsonPacket);
        int serverResponse = HttpUtils.getResponseStatusCode(response);
        String reasonPhrase = HttpUtils.getReasonPhrase(response);

        System.out.println();
        if (serverResponse == 200) {
            System.out.println("Notify Data Available for provider " + providerCode + " was successful.");
        } else {
            System.out.println("Notify Data Available for provider " + providerCode + " was NOT successful.");
            System.out.println("Server response was: " + serverResponse);
            System.out.println("Server reason phrase was: " + reasonPhrase);
        }
    }

    public static void querySetsAllHoldingsEndpoint(String channelId, String databaseCode, String providerCode,
            String baseUrl) throws Exception {
        String url = (baseUrl + providerCode + "/libraries/" + channelId + "/" + databaseCode
                + "/holdings?seqnum=1");
        System.out.println("Request URL is: " + url);

        StringEntity jsonPacket = new StringEntity(
                "[ { \"id\": \"1234\", \"owned\": true, \"descriptors\": [ { \"qty\": 3, \"desc\": \"Some license with quantity info\" }, { \"qty\": null, \"desc\": \"Other license with no quantity info\" }, { \"desc\": \"Yet another license with no quantity info\" }] }]");

        CloseableHttpResponse response = HttpUtils.processHttpPutRequest(url, jsonPacket);
        int serverResponse = HttpUtils.getResponseStatusCode(response);
        String reasonPhrase = HttpUtils.getReasonPhrase(response);

        System.out.println();
        if (serverResponse == 200) {
            System.out.println("Holdings were successfully replaced for provider " + providerCode + " and library "
                    + channelId + ".");
        } else {
            System.out.println("Holdings were NOT successfully replaced for provider " + providerCode
                    + " and library " + channelId + ".");
            System.out.println("Server response was: " + serverResponse);
            System.out.println("Server reason phrase was: " + reasonPhrase);
        }
    }

    public static void queryUploadEndpoint(String providerCode, String baseUrl) throws Exception {
        String url = (baseUrl + providerCode + "/data/upload");
        System.out.println("Request URL is: " + url);

        StringEntity jsonPacket = new StringEntity(
                "[{\"seqnum\": 123, \"url\": \"ftp://provider/export.zip\", \"type\": \"TITLE_DATA_FULL\" }]");

        String fileContentType = "multipart/form-data";

        CloseableHttpResponse response = HttpUtils.processHttpPostRequest(url, jsonPacket, fileContentType);
        int serverResponse = HttpUtils.getResponseStatusCode(response);
        String reasonPhrase = HttpUtils.getReasonPhrase(response);

        System.out.println();
        if (serverResponse == 200) {
            System.out.println("File Upload for provider " + providerCode + " was successful.");
        } else {
            System.out.println("File Upload for provider " + providerCode + " was NOT successful.");
            System.out.println("Server response was: " + serverResponse);
            System.out.println("Server reason phrase was: " + reasonPhrase);
        }
    }

    public static void queryRemoveHoldingsEndpoint(String channelId, String databaseCode, String providerCode,
            String baseUrl) throws Exception {
        String url = (baseUrl + providerCode + "/libraries/" + channelId + "/" + databaseCode
                + "/holdings/remove?seqnum=1");
        System.out.println("Request URL is: " + url);

        StringEntity jsonPacket = new StringEntity("[ { \"id\": \"1234\" }, { \"id\": \"5678\" } ]");

        CloseableHttpResponse response = HttpUtils.processHttpPostRequest(url, jsonPacket);
        int serverResponse = HttpUtils.getResponseStatusCode(response);
        String reasonPhrase = HttpUtils.getReasonPhrase(response);

        System.out.println();
        if (serverResponse == 200) {
            System.out.println("Holdings were successfully removed for provider " + providerCode + " and library "
                    + channelId + ".");
        } else {
            System.out.println("Holdings were NOT successfully removed for provider " + providerCode
                    + " and library " + channelId + ".");
            System.out.println("Server response was: " + serverResponse);
            System.out.println("Server reason phrase was: " + reasonPhrase);
        }
    }
}