Skip to content
This repository has been archived by the owner on Dec 25, 2024. It is now read-only.

Commit

Permalink
Generate a new UUID for idempotency key if method is PATCH or POST (#121
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sethonyango authored Aug 27, 2024
1 parent 1aa759d commit 234788f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
7 changes: 4 additions & 3 deletions src/main/java/io/falu/client/AbstractHttpApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,17 @@ protected <TResult> ResourceResponse<TResult> 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<TResult>) ResourceResponse.builder()
.statusCode(code)
.headers(response.headers())
Expand Down
73 changes: 40 additions & 33 deletions src/main/java/io/falu/networking/FaluApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
}

Expand Down Expand Up @@ -145,7 +153,7 @@ public ResourceResponse<Payment> updatePayment(String paymentId, PaymentUpdateOp
public ResourceResponse<Payment> 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));

Expand Down Expand Up @@ -177,7 +185,7 @@ public ResourceResponse<PaymentAuthorization> 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));

Expand All @@ -187,7 +195,7 @@ public ResourceResponse<PaymentAuthorization> updatePaymentAuthorization(String
public ResourceResponse<PaymentAuthorization> 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);
Expand All @@ -196,7 +204,7 @@ public ResourceResponse<PaymentAuthorization> approvePaymentAuthorization(String
public ResourceResponse<PaymentAuthorization> 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);
Expand All @@ -215,7 +223,7 @@ public ResourceResponse<PaymentRefund[]> getPaymentRefunds(PaymentRefundsListOpt
public ResourceResponse<PaymentRefund> 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));

Expand All @@ -238,7 +246,7 @@ public ResourceResponse<PaymentRefund> 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));

Expand All @@ -259,7 +267,7 @@ public ResourceResponse<Message[]> getMessages(MessagesListOptions listOptions,
public ResourceResponse<MessageResponse> 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));

Expand All @@ -279,7 +287,7 @@ public ResourceResponse<Message> getMessage(String messageId, RequestOptions opt
public ResourceResponse<Message> 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));

Expand All @@ -289,7 +297,7 @@ public ResourceResponse<Message> updateMessage(String messageId, MessageUpdateOp
public ResourceResponse<MessageResponse> sendBulkMessages(List<MessageCreateOptions> 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));

Expand All @@ -309,7 +317,7 @@ public ResourceResponse<MessageTemplate[]> getMessageTemplates(MessageTemplatesL
public ResourceResponse<MessageTemplate> 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));

Expand All @@ -329,7 +337,7 @@ public ResourceResponse<MessageTemplate> getMessageTemplate(String templateId, R
public ResourceResponse<MessageTemplate> 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));

Expand All @@ -349,7 +357,7 @@ public ResourceResponse<?> deleteMessageTemplate(String templateId, RequestOptio
public ResourceResponse<MessageTemplateValidationResponse> 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));

Expand All @@ -369,7 +377,7 @@ public ResourceResponse<MessageStream[]> getMessageStreams(MessageStreamsListOpt
public ResourceResponse<MessageStream> 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));

Expand All @@ -389,7 +397,7 @@ public ResourceResponse<MessageStream> getMessageStream(String streamId, Request
public ResourceResponse<MessageStream> 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));

Expand All @@ -410,7 +418,7 @@ public ResourceResponse<?> deleteMessageStream(String streamId, RequestOptions o
public ResourceResponse<MessageStream> 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));

Expand All @@ -420,7 +428,7 @@ public ResourceResponse<MessageStream> archiveMessageStream(String streamId, Req
public ResourceResponse<MessageStream> 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));

Expand All @@ -442,7 +450,7 @@ public ResourceResponse<MoneyBalance> getMoneyBalances(RequestOptions options) t
public ResourceResponse<MoneyBalance> 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));

Expand All @@ -463,7 +471,7 @@ public ResourceResponse<Transfer[]> getTransfers(TransferListOptions listOptions
public ResourceResponse<Transfer> 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));

Expand All @@ -485,7 +493,7 @@ public ResourceResponse<Transfer> 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));

Expand All @@ -505,20 +513,19 @@ public ResourceResponse<TransferReversal[]> getTransferReversals(TransferReversa
public ResourceResponse<TransferReversal> 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));

return execute(builder, TransferReversal.class);
}
//endregion

public ResourceResponse<TransferReversal> updateTransferReversal(String transferId,
TransferReversalUpdateOptions updateOptions, RequestOptions options) throws IOException {
public ResourceResponse<TransferReversal> 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));

Expand Down Expand Up @@ -572,7 +579,7 @@ public ResourceResponse<File> uploadFile(FileCreateOptions request, RequestOptio

RequestBody requestBody = bodyBuilder.build();

Request.Builder builder = buildRequest(requestOptions)
Request.Builder builder = buildRequest(requestOptions, "POST")
.url(url)
.post(requestBody);

Expand All @@ -592,7 +599,7 @@ public ResourceResponse<FileLink[]> getFileLinks(FileLinksListOptions listOption
public ResourceResponse<FileLink> 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));

Expand All @@ -612,7 +619,7 @@ public ResourceResponse<FileLink> getFileLink(String linkId, RequestOptions requ
public ResourceResponse<FileLink> 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));

Expand All @@ -632,7 +639,7 @@ public ResourceResponse<WebhookEndpoint[]> getWebhookEndpoints(WebhookEndpointLi

public ResourceResponse<WebhookEndpoint> 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));

Expand All @@ -654,7 +661,7 @@ public ResourceResponse<WebhookEndpoint> 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);
Expand Down Expand Up @@ -705,7 +712,7 @@ public ResourceResponse<IdentityVerification[]> getIdentityVerifications(Identit
public ResourceResponse<IdentityVerification> 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));

Expand All @@ -726,7 +733,7 @@ public ResourceResponse<IdentityVerification> 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));

Expand All @@ -747,7 +754,7 @@ public ResourceResponse<IdentityVerification> cancelIdentityVerification(String
public ResourceResponse<IdentityVerification> 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));

Expand Down Expand Up @@ -781,7 +788,7 @@ public ResourceResponse<IdentityVerificationReport> getIdentityVerificationRepor
public ResourceResponse<TemporaryKey> 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);
Expand Down

0 comments on commit 234788f

Please sign in to comment.