use of org.folio.rest.core.models.RequestContext in project mod-orders by folio-org.
the class RestClient method post.
public <T> CompletableFuture<T> post(RequestEntry requestEntry, JsonObject recordData, PostResponseType postResponseType, Class<T> responseType, RequestContext requestContext) {
CompletableFuture<T> future = new CompletableFuture<>();
String endpoint = requestEntry.buildEndpoint();
if (logger.isDebugEnabled()) {
logger.debug("Sending 'POST {}' with body: {}", endpoint, recordData.encodePrettily());
}
HttpClientInterface client = getHttpClient(requestContext.getHeaders());
try {
client.request(HttpMethod.POST, recordData.toBuffer(), endpoint, requestContext.getHeaders()).thenApply(response -> {
client.closeClient();
if (postResponseType == PostResponseType.BODY) {
JsonObject body = HelperUtils.verifyAndExtractBody(response);
if (responseType == JsonObject.class) {
return body;
}
T responseEntity = body.mapTo(responseType);
if (logger.isDebugEnabled()) {
logger.debug("'POST {}' request successfully processed. Record with '{}' id has been created", endpoint, body);
}
return responseEntity;
} else if (postResponseType == PostResponseType.UUID) {
return HelperUtils.verifyAndExtractRecordId(response);
}
return HelperUtils.verifyAndExtractBody(response);
}).thenAccept(body -> {
client.closeClient();
if (logger.isDebugEnabled()) {
logger.debug("'POST {}' request successfully processed. Record with '{}' id has been created", endpoint, body);
}
future.complete(responseType.cast(body));
}).exceptionally(t -> {
client.closeClient();
logger.error("'POST {}' request failed. Request body: {}", endpoint, recordData.encodePrettily(), t.getCause());
future.completeExceptionally(t.getCause());
return null;
});
} catch (Exception e) {
logger.error("'POST {}' request failed. Request body: {}", endpoint, recordData.encodePrettily(), e);
client.closeClient();
future.completeExceptionally(e);
}
return future;
}
use of org.folio.rest.core.models.RequestContext in project mod-orders by folio-org.
the class RestClient method delete.
public CompletableFuture<Void> delete(RequestEntry requestEntry, boolean skipThrowNorFoundException, 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(response -> {
client.closeClient();
int code = response.getCode();
if (code == NOT_FOUND && skipThrowNorFoundException) {
String errorMessage = (response.getError() != null) ? response.getError().getString(ERROR_MESSAGE) : StringUtils.EMPTY;
logger.error("The GET {} operation completed with {} error: {}", endpoint, code, errorMessage);
future.complete(null);
} else {
verifyResponse(response);
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.rest.core.models.RequestContext 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;
}
use of org.folio.rest.core.models.RequestContext in project mod-orders by folio-org.
the class TransactionService method getTransactionsByIds.
public CompletableFuture<List<Transaction>> getTransactionsByIds(List<String> trIds, RequestContext requestContext) {
return collectResultsOnSuccess(ofSubLists(new ArrayList<>(trIds), MAX_IDS_FOR_GET_RQ).map(ids -> getTransactionsChunksByIds(ids, requestContext)).toList()).thenApply(lists -> lists.stream().flatMap(Collection::stream).collect(Collectors.toList())).thenApply(trList -> {
if (trList.size() != trIds.size()) {
List<Parameter> parameters = new ArrayList<>();
parameters.add(new Parameter().withKey("trIds").withValue(trIds.toString()));
throw new HttpException(500, ERROR_RETRIEVING_TRANSACTION.toError().withParameters(parameters));
}
return trList;
});
}
use of org.folio.rest.core.models.RequestContext in project mod-orders by folio-org.
the class InventoryManager method fetchHoldingsByFundIds.
private CompletableFuture<List<JsonObject>> fetchHoldingsByFundIds(List<String> holdingIds, RequestContext requestContext) {
String query = convertIdsToCqlQuery(holdingIds);
RequestEntry requestEntry = new RequestEntry(INVENTORY_LOOKUP_ENDPOINTS.get(HOLDINGS_RECORDS)).withQuery(query).withOffset(0).withLimit(MAX_IDS_FOR_GET_RQ);
return restClient.getAsJsonObject(requestEntry, requestContext).thenApply(jsonHoldings -> jsonHoldings.getJsonArray(HOLDINGS_RECORDS).stream().map(o -> ((JsonObject) o)).collect(toList())).thenApply(holdings -> {
if (holdings.size() == holdingIds.size()) {
return holdings;
}
List<Parameter> parameters = holdingIds.stream().filter(id -> holdings.stream().noneMatch(holding -> holding.getString(ID).equals(id))).map(id -> new Parameter().withValue(id).withKey("holdings")).collect(Collectors.toList());
throw new HttpException(404, PARTIALLY_RETURNED_COLLECTION.toError().withParameters(parameters));
});
}
Aggregations