Search in sources :

Example 6 with DHIS2Response

use of net.geoprism.dhis2.dhis2adapter.response.DHIS2Response in project geoprism-registry by terraframe.

the class Dhis2IdCache method fetchIds.

/**
 * Fetches more ids from DHIS2 and adds them to our internal cache.
 *
 * @throws HTTPException
 * @throws InvalidLoginException
 * @throws UnexpectedResponseException
 * @throws BadServerUriException
 */
public DHIS2Response fetchIds() throws HTTPException, InvalidLoginException, UnexpectedResponseException, BadServerUriException {
    List<NameValuePair> nvp = new ArrayList<NameValuePair>();
    nvp.add(new BasicNameValuePair("limit", String.valueOf(FETCH_NUM)));
    DHIS2Response response = dhis2.apiGet("system/id.json", nvp);
    if (response.getStatusCode() != 200) {
        throw new HTTPException("Unable to get new ids from DHIS2. " + response.getResponse());
    }
    JsonObject json = response.getJsonObject();
    if (json.has("codes")) {
        JsonArray codes = json.get("codes").getAsJsonArray();
        for (int i = 0; i < codes.size(); ++i) {
            String id = codes.get(i).getAsString();
            cache.push(id);
        }
    } else {
        throw new UnexpectedResponseException(response);
    }
    return response;
}
Also used : JsonArray(com.google.gson.JsonArray) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HTTPException(net.geoprism.dhis2.dhis2adapter.exception.HTTPException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) UnexpectedResponseException(net.geoprism.dhis2.dhis2adapter.exception.UnexpectedResponseException) DHIS2Response(net.geoprism.dhis2.dhis2adapter.response.DHIS2Response)

Example 7 with DHIS2Response

use of net.geoprism.dhis2.dhis2adapter.response.DHIS2Response in project geoprism-registry by terraframe.

the class HTTPConnector method httpPatch.

public DHIS2Response httpPatch(String url, List<NameValuePair> params, HttpEntity body) throws InvalidLoginException, HTTPException, BadServerUriException {
    try {
        if (!isInitialized()) {
            initialize();
        }
        HttpPatch patch = new HttpPatch(this.buildUri(url, params));
        patch.addHeader("Content-Type", "application/json");
        patch.setEntity(body);
        try (CloseableHttpResponse response = client.execute(patch, this.getContext())) {
            return this.convertResponse(response);
        }
    } catch (IOException | URISyntaxException e) {
        throw new HTTPException(e);
    }
}
Also used : HTTPException(net.geoprism.dhis2.dhis2adapter.exception.HTTPException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) HttpPatch(org.apache.http.client.methods.HttpPatch)

Example 8 with DHIS2Response

use of net.geoprism.dhis2.dhis2adapter.response.DHIS2Response in project geoprism-registry by terraframe.

the class HTTPConnector method httpGet.

public DHIS2Response httpGet(String url, List<NameValuePair> params) throws InvalidLoginException, HTTPException, BadServerUriException {
    try {
        if (!isInitialized()) {
            initialize();
        }
        HttpGet get = new HttpGet(this.buildUri(url, params));
        get.addHeader("Accept", "application/json");
        try (CloseableHttpResponse response = client.execute(get, getContext())) {
            return this.convertResponse(response);
        }
    } catch (URISyntaxException | IOException e) {
        throw new HTTPException(e);
    }
}
Also used : HTTPException(net.geoprism.dhis2.dhis2adapter.exception.HTTPException) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 9 with DHIS2Response

use of net.geoprism.dhis2.dhis2adapter.response.DHIS2Response in project geoprism-registry by terraframe.

the class DHIS2BridgeTest method testSystemInfo.

@Test
public void testSystemInfo() throws Exception {
    String file = IOUtils.toString(DHIS2BridgeTest.class.getResourceAsStream("/default/system-info.json"), "UTF-8");
    DHIS2Bridge facade = TestBridgeBuilder.buildDefault(file, 200);
    facade.initialize();
    DHIS2Response resp = facade.systemInfo();
    Assert.assertEquals(200, resp.getStatusCode());
    JsonObject jo = resp.getJsonObject();
    Assert.assertEquals("2.31.9", jo.get("version").getAsString());
    Assert.assertEquals(Constants.DHIS2_URL, jo.get("contextPath").getAsString());
}
Also used : JsonObject(com.google.gson.JsonObject) DHIS2Response(net.geoprism.dhis2.dhis2adapter.response.DHIS2Response) Test(org.junit.Test)

Example 10 with DHIS2Response

use of net.geoprism.dhis2.dhis2adapter.response.DHIS2Response in project geoprism-registry by terraframe.

the class DHIS2BridgeTest method testEntityIdGet.

// @Test
// public void testMetadataGet() throws Exception
// {
// List<NameValuePair> params = new ArrayList<NameValuePair>();
// params.add(new BasicNameValuePair("organisationUnits", "true"));
// params.add(new BasicNameValuePair("code", "OU_525"));
// 
// DHIS2Response resp = facade.metadataGet(params);
// 
// System.out.println(resp.getResponse());
// }
@Test
public void testEntityIdGet() throws Exception {
    String file = IOUtils.toString(DHIS2BridgeTest.class.getResourceAsStream("/default/entityId-organisationUnit.json"), "UTF-8");
    DHIS2Bridge facade = TestBridgeBuilder.buildDefault(file, 200);
    facade.initialize();
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    // params.add(new BasicNameValuePair("organisationUnits", "true"));
    // params.add(new BasicNameValuePair("code", "OU_525"));
    DHIS2Response resp = facade.entityIdGet("organisationUnits", "ImspTQPwCqd", params);
// System.out.println(resp.getResponse());
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) ArrayList(java.util.ArrayList) DHIS2Response(net.geoprism.dhis2.dhis2adapter.response.DHIS2Response) Test(org.junit.Test)

Aggregations

HTTPException (net.geoprism.dhis2.dhis2adapter.exception.HTTPException)7 DHIS2Response (net.geoprism.dhis2.dhis2adapter.response.DHIS2Response)6 JsonObject (com.google.gson.JsonObject)5 IOException (java.io.IOException)5 URISyntaxException (java.net.URISyntaxException)5 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)5 NameValuePair (org.apache.http.NameValuePair)4 JsonArray (com.google.gson.JsonArray)3 ArrayList (java.util.ArrayList)3 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)3 UnexpectedResponseException (net.geoprism.dhis2.dhis2adapter.exception.UnexpectedResponseException)2 Test (org.junit.Test)2 GsonBuilder (com.google.gson.GsonBuilder)1 Transaction (com.runwaysdk.dataaccess.transaction.Transaction)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 InvalidLoginException (net.geoprism.dhis2.dhis2adapter.exception.InvalidLoginException)1 DHIS2ImportResponse (net.geoprism.dhis2.dhis2adapter.response.DHIS2ImportResponse)1 ImportStrategy (net.geoprism.dhis2.dhis2adapter.response.model.ImportStrategy)1 OrganisationUnit (net.geoprism.dhis2.dhis2adapter.response.model.OrganisationUnit)1