Search in sources :

Example 6 with JsonNode

use of com.mashape.unirest.http.JsonNode in project jabref by JabRef.

the class DOAJFetcher method processQuery.

@Override
public boolean processQuery(String query, ImportInspector inspector, OutputPrinter status) {
    shouldContinue = true;
    try {
        status.setStatus(Localization.lang("Searching..."));
        HttpResponse<JsonNode> jsonResponse;
        jsonResponse = Unirest.get(SEARCH_URL + query + "?pageSize=1").header("accept", "application/json").asJson();
        JSONObject jo = jsonResponse.getBody().getObject();
        int numberToFetch = jo.getInt("total");
        if (numberToFetch > 0) {
            if (numberToFetch > MAX_PER_PAGE) {
                boolean numberEntered = false;
                do {
                    String strCount = JOptionPane.showInputDialog(Localization.lang("%0 references found. Number of references to fetch?", String.valueOf(numberToFetch)));
                    if (strCount == null) {
                        status.setStatus(Localization.lang("%0 import canceled", getTitle()));
                        return false;
                    }
                    try {
                        numberToFetch = Integer.parseInt(strCount.trim());
                        numberEntered = true;
                    } catch (NumberFormatException ex) {
                        status.showMessage(Localization.lang("Please enter a valid number"));
                    }
                } while (!numberEntered);
            }
            // Keep track of number of items fetched for the progress bar
            int fetched = 0;
            for (int page = 1; ((page - 1) * MAX_PER_PAGE) <= numberToFetch; page++) {
                if (!shouldContinue) {
                    break;
                }
                int noToFetch = Math.min(MAX_PER_PAGE, numberToFetch - ((page - 1) * MAX_PER_PAGE));
                jsonResponse = Unirest.get(SEARCH_URL + query + "?page=" + page + "&pageSize=" + noToFetch).header("accept", "application/json").asJson();
                jo = jsonResponse.getBody().getObject();
                if (jo.has("results")) {
                    JSONArray results = jo.getJSONArray("results");
                    for (int i = 0; i < results.length(); i++) {
                        JSONObject bibJsonEntry = results.getJSONObject(i).getJSONObject("bibjson");
                        BibEntry entry = jsonConverter.parseBibJSONtoBibtex(bibJsonEntry, Globals.prefs.getKeywordDelimiter());
                        inspector.addEntry(entry);
                        fetched++;
                        inspector.setProgress(fetched, numberToFetch);
                    }
                }
            }
            return true;
        } else {
            status.showMessage(Localization.lang("No entries found for the search string '%0'", query), Localization.lang("Search %0", getTitle()), JOptionPane.INFORMATION_MESSAGE);
            return false;
        }
    } catch (UnirestException e) {
        LOGGER.error("Error while fetching from " + getTitle(), e);
        ((ImportInspectionDialog) inspector).showErrorMessage(this.getTitle(), e.getLocalizedMessage());
        return false;
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) JsonNode(com.mashape.unirest.http.JsonNode)

Example 7 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)

Example 8 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 9 with JsonNode

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

the class EnsemblMappingPipeline method getOverlapRegionCalls.

/**
     * Ensembl REST API call for the overlap region endpoint
     *
     * @param chromosome the chromosome name
     * @param position1  the 5' position of the region
     * @param position2  the 3' position of the region
     * @param rest_opt   the extra parameters to add at the end of the REST call url (inherited from other methods)
     * @return A JSONArray object containing a list of JSONObjects corresponding to the genes overlapping the region
     */
private JSONArray getOverlapRegionCalls(String chromosome, Integer position1, Integer position2, String rest_opt, String eRelease) throws EnsemblRestIOException {
    String data = chromosome + ":" + position1 + "-" + position2;
    String param = data.concat("?").concat(rest_opt);
    RestResponseResult restResponseResult = ensemblRestcallHistoryService.getEnsemblRestCallByTypeAndParamAndVersion("overlap_region", param, eRelease);
    if (restResponseResult == null) {
        restResponseResult = ensemblRestTemplateService.getRestCall("overlap_region", data, rest_opt);
        ensemblRestcallHistoryService.create(restResponseResult, "overlap_region", param, eRelease);
    }
    JsonNode result = restResponseResult.getRestResult();
    JSONArray overlap_result = new JSONArray();
    if (result.isArray()) {
        overlap_result = result.getArray();
    } else {
        // Errors
        getEnsemblMappingResult().addPipelineErrors(restResponseResult.getError());
        overlap_result = new JSONArray("[{\"overlap_error\":\"1\"}]");
    }
    return overlap_result;
}
Also used : JSONArray(org.json.JSONArray) RestResponseResult(uk.ac.ebi.spot.goci.model.RestResponseResult) JsonNode(com.mashape.unirest.http.JsonNode)

Example 10 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)

Aggregations

JsonNode (com.mashape.unirest.http.JsonNode)14 JSONObject (org.json.JSONObject)9 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)8 JSONArray (org.json.JSONArray)7 ActionException (org.apache.zeppelin.elasticsearch.action.ActionException)4 ActionResponse (org.apache.zeppelin.elasticsearch.action.ActionResponse)4 HitWrapper (org.apache.zeppelin.elasticsearch.action.HitWrapper)4 RestResponseResult (uk.ac.ebi.spot.goci.model.RestResponseResult)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 HttpRequest (com.mashape.unirest.request.HttpRequest)2 HttpRequestWithBody (com.mashape.unirest.request.HttpRequestWithBody)2 MalformedURLException (java.net.MalformedURLException)2 BibEntry (org.jabref.model.entry.BibEntry)2 RestTemplate (org.springframework.web.client.RestTemplate)2 EnsemblRestClientException (uk.ac.ebi.spot.goci.exception.EnsemblRestClientException)2 JsonParseException (com.google.gson.JsonParseException)1 IOException (java.io.IOException)1 URL (java.net.URL)1 Iterator (java.util.Iterator)1 AggWrapper (org.apache.zeppelin.elasticsearch.action.AggWrapper)1