Search in sources :

Example 11 with ApiCallException

use of org.eyeseetea.malariacare.domain.exception.ApiCallException in project pictureapp by EyeSeeTea.

the class ServerAPIController method banOrg.

/**
 * Bans the orgUnit for future pushes (too many too quick)
 */
public static boolean banOrg(String url, String orgUnitNameOrCode) throws ApiCallException, ConfigJsonIOException {
    Log.i(TAG, String.format("banOrg(%s,%s)", url, orgUnitNameOrCode));
    try {
        Log.i(TAG, String.format("banOrg(%s,%s)", url, orgUnitNameOrCode));
        JSONObject orgUnitJSON = getOrgUnitData(url, orgUnitNameOrCode);
        String orgUnitUID = "";
        String orgUnitDescription = "";
        if (orgUnitJSON.has(TAG_ID)) {
            orgUnitUID = orgUnitJSON.getString(TAG_ID);
        }
        if (orgUnitJSON.has(TAG_DESCRIPTIONCLOSEDATE)) {
            orgUnitDescription = orgUnitJSON.getString(TAG_DESCRIPTIONCLOSEDATE);
        }
        // NO OrgUnitUID -> Non blocking error, go on
        if (orgUnitUID == null) {
            new ApiCallException(String.format("banOrg(%s,%s) -> No UID", url, orgUnitNameOrCode));
            return false;
        }
        // Update date and description in the orgunit
        patchClosedDate(url, orgUnitUID);
        patchDescriptionClosedDate(url, orgUnitUID, orgUnitDescription);
        return true;
    } catch (JSONException e) {
        throw new ApiCallException(String.format("banOrg(%s,%s): %s", url, orgUnitNameOrCode, e.getMessage()));
    } catch (ApiCallException ex) {
        return false;
    }
}
Also used : JSONObject(org.json.JSONObject) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) JSONException(org.json.JSONException)

Example 12 with ApiCallException

use of org.eyeseetea.malariacare.domain.exception.ApiCallException in project pictureapp by EyeSeeTea.

the class APushUseCaseStrategy method banOrgUnit.

private void banOrgUnit() {
    OrganisationUnit organisationUnit = null;
    try {
        organisationUnit = mOrganisationUnitRepository.getCurrentOrganisationUnit(ReadPolicy.CACHE);
    } catch (NetworkException e) {
        e.printStackTrace();
    } catch (ApiCallException e) {
        e.printStackTrace();
    }
    if (organisationUnit != null) {
        organisationUnit.ban();
        mOrganisationUnitRepository.saveOrganisationUnit(organisationUnit);
        System.out.println("OrgUnit banned successfully");
    }
}
Also used : OrganisationUnit(org.eyeseetea.malariacare.domain.entity.OrganisationUnit) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) NetworkException(org.eyeseetea.malariacare.domain.exception.NetworkException)

Example 13 with ApiCallException

use of org.eyeseetea.malariacare.domain.exception.ApiCallException in project pictureapp by EyeSeeTea.

the class ServerAPIController method getOrgUnitData.

/**
 * Returns the orgunit data from the given server according to its current version
 */
static JSONObject getOrgUnitData(String url, String orgUnitNameOrCode) throws ApiCallException {
    // Version is required to choose which field to match
    String serverVersion = getServerVersion(url);
    // No version -> No data
    if (serverVersion == null) {
        return null;
    }
    String urlOrgUnitData = getOrgUnitDataUrl(url, serverVersion, orgUnitNameOrCode);
    Response response = ServerApiCallExecution.executeCall(null, urlOrgUnitData, "GET");
    // {"organisationUnits":[{}]}
    JSONObject jsonResponse = ServerApiUtils.getApiResponseAsJSONObject(response);
    JSONArray orgUnitsArray = null;
    try {
        orgUnitsArray = (JSONArray) jsonResponse.get(TAG_ORGANISATIONUNITS);
    } catch (JSONException e) {
        throw new ApiCallException(e);
    }
    // 0| >1 matches -> Error
    if (orgUnitsArray.length() == 0 || orgUnitsArray.length() > 1) {
        Log.e(TAG, String.format("getOrgUnitData(%s,%s) -> Found %d matches", url, orgUnitNameOrCode, orgUnitsArray.length()));
        return null;
    }
    try {
        return (JSONObject) orgUnitsArray.get(0);
    } catch (JSONException e) {
        throw new ApiCallException(e);
    }
}
Also used : Response(com.squareup.okhttp.Response) JSONObject(org.json.JSONObject) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 14 with ApiCallException

use of org.eyeseetea.malariacare.domain.exception.ApiCallException in project pictureapp by EyeSeeTea.

the class ServerAPIController method getOrganisationUnitsByCode.

