Search in sources :

Example 1 with RequestEntry

use of org.folio.rest.core.models.RequestEntry in project mod-invoice by folio-org.

the class ConfigurationService method getConfigurationsEntries.

/**
 * Retrieve configuration by moduleName and configName from mod-configuration.
 *
 * @param searchCriteria name of the module for which the configuration is to be retrieved
 * @return CompletableFuture with Configs
 */
public CompletableFuture<Configs> getConfigurationsEntries(RequestContext requestContext, String... searchCriteria) {
    String query = buildSearchingQuery(searchCriteria);
    RequestEntry requestEntry = new RequestEntry(CONFIGURATION_ENDPOINT).withQuery(query).withOffset(0).withLimit(Integer.MAX_VALUE);
    return restClient.get(requestEntry, requestContext, Configs.class);
}
Also used : RequestEntry(org.folio.rest.core.models.RequestEntry)

Example 2 with RequestEntry

use of org.folio.rest.core.models.RequestEntry 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 3 with RequestEntry

use of org.folio.rest.core.models.RequestEntry 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 4 with RequestEntry

use of org.folio.rest.core.models.RequestEntry 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)

Example 5 with RequestEntry

use of org.folio.rest.core.models.RequestEntry in project mod-invoice by folio-org.

the class InvoiceCancelServiceTest method setupEncumbrancePut.

private void setupEncumbrancePut(Transaction transaction) {
    RequestEntry requestEntry = new RequestEntry("/finance/encumbrances/{id}").withPathParameter("id", transaction.getId());
    Transaction updatedTransaction = JsonObject.mapFrom(transaction).mapTo(Transaction.class);
    updatedTransaction.getEncumbrance().setStatus(UNRELEASED);
    doReturn(completedFuture(null)).when(restClient).put(argThat(re -> sameRequestEntry(requestEntry, re)), eq(updatedTransaction), eq(requestContextMock));
}
Also used : INVOICE_MOCK_DATA_PATH(org.folio.TestMockDataConstants.INVOICE_MOCK_DATA_PATH) BeforeEach(org.junit.jupiter.api.BeforeEach) TransactionType(org.folio.rest.acq.model.finance.Transaction.TransactionType) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) PurchaseOrder(org.folio.rest.acq.model.orders.PurchaseOrder) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) PoLine(org.folio.rest.acq.model.orders.PoLine) OrderLineService(org.folio.services.order.OrderLineService) VOUCHERS_STORAGE(org.folio.invoices.utils.ResourcePathResolver.VOUCHERS_STORAGE) INVOICE_TRANSACTION_SUMMARIES(org.folio.invoices.utils.ResourcePathResolver.INVOICE_TRANSACTION_SUMMARIES) Mockito.argThat(org.mockito.Mockito.argThat) TransactionCollection(org.folio.rest.acq.model.finance.TransactionCollection) MOCK_CREDITS_LIST(org.folio.TestMockDataConstants.MOCK_CREDITS_LIST) Collections.singletonList(java.util.Collections.singletonList) VOUCHER_MOCK_DATA_PATH(org.folio.TestMockDataConstants.VOUCHER_MOCK_DATA_PATH) HttpException(org.folio.invoices.rest.exceptions.HttpException) Voucher(org.folio.rest.jaxrs.model.Voucher) InvoiceTransactionSummaryService(org.folio.services.finance.transaction.InvoiceTransactionSummaryService) VoucherCommandService(org.folio.services.voucher.VoucherCommandService) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) InvoiceTransactionSummary(org.folio.rest.acq.model.finance.InvoiceTransactionSummary) JsonObject(io.vertx.core.json.JsonObject) Method(java.lang.reflect.Method) ERROR_UNRELEASING_ENCUMBRANCES(org.folio.invoices.utils.ErrorCodes.ERROR_UNRELEASING_ENCUMBRANCES) Mockito.doReturn(org.mockito.Mockito.doReturn) RELEASED(org.folio.rest.acq.model.finance.Encumbrance.Status.RELEASED) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) ORDER_TRANSACTION_SUMMARIES(org.folio.invoices.utils.ResourcePathResolver.ORDER_TRANSACTION_SUMMARIES) CompletionException(java.util.concurrent.CompletionException) MOCK_ENCUMBRANCES_LIST(org.folio.TestMockDataConstants.MOCK_ENCUMBRANCES_LIST) UUID(java.util.UUID) Collectors.joining(java.util.stream.Collectors.joining) Test(org.junit.jupiter.api.Test) CompletableFuture.failedFuture(java.util.concurrent.CompletableFuture.failedFuture) List(java.util.List) FINANCE_TRANSACTIONS(org.folio.invoices.utils.ResourcePathResolver.FINANCE_TRANSACTIONS) PENDING(org.folio.rest.acq.model.finance.Encumbrance.Status.PENDING) PurchaseOrderCollection(org.folio.rest.acq.model.orders.PurchaseOrderCollection) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) UNRELEASED(org.folio.rest.acq.model.finance.Encumbrance.Status.UNRELEASED) WorkflowStatus(org.folio.rest.acq.model.orders.PurchaseOrder.WorkflowStatus) Invoice(org.folio.rest.jaxrs.model.Invoice) MOCK_PAYMENTS_LIST(org.folio.TestMockDataConstants.MOCK_PAYMENTS_LIST) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) RestClient(org.folio.rest.core.RestClient) EncumbranceService(org.folio.services.finance.transaction.EncumbranceService) CANNOT_CANCEL_INVOICE(org.folio.invoices.utils.ErrorCodes.CANNOT_CANCEL_INVOICE) PoLineCollection(org.folio.rest.acq.model.orders.PoLineCollection) CompletableFuture(java.util.concurrent.CompletableFuture) Transaction(org.folio.rest.acq.model.finance.Transaction) VoucherCollection(org.folio.rest.jaxrs.model.VoucherCollection) InvoiceLineCollection(org.folio.rest.jaxrs.model.InvoiceLineCollection) Answer(org.mockito.stubbing.Answer) OrderTransactionSummary(org.folio.rest.acq.model.finance.OrderTransactionSummary) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RequestContext(org.folio.rest.core.models.RequestContext) Encumbrance(org.folio.rest.acq.model.finance.Encumbrance) BaseTransactionService(org.folio.services.finance.transaction.BaseTransactionService) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MOCK_PENDING_PAYMENTS_LIST(org.folio.TestMockDataConstants.MOCK_PENDING_PAYMENTS_LIST) OrderService(org.folio.services.order.OrderService) ResourcePathResolver.resourcesPath(org.folio.invoices.utils.ResourcePathResolver.resourcesPath) Files(java.nio.file.Files) VoucherRetrieveService(org.folio.services.voucher.VoucherRetrieveService) Vertx(io.vertx.core.Vertx) RequestEntry(org.folio.rest.core.models.RequestEntry) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) Collectors.toList(java.util.stream.Collectors.toList) Paths(java.nio.file.Paths) INVOICE_LINES_LIST_PATH(org.folio.TestMockDataConstants.INVOICE_LINES_LIST_PATH) OrderTransactionSummaryService(org.folio.services.finance.transaction.OrderTransactionSummaryService) Transaction(org.folio.rest.acq.model.finance.Transaction) RequestEntry(org.folio.rest.core.models.RequestEntry)

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