Search in sources :

Example 26 with Response

use of com.squareup.okhttp.Response in project pictureapp by EyeSeeTea.

the class ServerAPIController method pullUserAttributes.

public static User pullUserAttributes(User loggedUser) throws ApiCallException {
    String lastMessage = loggedUser.getAnnouncement();
    String uid = loggedUser.getUid();
    String url = PreferencesState.getInstance().getDhisURL() + "/api/" + TAG_USER + String.format(QUERY_USER_ATTRIBUTES, uid);
    url = ServerApiUtils.encodeBlanks(url);
    Response response = ServerApiCallExecution.executeCall(null, url, "GET");
    JSONObject body = ServerApiUtils.getApiResponseAsJSONObject(response);
    JsonNode jsonNode = ServerApiUtils.getJsonNodeMappedResponse(body);
    JsonNode jsonNodeArray = jsonNode.get(ATTRIBUTE_VALUES);
    String newMessage = "";
    String closeDate = "";
    for (int i = 0; i < jsonNodeArray.size(); i++) {
        if (jsonNodeArray.get(i).get(ATTRIBUTE).get(CODE).textValue().equals(User.ATTRIBUTE_USER_ANNOUNCEMENT)) {
            newMessage = jsonNodeArray.get(i).get(VALUE).textValue();
        }
        if (jsonNodeArray.get(i).get(ATTRIBUTE).get(CODE).textValue().equals(User.ATTRIBUTE_USER_CLOSE_DATE)) {
            closeDate = jsonNodeArray.get(i).get(VALUE).textValue();
        }
    }
    if ((lastMessage == null && newMessage != null) || (newMessage != null && !newMessage.equals("") && !lastMessage.equals(newMessage))) {
        loggedUser.setAnnouncement(newMessage);
        PreferencesState.getInstance().setUserAccept(false);
    }
    if (closeDate == null || closeDate.equals("")) {
        loggedUser.setCloseDate(null);
    } else {
        loggedUser.setCloseDate(Utils.parseStringToCalendar(closeDate, DHIS2_GMT_NEW_DATE_FORMAT).getTime());
    }
    loggedUser.save();
    return loggedUser;
}
Also used : Response(com.squareup.okhttp.Response) JSONObject(org.json.JSONObject) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 27 with Response

use of com.squareup.okhttp.Response in project pictureapp by EyeSeeTea.

the class ServerAPIController method patchDescriptionClosedDate.

static void patchDescriptionClosedDate(String url, String orgUnitUID, String orgUnitDescription) throws ApiCallException {
    // https://malariacare.psi.org/api/organisationUnits/u5jlxuod8xQ/closedDate
    try {
        String urlPathClosedDescription = getPatchClosedDescriptionUrl(url, orgUnitUID);
        JSONObject data = prepareClosingDescriptionValue(orgUnitDescription);
        Response response = ServerApiCallExecution.executeCall(data, urlPathClosedDescription, "PATCH");
        ServerApiUtils.checkResponse(response, null);
    } 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) JSONException(org.json.JSONException)

Example 28 with Response

use of com.squareup.okhttp.Response in project pictureapp by EyeSeeTea.

the class ServerAPIController method isUserClosed.

public static boolean isUserClosed(String userUid) throws ApiCallException {
    if (Session.getCredentials().isDemoCredentials()) {
        return false;
    }
    // Lets for a last event with that orgunit/program
    String url = PreferencesState.getInstance().getDhisURL() + "/api/" + TAG_USER + String.format(QUERY_USER_ATTRIBUTES, userUid);
    url = ServerApiUtils.encodeBlanks(url);
    Date closedDate = null;
    Response response = ServerApiCallExecution.executeCall(null, url, "GET");
    JsonNode jsonNode = ServerApiUtils.getJsonNodeMappedResponse(ServerApiUtils.getApiResponseAsJSONObject(response));
    JsonNode jsonNodeArray = jsonNode.get(ATTRIBUTEVALUES);
    String closeDateAsString = "";
    for (int i = 0; i < jsonNodeArray.size(); i++) {
        if (jsonNodeArray.get(i).get(ATTRIBUTE).get(CODE).textValue().equals(User.ATTRIBUTE_USER_CLOSE_DATE)) {
            closeDateAsString = jsonNodeArray.get(i).get(VALUE).textValue();
        }
    }
    if (closeDateAsString == null || closeDateAsString.equals("")) {
        return false;
    }
    closedDate = Utils.parseStringToCalendar(closeDateAsString, DHIS2_GMT_NEW_DATE_FORMAT).getTime();
    if (closedDate == null) {
        return false;
    }
    return closedDate.before(new Date());
}
Also used : Response(com.squareup.okhttp.Response) JsonNode(com.fasterxml.jackson.databind.JsonNode) Date(java.util.Date)

Example 29 with Response

use of com.squareup.okhttp.Response in project pictureapp by EyeSeeTea.

the class ServerAPIController method patchClosedDate.

/**
 * Updates the orgUnit adding a closedDate
 */
static void patchClosedDate(String url, String orgUnitUID) throws ApiCallException {
    // https://malariacare.psi.org/api/organisationUnits/u5jlxuod8xQ/closedDate
    try {
        String urlPathClosedDate = getPatchClosedDateUrl(url, orgUnitUID);
        JSONObject data = prepareTodayDateValue();
        Response response = ServerApiCallExecution.executeCall(data, urlPathClosedDate, "PATCH");
        ServerApiUtils.checkResponse(response, null);
    } 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) JSONException(org.json.JSONException)

Example 30 with Response

use of com.squareup.okhttp.Response in project pictureapp by EyeSeeTea.

the class ServerAPIController method patchDescription.

static void patchDescription(OrganisationUnit organisationUnit) {
    String url = ServerAPIController.getServerUrl();
    try {
        String urlPathClosedDescription = getPatchClosedDescriptionUrl(url, organisationUnit.getUid());
        JSONObject data = parseOrganisationUnitDescriptionToJson(organisationUnit.getDescription());
        Response response = ServerApiCallExecution.executeCall(data, urlPathClosedDescription, "PATCH");
        ServerApiUtils.checkResponse(response, null);
    } catch (Exception e) {
        Log.e(TAG, String.format("patchDescriptionClosedDate(%s,%s): %s", url, organisationUnit.getUid(), e.getMessage()));
    }
}
Also used : Response(com.squareup.okhttp.Response) JSONObject(org.json.JSONObject) 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)

Aggregations

Response (com.squareup.okhttp.Response)108 Request (com.squareup.okhttp.Request)75 IOException (java.io.IOException)72 OkHttpClient (com.squareup.okhttp.OkHttpClient)33 RequestBody (com.squareup.okhttp.RequestBody)24 JSONException (org.json.JSONException)16 JSONObject (org.json.JSONObject)15 FormEncodingBuilder (com.squareup.okhttp.FormEncodingBuilder)14 UnsupportedEncodingException (java.io.UnsupportedEncodingException)10 ApiCallException (org.eyeseetea.malariacare.domain.exception.ApiCallException)10 Callback (com.squareup.okhttp.Callback)9 SocketTimeoutException (java.net.SocketTimeoutException)8 File (java.io.File)7 Buffer (okio.Buffer)7 ShowException (org.eyeseetea.malariacare.views.ShowException)7 MediaType (com.squareup.okhttp.MediaType)5 InputStream (java.io.InputStream)5 ResponseBody (com.squareup.okhttp.ResponseBody)4 ApiException (io.kubernetes.client.ApiException)4 FileOutputStream (java.io.FileOutputStream)4