use of org.folio.rest.core.models.RequestEntry in project mod-invoice by folio-org.
the class RestClient method get.
public <S> CompletableFuture<S> get(RequestEntry requestEntry, RequestContext requestContext, Class<S> responseType) {
CompletableFuture<S> future = new CompletableFuture<>();
String endpoint = requestEntry.buildEndpoint();
HttpClientInterface client = getHttpClient(requestContext.getHeaders());
if (logger.isDebugEnabled()) {
logger.debug("Calling GET {}", endpoint);
}
try {
client.request(HttpMethod.GET, endpoint, requestContext.getHeaders()).thenApply(response -> {
if (logger.isDebugEnabled()) {
logger.debug("Validating response for GET {}", endpoint);
}
return verifyAndExtractBody(response);
}).thenAccept(body -> {
client.closeClient();
if (logger.isDebugEnabled()) {
logger.debug("The response body for GET {}: {}", endpoint, nonNull(body) ? body.encodePrettily() : null);
}
S responseEntity = body.mapTo(responseType);
future.complete(responseEntity);
}).exceptionally(t -> {
client.closeClient();
logger.error(String.format(EXCEPTION_CALLING_ENDPOINT_MSG, HttpMethod.GET, endpoint, requestContext), t);
future.completeExceptionally(t.getCause());
return null;
});
} catch (Exception e) {
logger.error(String.format(EXCEPTION_CALLING_ENDPOINT_MSG, HttpMethod.GET, requestEntry.getBaseEndpoint(), requestContext), e);
client.closeClient();
future.completeExceptionally(e);
}
return future;
}
Aggregations