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