Search in sources :

Example 36 with JsonNode

use of com.mashape.unirest.http.JsonNode in project goci by EBISPOT.

the class EnsemblRestService method fetchJson.

/**
 * Fetch response from API
 *
 * @param url URL to query
 * @return the corresponding result
 */
private RestResponseResult fetchJson(String url) throws UnirestException, InterruptedException, EnsemblRestIOException {
    RestResponseResult restResponseResult = new RestResponseResult();
    restResponseResult.setUrl(url);
    // Parameters to monitor API call success
    int maxTries = 5;
    int tries = 0;
    int wait = 1;
    boolean success = false;
    while (!success && tries < maxTries) {
        tries++;
        try {
            getLog().trace("Querying URL: " + url);
            HttpResponse<JsonNode> response = Unirest.get(url).header("Content-Type", "application/json").asJson();
            String retryHeader = response.getHeaders().getFirst("Retry-After");
            getLog().trace("URL response: " + response.getStatus());
            if (response.getStatus() == 200) {
                // Success
                success = true;
            // restResponseResult.setRestResult(response.getBody());
            } else if (response.getStatus() == 429 && retryHeader != null) {
                // Too Many Requests
                Long waitSeconds = Long.valueOf(retryHeader);
                Thread.sleep(waitSeconds * 1000);
            } else {
                if (response.getStatus() == 503) {
                    // Service unavailable
                    restResponseResult.setError("No server is available to handle this request (Error 503: service unavailable) at url: " + url);
                    getLog().error("No server is available to handle this request (Error 503: service unavailable) at url: " + url);
                    throw new EnsemblRestIOException("No server is available to handle this request (Error 503: service unavailable) at url: " + url);
                } else if (response.getStatus() == 400) {
                    // Bad request (no result found)
                    JSONObject json_obj = response.getBody().getObject();
                    if (json_obj.has("error")) {
                        restResponseResult.setError(json_obj.getString("error"));
                    }
                    getLog().error(url + " is generating an invalid request. (Error 400: bad request)");
                    // Success is set to true here as the API call was successful
                    // but the gene or snp does not exist in Ensembl
                    success = true;
                } else {
                    // Other issue
                    restResponseResult.setError("No data available at url " + url);
                    getLog().error("No data at " + url);
                    throw new EnsemblRestIOException("No data available at url " + url);
                }
            }
        } catch (UnirestException e) {
            getLog().error("Caught exception from Ensembl Rest call, this call will be retried after " + wait + "s.", e);
            Thread.sleep(wait * 1000);
        }
    }
    if (success) {
        return restResponseResult;
    } else {
        getLog().error("Failed to obtain a result from from '" + url + "' after after " + maxTries + " attempts");
        throw new EnsemblRestIOException("Failed to obtain a result from '" + url + "' after " + maxTries + " attempts");
    }
}
Also used : JSONObject(org.json.JSONObject) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) RestResponseResult(uk.ac.ebi.spot.goci.model.RestResponseResult) JsonNode(com.mashape.unirest.http.JsonNode) EnsemblRestIOException(uk.ac.ebi.spot.goci.exception.EnsemblRestIOException)

Example 37 with JsonNode

use of com.mashape.unirest.http.JsonNode in project goci by EBISPOT.

the class EnsemblDbService method buildNode.

private JsonNode buildNode(Map<String, String> data) {
    JSONObject dataObj = new JSONObject();
    data.forEach((k, v) -> {
        dataObj.put(k, v);
    });
    return new JsonNode(dataObj.toString());
}
Also used : JSONObject(org.json.JSONObject) JsonNode(com.mashape.unirest.http.JsonNode)

Example 38 with JsonNode

use of com.mashape.unirest.http.JsonNode in project goci by EBISPOT.

the class EnsemblRestcallHistoryService method getEnsemblRestCallByTypeAndParamAndVersion.

