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;
}
}
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");
}
}
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);
}
}
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);
}
}
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;
}
Aggregations