Search in sources :

Example 1 with HelperUtils

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

the class RestClient method post.

public <T> CompletableFuture<T> post(RequestEntry requestEntry, T entity, RequestContext requestContext, Class<T> responseType) {
    CompletableFuture<T> future = new CompletableFuture<>();
    String endpoint = requestEntry.buildEndpoint();
    JsonObject recordData = JsonObject.mapFrom(entity);
    if (logger.isDebugEnabled()) {
        logger.debug("Sending 'POST {}' with body: {}", endpoint, Optional.ofNullable(recordData).map(JsonObject::encodePrettily).orElse(null));
    }
    HttpClientInterface client = getHttpClient(requestContext.getHeaders());
    try {
        client.request(HttpMethod.POST, Optional.ofNullable(recordData).map(JsonObject::toBuffer).orElse(null), endpoint, requestContext.getHeaders()).thenApply(HelperUtils::verifyAndExtractBody).thenAccept(body -> {
            client.closeClient();
            T responseEntity = Optional.ofNullable(body).map(json -> json.mapTo(responseType)).orElse(null);
            if (logger.isDebugEnabled()) {
                logger.debug("'POST {}' request successfully processed. Record with '{}' id has been created", endpoint, body);
            }
            future.complete(responseEntity);
        }).exceptionally(t -> {
            client.closeClient();
            logger.error("'POST {}' request failed. Request body: {}", endpoint, Optional.ofNullable(recordData).map(JsonObject::encodePrettily).orElse(null), t.getCause());
            future.completeExceptionally(t.getCause());
            return null;
        });
    } catch (Exception e) {
        logger.error("'POST {}' request failed. Request body: {}", endpoint, Optional.ofNullable(recordData).map(JsonObject::encodePrettily).orElse(null), 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) HelperUtils(org.folio.invoices.utils.HelperUtils) OKAPI_HEADER_TENANT(org.folio.rest.RestVerticle.OKAPI_HEADER_TENANT) JsonObject(io.vertx.core.json.JsonObject)

Example 2 with HelperUtils

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

the class RestClient method delete.

public CompletableFuture<Void> delete(RequestEntry requestEntry, RequestContext requestContext) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    String endpoint = requestEntry.buildEndpoint();
    if (logger.isDebugEnabled()) {
        logger.debug(CALLING_ENDPOINT_MSG, HttpMethod.DELETE, endpoint);
    }
    HttpClientInterface client = getHttpClient(requestContext.getHeaders());
    setDefaultHeaders(client);
    try {
        client.request(HttpMethod.DELETE, endpoint, requestContext.getHeaders()).thenAccept(HelperUtils::verifyResponse).thenAccept(aVoid -> {
            client.closeClient();
            future.complete(null);
        }).exceptionally(t -> {
            client.closeClient();
            logger.error(String.format(EXCEPTION_CALLING_ENDPOINT_MSG, HttpMethod.DELETE, endpoint, requestContext), t);
            future.completeExceptionally(t.getCause());
            return null;
        });
    } catch (Exception e) {
        client.closeClient();
        logger.error(String.format(EXCEPTION_CALLING_ENDPOINT_MSG, HttpMethod.DELETE, endpoint, requestContext), e);
        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) HelperUtils(org.folio.invoices.utils.HelperUtils)

Example 3 with HelperUtils

use of org.folio.invoices.utils.HelperUtils in project mod-invoice 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(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) HelperUtils(org.folio.invoices.utils.HelperUtils) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

HttpMethod (io.vertx.core.http.HttpMethod)3 JsonObject (io.vertx.core.json.JsonObject)3 Collections (java.util.Collections)3 Map (java.util.Map)3 Objects.nonNull (java.util.Objects.nonNull)3 Optional (java.util.Optional)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 APPLICATION_JSON (javax.ws.rs.core.MediaType.APPLICATION_JSON)3 TEXT_PLAIN (javax.ws.rs.core.MediaType.TEXT_PLAIN)3 LogManager (org.apache.logging.log4j.LogManager)3 Logger (org.apache.logging.log4j.Logger)3 HelperUtils (org.folio.invoices.utils.HelperUtils)3 HelperUtils.verifyAndExtractBody (org.folio.invoices.utils.HelperUtils.verifyAndExtractBody)3 RestConstants (org.folio.rest.RestConstants)3 OKAPI_HEADER_TENANT (org.folio.rest.RestVerticle.OKAPI_HEADER_TENANT)3 RequestContext (org.folio.rest.core.models.RequestContext)3 RequestEntry (org.folio.rest.core.models.RequestEntry)3 HttpClientFactory (org.folio.rest.tools.client.HttpClientFactory)3 HttpClientInterface (org.folio.rest.tools.client.interfaces.HttpClientInterface)3 TenantTool (org.folio.rest.tools.utils.TenantTool)3