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