// BEWARE:If the Ensembl release is valid, the system can try to retrieve the data from the table
public RestResponseResult getEnsemblRestCallByTypeAndParamAndVersion(String type, String param, String eRelease) {
    RestResponseResult restResponseResult = null;
    // Without release it is pointless stores the info.
    if (eRelease != null) {
        if (!(eRelease.isEmpty())) {
            try {
                Collection<EnsemblRestcallHistory> urls = ensemblRestcallHistoryRepository.findByRequestTypeAndEnsemblParamAndEnsemblVersion(type, param, eRelease);
                if (urls.size() > 0) {
                    EnsemblRestcallHistory result = urls.iterator().next();
                    restResponseResult = new RestResponseResult();
                    restResponseResult.setUrl(result.getEnsemblUrl());
                    String restApiError = result.getEnsemblError();
                    if (restApiError != null && !restApiError.isEmpty()) {
                        restResponseResult.setError(restApiError);
                    } else {
                        ObjectMapper mapper = new ObjectMapper();
                        JsonNode jsonNode = mapper.convertValue(result.getEnsemblResponse().toString(), JsonNode.class);
                        restResponseResult.setRestResult(jsonNode);
                    }
                }
            } catch (Exception e) {
            // BEWARE: the following code MUST NOT block Ensembl Rest API Call
            }
        }
    }
    return restResponseResult;
}
Also used : RestResponseResult(uk.ac.ebi.spot.goci.model.RestResponseResult) JsonNode(com.mashape.unirest.http.JsonNode) EnsemblRestcallHistory(uk.ac.ebi.spot.goci.model.EnsemblRestcallHistory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 39 with JsonNode

use of com.mashape.unirest.http.JsonNode in project goci by EBISPOT.

the class EnsemblRestTemplateService method getRelease.

public String getRelease() {
    String eRelease = null;
    RestTemplate restTemplate = this.getRestTemplate();
    HttpEntity<Object> entity = this.getEntity();
    String url = createUrl("/info/data/", "");
    getLog().debug("Querying " + url);
    try {
        ResponseEntity<String> output = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
        JsonNode body = new JsonNode(output.getBody().toString());
        if ((body != null) && (body.getObject() != null)) {
            JSONObject release = body.getObject();
            if (release.has("releases")) {
                JSONArray releases = release.getJSONArray("releases");
                eRelease = String.valueOf(releases.get(0));
            }
        }
    } catch (Exception e) {
    /*No release */
    }
    return eRelease;
}
Also used : JSONObject(org.json.JSONObject) RestTemplate(org.springframework.web.client.RestTemplate) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) JsonNode(com.mashape.unirest.http.JsonNode) MalformedURLException(java.net.MalformedURLException) EnsemblRestClientException(uk.ac.ebi.spot.goci.exception.EnsemblRestClientException)

Example 40 with JsonNode

use of com.mashape.unirest.http.JsonNode in project zeppelin by apache.

the class HttpBasedClient method index.

@Override
public ActionResponse index(String index, String type, String id, String data) {
    ActionResponse response = null;
    try {
        HttpRequestWithBody request = null;
        if (StringUtils.isEmpty(id)) {
            request = Unirest.post(getUrl(index, type, id, false));
        } else {
            request = Unirest.put(getUrl(index, type, id, false));
        }
        request.header("Accept", "application/json").header("Content-Type", "application/json").body(data).getHttpRequest();
        if (StringUtils.isNotEmpty(username)) {
            request.basicAuth(username, password);
        }
        final HttpResponse<JsonNode> result = request.asJson();
        final boolean isSucceeded = isSucceeded(result);
        if (isSucceeded) {
            response = new ActionResponse().succeeded(true).hit(new HitWrapper(getFieldAsString(result, "_index"), getFieldAsString(result, "_type"), getFieldAsString(result, "_id"), null));
        } else {
            throw new ActionException(result.getBody().toString());
        }
    } catch (final UnirestException e) {
        throw new ActionException(e);
    }
    return response;
}
Also used : HitWrapper(org.apache.zeppelin.elasticsearch.action.HitWrapper) HttpRequestWithBody(com.mashape.unirest.request.HttpRequestWithBody) ActionException(org.apache.zeppelin.elasticsearch.action.ActionException) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) JsonNode(com.mashape.unirest.http.JsonNode) ActionResponse(org.apache.zeppelin.elasticsearch.action.ActionResponse)

Aggregations

JsonNode (com.mashape.unirest.http.JsonNode)47 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)20 JSONObject (org.json.JSONObject)19 IOException (java.io.IOException)14 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)12 JSONArray (org.json.JSONArray)11 RestResponseResult (uk.ac.ebi.spot.goci.model.RestResponseResult)5 Incident (io.github.asw.i3a.operatorsWebClient.entitites.Incident)4 List (java.util.List)4 ActionException (org.apache.zeppelin.elasticsearch.action.ActionException)4 ActionResponse (org.apache.zeppelin.elasticsearch.action.ActionResponse)4 HitWrapper (org.apache.zeppelin.elasticsearch.action.HitWrapper)4 JSONException (org.json.JSONException)4 Gson (com.google.gson.Gson)3 Type (java.lang.reflect.Type)3 JsonObjectBuilder (javax.json.JsonObjectBuilder)3 Test (org.junit.Test)3 RestTemplate (org.springframework.web.client.RestTemplate)3 HttpResponse (com.mashape.unirest.http.HttpResponse)2 HttpRequest (com.mashape.unirest.request.HttpRequest)2