diff --git a/src/main/java/io/falu/client/AbstractHttpApiClient.java b/src/main/java/io/falu/client/AbstractHttpApiClient.java index 017af43..16cff30 100644 --- a/src/main/java/io/falu/client/AbstractHttpApiClient.java +++ b/src/main/java/io/falu/client/AbstractHttpApiClient.java @@ -76,16 +76,17 @@ protected ResourceResponse execute(Request.Builder builder, C if (classOfTResult != null) { result = objectMapper.readValue(body.charStream(), classOfTResult); } + break; } case 400: { error = objectMapper.readValue(body.charStream(), HttpResponseProblem.class); + break; } } - - // close the body stream - body.close(); } + response.close(); + return (ResourceResponse) ResourceResponse.builder() .statusCode(code) .headers(response.headers()) diff --git a/src/main/java/io/falu/networking/FaluApiClient.java b/src/main/java/io/falu/networking/FaluApiClient.java index 1b49a87..d851610 100644 --- a/src/main/java/io/falu/networking/FaluApiClient.java +++ b/src/main/java/io/falu/networking/FaluApiClient.java @@ -59,6 +59,7 @@ import java.io.IOException; import java.util.List; +import java.util.UUID; public class FaluApiClient extends AbstractHttpApiClient { private static final String HOST = "api.falu.io"; @@ -75,20 +76,27 @@ public FaluApiClient(FaluClientOptions options, AppDetailsInterceptor intercepto } private static Request.Builder buildRequest(RequestOptions options) { + return buildRequest(options, null); + } + + private static Request.Builder buildRequest(RequestOptions options, String method) { Request.Builder builder = new Request.Builder(); if (options.workspace != null && !options.workspace.isEmpty()) { builder.header("X-Workspace-Id", options.workspace); } + // Check if idempotencyKey is not null and not empty if (options.idempotencyKey != null && !options.idempotencyKey.isEmpty()) { builder.header("X-Idempotency-Key", options.idempotencyKey); + } else if ("PATCH".equalsIgnoreCase(method) || "POST".equalsIgnoreCase(method)) { + // Generate a new UUID for idempotency key if method is PATCH or POST + builder.header("X-Idempotency-Key", UUID.randomUUID().toString()); } boolean live = options.live != null && options.live; builder.header("X-Live-Mode", String.valueOf(live)); - return builder; } @@ -145,7 +153,7 @@ public ResourceResponse updatePayment(String paymentId, PaymentUpdateOp public ResourceResponse createPayment(PaymentCreateOptions request, RequestOptions options) throws IOException { HttpUrl url = buildUrl("v1/payments/", null); - Request.Builder builder = buildRequest(options) + Request.Builder builder = buildRequest(options, "POST") .url(url) .post(RequestBody.create(makeJson(request), MEDIA_TYPE_JSON)); @@ -177,7 +185,7 @@ public ResourceResponse updatePaymentAuthorization(String HttpUrl url = buildUrl("v1/payment_authorizations/" + authorizationId, null); - Request.Builder builder = buildRequest(options) + Request.Builder builder = buildRequest(options, "PATCH") .url(url) .patch(RequestBody.create(makeJson(updateOptions), MEDIA_TYPE_MERGE_PATCH_JSON)); @@ -187,7 +195,7 @@ public ResourceResponse updatePaymentAuthorization(String public ResourceResponse approvePaymentAuthorization(String authorizationId, RequestOptions options) throws IOException { HttpUrl url = buildUrl("v1/payment_authorizations/" + authorizationId + "/approve", null); - Request.Builder builder = buildRequest(options) + Request.Builder builder = buildRequest(options, "POST") .url(url) .post(RequestBody.create(makeJson(null), MEDIA_TYPE_JSON)); return execute(builder, PaymentAuthorization.class); @@ -196,7 +204,7 @@ public ResourceResponse approvePaymentAuthorization(String public ResourceResponse declinePaymentAuthorization(String authorizationId, RequestOptions options) throws IOException { HttpUrl url = buildUrl("v1/payment_authorizations/" + authorizationId + "/decline", null); - Request.Builder builder = buildRequest(options) + Request.Builder builder = buildRequest(options, "POST") .url(url) .post(RequestBody.create(makeJson(null), MEDIA_TYPE_JSON)); return execute(builder, PaymentAuthorization.class); @@ -215,7 +223,7 @@ public ResourceResponse getPaymentRefunds(PaymentRefundsListOpt public ResourceResponse createPaymentRefund(PaymentRefundCreateOptions request, RequestOptions options) throws IOException { HttpUrl url = buildUrl("v1/payment_refunds/", null); - Request.Builder builder = buildRequest(options) + Request.Builder builder = buildRequest(options, "POST") .url(url) .post(RequestBody.create(makeJson(request), MEDIA_TYPE_JSON)); @@ -238,7 +246,7 @@ public ResourceResponse updatePaymentRefund(String refundId, HttpUrl url = buildUrl("v1/payment_refunds/" + refundId, null); - Request.Builder builder = buildRequest(options) + Request.Builder builder = buildRequest(options, "PATCH") .url(url) .patch(RequestBody.create(makeJson(updateOptions), MEDIA_TYPE_JSON)); @@ -259,7 +267,7 @@ public ResourceResponse getMessages(MessagesListOptions listOptions, public ResourceResponse createMessage(MessageCreateOptions request, RequestOptions options) throws IOException { HttpUrl url = buildUrl("v1/messages", null); - Request.Builder builder = buildRequest(options) + Request.Builder builder = buildRequest(options, "POST") .url(url) .post(RequestBody.create(makeJson(request), MEDIA_TYPE_JSON)); @@ -279,7 +287,7 @@ public ResourceResponse getMessage(String messageId, RequestOptions opt public ResourceResponse updateMessage(String messageId, MessageUpdateOptions updateOptions, RequestOptions options) throws IOException { HttpUrl url = buildUrl("v1/messages/" + messageId, null); - Request.Builder builder = buildRequest(options) + Request.Builder builder = buildRequest(options, "PATCH") .url(url) .patch(RequestBody.create(makeJson(updateOptions), MEDIA_TYPE_JSON)); @@ -289,7 +297,7 @@ public ResourceResponse updateMessage(String messageId, MessageUpdateOp public ResourceResponse sendBulkMessages(List messages, RequestOptions options) throws IOException { HttpUrl url = buildUrl("v1/messages/batch", null); - Request.Builder builder = buildRequest(options) + Request.Builder builder = buildRequest(options, "POST") .url(url) .post(RequestBody.create(makeJson(messages), MEDIA_TYPE_JSON)); @@ -309,7 +317,7 @@ public ResourceResponse getMessageTemplates(MessageTemplatesL public ResourceResponse createMessageTemplate(MessageTemplateCreateOptions request, RequestOptions options) throws IOException { HttpUrl url = buildUrl("v1/message_templates", null); - Request.Builder builder = buildRequest(options) + Request.Builder builder = buildRequest(options, "POST") .url(url) .post(RequestBody.create(makeJson(request), MEDIA_TYPE_JSON)); @@ -329,7 +337,7 @@ public ResourceResponse getMessageTemplate(String templateId, R public ResourceResponse updateMessageTemplate(String templateId, MessageTemplateUpdateOptions patchModel, RequestOptions options) throws IOException { HttpUrl url = buildUrl("v1/message_templates/" + templateId, null); - Request.Builder builder = buildRequest(options) + Request.Builder builder = buildRequest(options, "PATCH") .url(url) .patch(RequestBody.create(makeJson(patchModel), MEDIA_TYPE_JSON)); @@ -349,7 +357,7 @@ public ResourceResponse deleteMessageTemplate(String templateId, RequestOptio public ResourceResponse validateMessageTemplate(MessageTemplateValidationRequest request, RequestOptions options) throws IOException { HttpUrl url = buildUrl("v1/message_templates/validate", null); - Request.Builder builder = buildRequest(options) + Request.Builder builder = buildRequest(options, "POST") .url(url) .post(RequestBody.create(makeJson(request), MEDIA_TYPE_JSON)); @@ -369,7 +377,7 @@ public ResourceResponse getMessageStreams(MessageStreamsListOpt public ResourceResponse createMessageStream(MessageStreamCreateOptions request, RequestOptions options) throws IOException { HttpUrl url = buildUrl("v1/message_streams", null); - Request.Builder builder = buildRequest(options) + Request.Builder builder = buildRequest(options, "POST") .url(url) .post(RequestBody.create(makeJson(request), MEDIA_TYPE_JSON)); @@ -389,7 +397,7 @@ public ResourceResponse getMessageStream(String streamId, Request public ResourceResponse updateMessageStream(String streamId, MessageStreamUpdateOptions updateOptions, RequestOptions options) throws IOException { HttpUrl url = buildUrl("v1/message_streams/" + streamId, null); - Request.Builder builder = buildRequest(options) + Request.Builder builder = buildRequest(options, "PATCH") .url(url) .patch(RequestBody.create(makeJson(updateOptions), MEDIA_TYPE_JSON)); @@ -410,7 +418,7 @@ public ResourceResponse deleteMessageStream(String streamId, RequestOptions o public ResourceResponse archiveMessageStream(String streamId, RequestOptions options) throws IOException { HttpUrl url = buildUrl("v1/message_streams/" + streamId + "/archive", null); - Request.Builder builder = buildRequest(options) + Request.Builder builder = buildRequest(options, "POST") .url(url) .post(RequestBody.create(makeJson(null), MEDIA_TYPE_JSON)); @@ -420,7 +428,7 @@ public ResourceResponse archiveMessageStream(String streamId, Req public ResourceResponse unarchiveMessageStream(String streamId, RequestOptions options) throws IOException { HttpUrl url = buildUrl("v1/message_streams/" + streamId + "/unarchive", null); - Request.Builder builder = buildRequest(options) + Request.Builder builder = buildRequest(options, "POST") .url(url) .post(RequestBody.create(makeJson(null), MEDIA_TYPE_JSON)); @@ -442,7 +450,7 @@ public ResourceResponse getMoneyBalances(RequestOptions options) t public ResourceResponse refreshMoneyBalances(RequestOptions options) throws IOException { HttpUrl url = buildUrl("v1/money_balances/refresh", null); - Request.Builder builder = buildRequest(options) + Request.Builder builder = buildRequest(options, "POST") .url(url) .post(RequestBody.create(makeJson(null), MEDIA_TYPE_JSON)); @@ -463,7 +471,7 @@ public ResourceResponse getTransfers(TransferListOptions listOptions public ResourceResponse createTransfer(TransferCreateOptions request, RequestOptions options) throws IOException { HttpUrl url = buildUrl("v1/transfers", null); - Request.Builder builder = buildRequest(options) + Request.Builder builder = buildRequest(options, "POST") .url(url) .post(RequestBody.create(makeJson(request), MEDIA_TYPE_JSON)); @@ -485,7 +493,7 @@ public ResourceResponse updateTransfer(String transferId, TransferUpda HttpUrl url = buildUrl("v1/transfers" + transferId, null); - Request.Builder builder = buildRequest(options) + Request.Builder builder = buildRequest(options, "PATCH") .url(url) .patch(RequestBody.create(makeJson(updateOptions), MEDIA_TYPE_JSON)); @@ -505,7 +513,7 @@ public ResourceResponse getTransferReversals(TransferReversa public ResourceResponse createTransferReversal(TransferReversalCreateOptions request, RequestOptions options) throws IOException { HttpUrl url = buildUrl("v1/transfer_reversals", null); - Request.Builder builder = buildRequest(options) + Request.Builder builder = buildRequest(options, "POST") .url(url) .post(RequestBody.create(makeJson(request), MEDIA_TYPE_JSON)); @@ -513,12 +521,11 @@ public ResourceResponse createTransferReversal(TransferReversa } //endregion - public ResourceResponse updateTransferReversal(String transferId, - TransferReversalUpdateOptions updateOptions, RequestOptions options) throws IOException { + public ResourceResponse updateTransferReversal(String transferId, TransferReversalUpdateOptions updateOptions, RequestOptions options) throws IOException { HttpUrl url = buildUrl("v1/transfer_reversals/" + transferId, null); - Request.Builder builder = buildRequest(options) + Request.Builder builder = buildRequest(options, "POST") .url(url) .post(RequestBody.create(makeJson(updateOptions), MEDIA_TYPE_JSON)); @@ -572,7 +579,7 @@ public ResourceResponse uploadFile(FileCreateOptions request, RequestOptio RequestBody requestBody = bodyBuilder.build(); - Request.Builder builder = buildRequest(requestOptions) + Request.Builder builder = buildRequest(requestOptions, "POST") .url(url) .post(requestBody); @@ -592,7 +599,7 @@ public ResourceResponse getFileLinks(FileLinksListOptions listOption public ResourceResponse createFileLink(FileLinkCreateOptions request, RequestOptions requestOptions) throws IOException { HttpUrl url = buildUrl("v1/file_links", null); - Request.Builder builder = buildRequest(requestOptions) + Request.Builder builder = buildRequest(requestOptions, "POST") .url(url) .post(RequestBody.create(makeJson(request), MEDIA_TYPE_JSON)); @@ -612,7 +619,7 @@ public ResourceResponse getFileLink(String linkId, RequestOptions requ public ResourceResponse updateFileLink(String linkId, FileLinkUpdateOptions updateOptions, RequestOptions requestOptions) throws IOException { HttpUrl url = buildUrl("v1/file_links/" + linkId, null); - Request.Builder builder = buildRequest(requestOptions) + Request.Builder builder = buildRequest(requestOptions, "PATCH") .url(url) .patch(RequestBody.create(makeJson(updateOptions), MEDIA_TYPE_MERGE_PATCH_JSON)); @@ -632,7 +639,7 @@ public ResourceResponse getWebhookEndpoints(WebhookEndpointLi public ResourceResponse createWebhookEndpoint(WebhookEndpointCreateRequest request, RequestOptions requestOptions) throws IOException { HttpUrl url = buildUrl("v1/webhooks/endpoints", null); - Request.Builder builder = buildRequest(requestOptions) + Request.Builder builder = buildRequest(requestOptions, "PATCH") .url(url) .post(RequestBody.create(makeJson(request), MEDIA_TYPE_JSON)); @@ -654,7 +661,7 @@ public ResourceResponse updateWebhookEndpoint(String endpointId HttpUrl url = buildUrl("v1/webhooks/endpoints/" + endpointId, null); - Request.Builder builder = buildRequest(requestOptions) + Request.Builder builder = buildRequest(requestOptions, "PATCH") .url(url) .patch(RequestBody.create(makeJson(updateOptions), MEDIA_TYPE_JSON)); return execute(builder, WebhookEndpoint.class); @@ -705,7 +712,7 @@ public ResourceResponse getIdentityVerifications(Identit public ResourceResponse createIdentityVerification(IdentityVerificationCreateOptions request, RequestOptions requestOptions) throws IOException { HttpUrl url = buildUrl("v1/identity/verifications", null); - Request.Builder builder = buildRequest(requestOptions) + Request.Builder builder = buildRequest(requestOptions, "POST") .url(url) .post(RequestBody.create(makeJson(request), MEDIA_TYPE_JSON)); @@ -726,7 +733,7 @@ public ResourceResponse updateIdentityVerification(String IdentityVerificationUpdateOptions updateOptions, RequestOptions requestOptions) throws IOException { HttpUrl url = buildUrl("v1/identity/verifications/" + id, null); - Request.Builder builder = buildRequest(requestOptions) + Request.Builder builder = buildRequest(requestOptions, "PATCH") .url(url) .patch(RequestBody.create(makeJson(updateOptions), MEDIA_TYPE_JSON)); @@ -747,7 +754,7 @@ public ResourceResponse cancelIdentityVerification(String public ResourceResponse redactIdentityVerification(String id, RequestOptions requestOptions) throws IOException { HttpUrl url = buildUrl("v1/identity/verifications/" + id + "/redact", null); - Request.Builder builder = buildRequest(requestOptions) + Request.Builder builder = buildRequest(requestOptions, "POST") .url(url) .post(RequestBody.create(makeJson(null), MEDIA_TYPE_JSON)); @@ -781,7 +788,7 @@ public ResourceResponse getIdentityVerificationRepor public ResourceResponse createTemporaryKey(TemporaryKeyCreateOptions request, RequestOptions requestOptions) throws IOException { HttpUrl url = buildUrl("v1/temporary_keys", null); - Request.Builder builder = buildRequest(requestOptions) + Request.Builder builder = buildRequest(requestOptions, "POST") .url(url) .post(RequestBody.create(makeJson(request), MEDIA_TYPE_JSON)); return execute(builder, TemporaryKey.class);