Search in sources :

Example 11 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 12 with JsonNode

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

the class SpringerFetcher method processQuery.

@Override
public boolean processQuery(String query, ImportInspector inspector, OutputPrinter status) {
    shouldContinue = true;
    try {
        status.setStatus(Localization.lang("Searching..."));
        HttpResponse<JsonNode> jsonResponse;
        String encodedQuery = URLEncoder.encode(query, "UTF-8");
        jsonResponse = Unirest.get(API_URL + encodedQuery + "&api_key=" + API_KEY + "&p=1").header("accept", "application/json").asJson();
        JSONObject jo = jsonResponse.getBody().getObject();
        int numberToFetch = jo.getJSONArray("result").getJSONObject(0).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 startItem = 1; startItem <= numberToFetch; startItem += MAX_PER_PAGE) {
                if (!shouldContinue) {
                    break;
                }
                int noToFetch = Math.min(MAX_PER_PAGE, (numberToFetch - startItem) + 1);
                jsonResponse = Unirest.get(API_URL + encodedQuery + "&api_key=" + API_KEY + "&p=" + noToFetch + "&s=" + startItem).header("accept", "application/json").asJson();
                jo = jsonResponse.getBody().getObject();
                if (jo.has("records")) {
                    JSONArray results = jo.getJSONArray("records");
                    for (int i = 0; i < results.length(); i++) {
                        JSONObject springerJsonEntry = results.getJSONObject(i);
                        BibEntry entry = JSONEntryParser.parseSpringerJSONtoBibtex(springerJsonEntry);
                        inspector.addEntry(entry);
                        fetched++;
                        inspector.setProgress(fetched, numberToFetch);
                    }
                }
            }
            return true;
        } else {
            status.showMessage(Localization.lang("No entries found for the search string '%0'", encodedQuery), Localization.lang("Search %0", getTitle()), JOptionPane.INFORMATION_MESSAGE);
            return false;
        }
    } catch (IOException | 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) IOException(java.io.IOException)

Example 13 with JsonNode

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

the class SpringerLink method findFullText.

@Override
public Optional<URL> findFullText(BibEntry entry) throws IOException {
    Objects.requireNonNull(entry);
    Optional<URL> pdfLink = Optional.empty();
    // Try unique DOI first
    Optional<DOI> doi = entry.getField(FieldName.DOI).flatMap(DOI::parse);
    if (doi.isPresent()) {
        // Available in catalog?
        try {
            HttpResponse<JsonNode> jsonResponse = Unirest.get(API_URL).queryString("api_key", API_KEY).queryString("q", String.format("doi:%s", doi.get().getDOI())).asJson();
            JSONObject json = jsonResponse.getBody().getObject();
            int results = json.getJSONArray("result").getJSONObject(0).getInt("total");
            if (results > 0) {
                LOGGER.info("Fulltext PDF found @ Springer.");
                pdfLink = Optional.of(new URL("http", CONTENT_HOST, String.format("/content/pdf/%s.pdf", doi.get().getDOI())));
            }
        } catch (UnirestException e) {
            LOGGER.warn("SpringerLink API request failed", e);
        }
    }
    return pdfLink;
}
Also used : JSONObject(org.json.JSONObject) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) JsonNode(com.mashape.unirest.http.JsonNode) URL(java.net.URL) DOI(org.jabref.model.entry.identifier.DOI)

Example 14 with JsonNode

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

the class ScienceDirect method getUrlByDoi.

private String getUrlByDoi(String doi) throws UnirestException {
    String sciLink = "";
    try {
        String request = API_URL + doi;
        HttpResponse<JsonNode> jsonResponse = Unirest.get(request).header("X-ELS-APIKey", API_KEY).queryString("httpAccept", "application/json").asJson();
        JSONObject json = jsonResponse.getBody().getObject();
        JSONArray links = json.getJSONObject("full-text-retrieval-response").getJSONObject("coredata").getJSONArray("link");
        for (int i = 0; i < links.length(); i++) {
            JSONObject link = links.getJSONObject(i);
            if (link.getString("@rel").equals("scidir")) {
                sciLink = link.getString("@href");
            }
        }
        return sciLink;
    } catch (JSONException e) {
        LOGGER.debug("No ScienceDirect link found in API request", e);
        return sciLink;
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JsonNode(com.mashape.unirest.http.JsonNode)

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