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 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;
}
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;
}
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;
}
}
Aggregations