public static OrganisationUnit getOrganisationUnitsByCode(String code) throws ApiCallException {
    // Version is required to choose which field to match
    String serverVersion = getServerVersion(PreferencesState.getInstance().getDhisURL());
    // No version -> No data
    if (serverVersion == null) {
        return null;
    }
    try {
        String urlOrgUnitData = getOrganisationUnitsCredentialsUrl(code);
        if (!isNetworkAvailable()) {
            throw new NetworkException();
        }
        Response response = ServerApiCallExecution.executeCall(null, urlOrgUnitData, "GET");
        JSONObject body = ServerApiUtils.getApiResponseAsJSONObject(response);
        // {"organisationUnits":[{}]}
        JSONArray orgUnitsArray = (JSONArray) body.get(TAG_ORGANISATIONUNITS);
        // 0| >1 matches -> Error
        if (orgUnitsArray.length() == 0 || orgUnitsArray.length() > 1) {
            Log.e(TAG, String.format("getOrgUnitData(%s) -> Found %d matches", code, orgUnitsArray.length()));
            return null;
        }
        JSONObject orgUnitJO = (JSONObject) orgUnitsArray.get(0);
        return parseOrgUnit(orgUnitJO);
    } catch (NetworkException e) {
        throw new ApiCallException(e);
    } catch (Exception ex) {
        throw new ApiCallException(ex);
    }
}
Also used : Response(com.squareup.okhttp.Response) JSONObject(org.json.JSONObject) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) JSONArray(org.json.JSONArray) NetworkException(org.eyeseetea.malariacare.domain.exception.NetworkException) NetworkException(org.eyeseetea.malariacare.domain.exception.NetworkException) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) ConfigJsonIOException(org.eyeseetea.malariacare.domain.exception.ConfigJsonIOException) PullConversionException(org.eyeseetea.malariacare.domain.exception.PullConversionException) JSONException(org.json.JSONException)

Example 15 with ApiCallException

use of org.eyeseetea.malariacare.domain.exception.ApiCallException in project pictureapp by EyeSeeTea.

the class BasicAuthenticator method executeCall.

/**
 * Call to DHIS Server
 */
static Response executeCall(JSONObject data, String url, String method) throws ApiCallException {
    final String DHIS_URL = url;
    Log.d(method, DHIS_URL);
    OkHttpClient client = UnsafeOkHttpsClientFactory.getUnsafeOkHttpClient();
    BasicAuthenticator basicAuthenticator = new BasicAuthenticator();
    client.setAuthenticator(basicAuthenticator);
    Request.Builder builder = new Request.Builder().header(basicAuthenticator.AUTHORIZATION_HEADER, basicAuthenticator.getCredentials()).url(DHIS_URL);
    switch(method) {
        case "POST":
            RequestBody postBody = RequestBody.create(JSON, data.toString());
            builder.post(postBody);
            break;
        case "PUT":
            RequestBody putBody = RequestBody.create(JSON, data.toString());
            builder.put(putBody);
            break;
        case "PATCH":
            RequestBody patchBody = RequestBody.create(JSON, data.toString());
            builder.patch(patchBody);
            break;
        case "GET":
            builder.get();
            break;
    }
    Request request = builder.build();
    Response response = null;
    try {
        response = client.newCall(request).execute();
    } catch (IOException ex) {
        throw new ApiCallException(ex);
    }
    return response;
}
Also used : Response(com.squareup.okhttp.Response) OkHttpClient(com.squareup.okhttp.OkHttpClient) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) ConfigJsonIOException(org.eyeseetea.malariacare.domain.exception.ConfigJsonIOException) RequestBody(com.squareup.okhttp.RequestBody)

Aggregations

ApiCallException (org.eyeseetea.malariacare.domain.exception.ApiCallException)18 JSONException (org.json.JSONException)10 JSONObject (org.json.JSONObject)10 Response (com.squareup.okhttp.Response)8 NetworkException (org.eyeseetea.malariacare.domain.exception.NetworkException)4 IOException (java.io.IOException)3 Date (java.util.Date)3 OrganisationUnit (org.eyeseetea.malariacare.domain.entity.OrganisationUnit)3 ConfigJsonIOException (org.eyeseetea.malariacare.domain.exception.ConfigJsonIOException)3 JSONArray (org.json.JSONArray)3 OkHttpClient (com.squareup.okhttp.OkHttpClient)2 Request (com.squareup.okhttp.Request)2 RequestBody (com.squareup.okhttp.RequestBody)2 SurveyLocalDataSource (org.eyeseetea.malariacare.data.database.datasources.SurveyLocalDataSource)2 Program (org.eyeseetea.malariacare.data.database.model.Program)2 Survey (org.eyeseetea.malariacare.data.database.model.Survey)2 OrganisationUnitRepository (org.eyeseetea.malariacare.data.repositories.OrganisationUnitRepository)2 IAsyncExecutor (org.eyeseetea.malariacare.domain.boundary.executors.IAsyncExecutor)2 IMainExecutor (org.eyeseetea.malariacare.domain.boundary.executors.IMainExecutor)2 IOrganisationUnitRepository (org.eyeseetea.malariacare.domain.boundary.repositories.IOrganisationUnitRepository)2