Search in sources :

Example 6 with HelperUtils

use of org.folio.orders.utils.HelperUtils in project mod-orders by folio-org.

the class RestClient method put.

public <T> CompletableFuture<Void> put(RequestEntry requestEntry, T entity, RequestContext requestContext) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    String endpoint = requestEntry.buildEndpoint();
    JsonObject recordData = JsonObject.mapFrom(entity);
    if (logger.isDebugEnabled()) {
        logger.debug("Sending 'PUT {}' with body: {}", endpoint, recordData.encodePrettily());
    }
    HttpClientInterface client = getHttpClient(requestContext.getHeaders());
    setDefaultHeaders(client);
    try {
        client.request(HttpMethod.PUT, recordData.toBuffer(), endpoint, requestContext.getHeaders()).thenAccept(HelperUtils::verifyResponse).thenAccept(avoid -> {
            client.closeClient();
            future.complete(null);
        }).exceptionally(t -> {
            client.closeClient();
            future.completeExceptionally(t.getCause());
            logger.error("'PUT {}' request failed. Request body: {}", endpoint, recordData.encodePrettily(), t.getCause());
            return null;
        });
    } catch (Exception e) {
        logger.error("'PUT {}' request failed. Request body: {}", endpoint, recordData.encodePrettily(), e);
        client.closeClient();
        future.completeExceptionally(e);
    }
    return future;
}
Also used : HelperUtils.verifyResponse(org.folio.orders.utils.HelperUtils.verifyResponse) ERROR_MESSAGE(org.folio.rest.RestConstants.ERROR_MESSAGE) HelperUtils(org.folio.orders.utils.HelperUtils) RequestEntry(org.folio.rest.core.models.RequestEntry) OKAPI_HEADER_TENANT(org.folio.rest.RestVerticle.OKAPI_HEADER_TENANT) CompletableFuture(java.util.concurrent.CompletableFuture) StringUtils(org.apache.commons.lang3.StringUtils) TenantTool(org.folio.rest.tools.utils.TenantTool) HttpClientFactory(org.folio.rest.tools.client.HttpClientFactory) NOT_FOUND(org.folio.rest.RestConstants.NOT_FOUND) HelperUtils.verifyAndExtractBody(org.folio.orders.utils.HelperUtils.verifyAndExtractBody) 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) 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) HelperUtils(org.folio.orders.utils.HelperUtils) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)6 Collections (java.util.Collections)6 Map (java.util.Map)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 LogManager (org.apache.logging.log4j.LogManager)6 Logger (org.apache.logging.log4j.Logger)6 HelperUtils (org.folio.orders.utils.HelperUtils)6 RequestContext (org.folio.rest.core.models.RequestContext)6 HttpClientInterface (org.folio.rest.tools.client.interfaces.HttpClientInterface)6 StringUtils (org.apache.commons.lang3.StringUtils)5 HttpMethod (io.vertx.core.http.HttpMethod)4 APPLICATION_JSON (javax.ws.rs.core.MediaType.APPLICATION_JSON)4 TEXT_PLAIN (javax.ws.rs.core.MediaType.TEXT_PLAIN)4 OKAPI_HEADER_TENANT (org.folio.rest.RestVerticle.OKAPI_HEADER_TENANT)4 List (java.util.List)3 Objects.nonNull (java.util.Objects.nonNull)3 HelperUtils.verifyAndExtractBody (org.folio.orders.utils.HelperUtils.verifyAndExtractBody)3 HelperUtils.verifyResponse (org.folio.orders.utils.HelperUtils.verifyResponse)3 RestConstants (org.folio.rest.RestConstants)3 ERROR_MESSAGE (org.folio.rest.RestConstants.ERROR_MESSAGE)3