BasicCdiscLibraryClient.java

BasicCdiscLibraryClient.java
/*
* Copyright 2020 Jozef Aerts.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.xml4pharma.cdisclibrary.test;

import javax.ws.rs.client.*; // Client and ClientBuilder - javax.ws.rs-api-2.1.jar
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

// Jersey-2 imports
import org.glassfish.jersey.client.*;

public class BasicCDISCLibraryClient {

    private String base = "https://library.cdisc.org/api"; // the base of the service
    private String apiKey = "xxxxxxxxxxxxxxxxxxxxxx"; // your API key

    private Client client;

    public BasicCDISCLibraryClient() {
        ClientConfig clientConfig = new ClientConfig();
        // see e.g. https://jersey.github.io/documentation/latest/logging_chapter.html
        // for how to add logging
        // clientConfig.property(LoggingFeature.LOGGING_FEATURE_VERBOSITY_CLIENT,
        // LoggingFeature.Verbosity.PAYLOAD_ANY);
        client = ClientBuilder.newClient(clientConfig);
    }

    // Get the properties of the provided SDTM-IG variable in the provided domain
    // and for the provided IG version
    public String getSDTMVariableProperties(String igVersion, String domain, String variable) {
        WebTarget webTarget = client.target(base).path("mdr").path("sdtmig").path(igVersion).path("datasets")
                .path(domain).path("variables").path(variable);
        Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_XML);
        // Response requires javax.annotation-api-1.2.jar, hk2-api-2.5.0-b42.jar,
        // hk2-locator-2.5.0-b42.jar, hk2-utils-2.5.0-b42.jar
        // and javax.inject-1.jar, javax.inject-2.5.0-b42.jar
        // and jersey-media-json-binding.jar (when requesting JSON response)
        // from the Jersey-2 library - see https://jersey.github.io/
        Response response = invocationBuilder.header("api-key", apiKey).get();
        System.out.println("HTTP Response Status = " + response.getStatus());
        String xml = response.readEntity(String.class);
        // System.out.println(xml);
        return xml;
    }

    public static void main(String[] args) {
        BasicCDISCLibraryClient cl = new BasicCDISCLibraryClient();
        String response = cl.getSDTMVariableProperties("3-3", "PE", "PESTRESC");
        System.out.println("Response = " + response);
    }

}