Search in sources :

Example 96 with RequestEntry

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;
}
Also used : HelperUtils(org.folio.invoices.utils.HelperUtils) RequestEntry(org.folio.rest.core.models.RequestEntry) OKAPI_HEADER_TENANT(org.folio.rest.RestVerticle.OKAPI_HEADER_TENANT) CompletableFuture(java.util.concurrent.CompletableFuture) TenantTool(org.folio.rest.tools.utils.TenantTool) HttpClientFactory(org.folio.rest.tools.client.HttpClientFactory) Logger(org.apache.logging.log4j.Logger) HttpClientInterface(org.folio.rest.tools.client.interfaces.HttpClientInterface) HttpMethod(io.vertx.core.http.HttpMethod) Map(java.util.Map) RequestContext(org.folio.rest.core.models.RequestContext) Optional(java.util.Optional) HelperUtils.verifyAndExtractBody(org.folio.invoices.utils.HelperUtils.verifyAndExtractBody) JsonObject(io.vertx.core.json.JsonObject) Objects.nonNull(java.util.Objects.nonNull) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) RestConstants(org.folio.rest.RestConstants) TEXT_PLAIN(javax.ws.rs.core.MediaType.TEXT_PLAIN) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) CompletableFuture(java.util.concurrent.CompletableFuture) HttpClientInterface(org.folio.rest.tools.client.interfaces.HttpClientInterface)

Aggregations

RequestEntry (org.folio.rest.core.models.RequestEntry)96 JsonObject (io.vertx.core.json.JsonObject)38 CompletableFuture (java.util.concurrent.CompletableFuture)37 RequestContext (org.folio.rest.core.models.RequestContext)33 Test (org.junit.jupiter.api.Test)30 Collections (java.util.Collections)22 List (java.util.List)21 RestClient (org.folio.rest.core.RestClient)21 LogManager (org.apache.logging.log4j.LogManager)20 Logger (org.apache.logging.log4j.Logger)20 Map (java.util.Map)19 CompletionException (java.util.concurrent.CompletionException)19 Transaction (org.folio.rest.acq.model.finance.Transaction)18 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)17 Collectors.toList (java.util.stream.Collectors.toList)16 TenantTool (org.folio.rest.tools.utils.TenantTool)16 Collections.singletonList (java.util.Collections.singletonList)14 UUID (java.util.UUID)14 Collectors.joining (java.util.stream.Collectors.joining)14 Vertx (io.vertx.core.Vertx)13