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

Commit

Permalink
Migrate from json-patch to merge-patch (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethonyango authored Jun 24, 2024
1 parent e54eb9e commit 48afa72
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 83 deletions.
9 changes: 8 additions & 1 deletion src/main/java/io/falu/client/AbstractHttpApiClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.falu.client;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.falu.client.headers.IAuthenticationProvider;
import io.falu.networking.AppDetailsInterceptor;
import okhttp3.*;
Expand All @@ -18,10 +19,11 @@ public class AbstractHttpApiClient {
protected static final MediaType MEDIA_TYPE_JSON = MediaType.get("application/json; charset=utf-8");
protected static final MediaType MEDIA_TYPE_TEXT_JSON = MediaType.get("text/json");
protected static final MediaType MEDIA_TYPE_PATH_JSON = MediaType.get("application/json-path+json");
protected static final MediaType MEDIA_TYPE_MERGE_PATCH_JSON = MediaType.get("application/merge-patch+json");
protected static final MediaType MEDIA_TYPE_PLUS_JSON = MediaType.get("application/*+json");

private final OkHttpClient backChannel;
private final Gson gson = new Gson();
private Gson gson = new Gson();

/**
* Creates an instance of @[AbstractHttpApiClient]
Expand Down Expand Up @@ -84,4 +86,9 @@ protected <TResult> ResourceResponse<TResult> execute(Request.Builder builder, C
protected String makeJson(@Nullable Object o) {
return gson.toJson(o);
}

protected String makeJson(@Nullable Object o, GsonBuilder builder) {
gson = builder.create();
return gson.toJson(o);
}
}
112 changes: 58 additions & 54 deletions src/main/java/io/falu/networking/FaluApiClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.falu.networking;

import com.google.gson.GsonBuilder;
import com.google.gson.internal.bind.util.ISO8601Utils;
import io.falu.FaluClientOptions;
import io.falu.client.AbstractHttpApiClient;
Expand Down Expand Up @@ -71,6 +72,39 @@ public FaluApiClient(FaluClientOptions options, AppDetailsInterceptor intercepto
super(new FaluAuthenticationHeaderProvider(options.getApiKey()), interceptor, enableLogging);
}

private static Request.Builder buildRequest(RequestOptions options) {
Request.Builder builder = new Request.Builder();

if (options.workspace != null && !options.workspace.isEmpty()) {
builder.header("X-Workspace-Id", options.workspace);
}

if (options.idempotencyKey != null && !options.idempotencyKey.isEmpty()) {
builder.header("X-Idempotency-Key", options.idempotencyKey);
}

boolean live = options.live != null && options.live;
builder.header("X-Live-Mode", String.valueOf(live));


return builder;
}

private static HttpUrl buildUrl(String path, @Nullable BasicListOptions listOptions) {
HttpUrl.Builder builder = new HttpUrl.Builder();
builder.scheme(SCHEME);
builder.host(HOST);
builder.addPathSegments(path);

if (listOptions != null) {
QueryValues args = new QueryValues();
listOptions.populate(args);
args.getQueryParameters(builder);
}

return builder.build();
}

//region Payments
public ResourceResponse<Payment[]> getPayments(PaymentsListOptions listOptions, RequestOptions requestOptions) throws IOException {
HttpUrl url = buildUrl("v1/payments", listOptions);
Expand All @@ -92,12 +126,12 @@ public ResourceResponse<Payment> getPayment(String paymentId, RequestOptions opt
return execute(builder, Payment.class);
}

public ResourceResponse<Payment> updatePayment(String paymentId, JsonPatchDocument<PaymentPatchModel> document, RequestOptions options) throws IOException {
public ResourceResponse<Payment> updatePayment(String paymentId, PaymentPatchModel patchModel, RequestOptions options) throws IOException {
HttpUrl url = buildUrl("v1/payments/" + paymentId, null);

Request.Builder builder = buildRequest(options)
.url(url)
.patch(RequestBody.create(makeJson(document.getOperations()), MEDIA_TYPE_JSON));
.patch(RequestBody.create(makeJson(patchModel, new GsonBuilder().serializeNulls()), MEDIA_TYPE_JSON));

return execute(builder, Payment.class);
}
Expand Down Expand Up @@ -177,6 +211,7 @@ public ResourceResponse<PaymentRefund> createPaymentRefund(PaymentRefundRequest

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

public ResourceResponse<PaymentRefund> getPaymentRefund(String refundId, RequestOptions options) throws IOException {
HttpUrl url = buildUrl("v1/payment_refunds/" + refundId, null);
Expand All @@ -195,7 +230,6 @@ public ResourceResponse<PaymentRefund> updatePaymentRefund(String refundId, Json
.patch(RequestBody.create(makeJson(document.getOperations()), MEDIA_TYPE_JSON));
return execute(builder, PaymentRefund.class);
}
//endregion

//region Messages, Message Templates, and Message Streams
public ResourceResponse<Message[]> getMessages(MessagesListOptions listOptions, RequestOptions options) throws IOException {
Expand Down Expand Up @@ -271,12 +305,12 @@ public ResourceResponse<MessageTemplate> getMessageTemplate(String templateId, R
return execute(builder, MessageTemplate.class);
}

public ResourceResponse<MessageTemplate> updateMessageTemplate(String templateId, JsonPatchDocument<MessageTemplatePatchModel> document, RequestOptions options) throws IOException {
public ResourceResponse<MessageTemplate> updateMessageTemplate(String templateId, MessageTemplatePatchModel patchModel, RequestOptions options) throws IOException {
HttpUrl url = buildUrl("v1/message_templates/" + templateId, null);

Request.Builder builder = buildRequest(options)
.url(url)
.patch(RequestBody.create(makeJson(document.getOperations()), MEDIA_TYPE_JSON));
.patch(RequestBody.create(makeJson(patchModel, new GsonBuilder().serializeNulls()), MEDIA_TYPE_JSON));
return execute(builder, MessageTemplate.class);
}

Expand Down Expand Up @@ -343,6 +377,7 @@ public ResourceResponse<?> deleteMessageStream(String streamId, RequestOptions o
.delete();
return execute(builder, ResourceResponse.class);
}
//endregion

public ResourceResponse<MessageStream> archiveMessageStream(String streamId, RequestOptions options) throws IOException {
HttpUrl url = buildUrl("v1/message_streams/" + streamId + "/archive", null);
Expand Down Expand Up @@ -381,7 +416,6 @@ public ResourceResponse<MoneyBalance> refreshMoneyBalances(RequestOptions option
.post(RequestBody.create(makeJson(null), MEDIA_TYPE_JSON));
return execute(builder, MoneyBalance.class);
}
//endregion

//region Transfers, Transfer Reversals
public ResourceResponse<Transfer[]> getTransfers(TransferListOptions listOptions, RequestOptions requestOptions) throws IOException {
Expand Down Expand Up @@ -437,13 +471,16 @@ public ResourceResponse<TransferReversal> createTransferReversal(TransferReversa
.post(RequestBody.create(makeJson(request), MEDIA_TYPE_JSON));
return execute(builder, TransferReversal.class);
}
//endregion

public ResourceResponse<TransferReversal> updateTransferReversal(String transferId,
TransferReversalPatchModel patchModel, RequestOptions options) throws IOException {

public ResourceResponse<TransferReversal> updateTransferReversal(String transferId, JsonPatchDocument<TransferReversalPatchModel> document, RequestOptions options) throws IOException {
HttpUrl url = buildUrl("v1/transfer_reversals/" + transferId, null);

Request.Builder builder = buildRequest(options)
.url(url)
.post(RequestBody.create(makeJson(document.getOperations()), MEDIA_TYPE_JSON));
.post(RequestBody.create(makeJson(patchModel, new GsonBuilder().serializeNulls()), MEDIA_TYPE_JSON));

return execute(builder, TransferReversal.class);
}
Expand All @@ -456,8 +493,6 @@ public ResourceResponse<TransferReversal> getTransferReversal(String transferId,
.get();
return execute(builder, TransferReversal.class);
}
//endregion


//region Files and File Links
public ResourceResponse<File[]> getFiles(FileListOptions listOptions, RequestOptions requestOptions) throws IOException {
Expand Down Expand Up @@ -517,6 +552,7 @@ public ResourceResponse<FileLink> createFileLink(FileLinkCreateRequest request,
.post(RequestBody.create(makeJson(request), MEDIA_TYPE_JSON));
return execute(builder, FileLink.class);
}
//endregion

public ResourceResponse<FileLink> getFileLink(String linkId, RequestOptions requestOptions) throws IOException {
HttpUrl url = buildUrl("v1/file_links/" + linkId, null);
Expand All @@ -527,15 +563,14 @@ public ResourceResponse<FileLink> getFileLink(String linkId, RequestOptions requ
return execute(builder, FileLink.class);
}

public ResourceResponse<FileLink> updateFileLink(String linkId, JsonPatchDocument<FileLinkPatchModel> document, RequestOptions requestOptions) throws IOException {
public ResourceResponse<FileLink> updateFileLink(String linkId, FileLinkPatchModel patchModel, RequestOptions requestOptions) throws IOException {
HttpUrl url = buildUrl("v1/file_links/" + linkId, null);

Request.Builder builder = buildRequest(requestOptions)
.url(url)
.patch(RequestBody.create(makeJson(document.getOperations()), MEDIA_TYPE_JSON));
.patch(RequestBody.create(makeJson(patchModel, new GsonBuilder().serializeNulls()), MEDIA_TYPE_MERGE_PATCH_JSON));
return execute(builder, FileLink.class);
}
//endregion

//region Webhook Endpoints, Events
public ResourceResponse<WebhookEndpoint[]> getWebhookEndpoints(WebhookEndpointListOptions listOptions, RequestOptions requestOptions) throws IOException {
Expand Down Expand Up @@ -564,12 +599,14 @@ public ResourceResponse<WebhookEndpoint> getWebhookEndpoint(String endpointId, R
return execute(builder, WebhookEndpoint.class);
}

public ResourceResponse<WebhookEndpoint> updateWebhookEndpoint(String endpointId, JsonPatchDocument<WebhookEndpointPatchModel> document, RequestOptions requestOptions) throws IOException {
public ResourceResponse<WebhookEndpoint> updateWebhookEndpoint(String endpointId,
WebhookEndpointPatchModel patchModel, RequestOptions requestOptions) throws IOException {

HttpUrl url = buildUrl("v1/webhooks/endpoints/" + endpointId, null);

Request.Builder builder = buildRequest(requestOptions)
.url(url)
.patch(RequestBody.create(makeJson(document.getOperations()), MEDIA_TYPE_JSON));
.patch(RequestBody.create(makeJson(patchModel, new GsonBuilder().serializeNulls()), MEDIA_TYPE_JSON));
return execute(builder, WebhookEndpoint.class);
}

Expand All @@ -581,6 +618,7 @@ public ResourceResponse<?> deleteWebhookEndpoint(String endpointId, RequestOptio
.delete();
return execute(builder, ResourceResponse.class);
}
//endregion

public ResourceResponse<WebhookEvent[]> getWebhookEvents(EventListOptions listOptions, RequestOptions requestOptions) throws IOException {
HttpUrl url = buildUrl("v1/events", listOptions);
Expand All @@ -600,7 +638,6 @@ public ResourceResponse<WebhookEvent> getWebhookEvent(String eventId, RequestOpt

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

//region IdentityVerification
public ResourceResponse<IdentityVerification[]> getIdentityVerifications(IdentityVerificationListOptions listOptions, RequestOptions requestOptions) throws IOException {
Expand Down Expand Up @@ -631,14 +668,15 @@ public ResourceResponse<IdentityVerification> getIdentityVerification(String id,
}

public ResourceResponse<IdentityVerification> updateIdentityVerification(String id,
JsonPatchDocument<IdentityVerificationPatchModel> document, RequestOptions requestOptions) throws IOException {
IdentityVerificationPatchModel patchModel, RequestOptions requestOptions) throws IOException {
HttpUrl url = buildUrl("v1/identity/verifications/" + id, null);

Request.Builder builder = buildRequest(requestOptions)
.url(url)
.patch(RequestBody.create(makeJson(document.getOperations()), MEDIA_TYPE_JSON));
.patch(RequestBody.create(makeJson(patchModel, new GsonBuilder().serializeNulls()), MEDIA_TYPE_JSON));
return execute(builder, IdentityVerification.class);
}
//endregion

public ResourceResponse<IdentityVerification> cancelIdentityVerification(String id, RequestOptions requestOptions) throws IOException {
HttpUrl url = buildUrl("v1/identity/verifications/" + id + "/cancel", null);
Expand All @@ -657,7 +695,7 @@ public ResourceResponse<IdentityVerification> redactIdentityVerification(String
.post(RequestBody.create(makeJson(null), MEDIA_TYPE_JSON));
return execute(builder, IdentityVerification.class);
}
//endregion
//endRegion

//region IdentityVerificationReports
public ResourceResponse<IdentityVerificationReport[]> getIdentityVerificationReports(IdentityVerificationReportsListOptions listOptions, RequestOptions requestOptions) throws IOException {
Expand All @@ -677,7 +715,7 @@ public ResourceResponse<IdentityVerificationReport> getIdentityVerificationRepor
.get();
return execute(builder, IdentityVerificationReport.class);
}
//endRegion
//endregion

//region TemporaryKeys
public ResourceResponse<TemporaryKey> createTemporaryKey(TemporaryKeyCreateRequest request, RequestOptions requestOptions) throws IOException {
Expand All @@ -697,38 +735,4 @@ public ResourceResponse<?> deleteTemporaryKey(String keyId, RequestOptions reque
.delete();
return execute(builder, ResourceResponse.class);
}
//endregion

private static Request.Builder buildRequest(RequestOptions options) {
Request.Builder builder = new Request.Builder();

if (options.workspace != null && !options.workspace.isEmpty()) {
builder.header("X-Workspace-Id", options.workspace);
}

if (options.idempotencyKey != null && !options.idempotencyKey.isEmpty()) {
builder.header("X-Idempotency-Key", options.idempotencyKey);
}

boolean live = options.live != null && options.live;
builder.header("X-Live-Mode", String.valueOf(live));


return builder;
}

private static HttpUrl buildUrl(String path, @Nullable BasicListOptions listOptions) {
HttpUrl.Builder builder = new HttpUrl.Builder();
builder.scheme(SCHEME);
builder.host(HOST);
builder.addPathSegments(path);

if (listOptions != null) {
QueryValues args = new QueryValues();
listOptions.populate(args);
args.getQueryParameters(builder);
}

return builder.build();
}
}
2 changes: 1 addition & 1 deletion src/main/java/io/falu/services/FilesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public ResourceResponse<FileLink> getFileLink(@NotNull String linkId, @Nullable
* @param linkId Unique identifier of the file.
* @param requestOptions Additional info to add to the request.
*/
public ResourceResponse<FileLink> updateFileLink(@NotNull String linkId, @NotNull JsonPatchDocument<FileLinkPatchModel> patch, RequestOptions requestOptions) throws IOException {
public ResourceResponse<FileLink> updateFileLink(@NotNull String linkId, @NotNull FileLinkPatchModel patch, RequestOptions requestOptions) throws IOException {
return getApiClient().updateFileLink(linkId, patch, requestOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public ResourceResponse<IdentityVerification> getIdentityVerification(@NotNull S
* Update an identity verification
*/
public ResourceResponse<IdentityVerification> updateIdentityVerification(
@NotNull String id, @NotNull JsonPatchDocument<IdentityVerificationPatchModel> patch, @Nullable RequestOptions options) throws IOException {
return getApiClient().updateIdentityVerification(id, patch, options);
@NotNull String id, @NotNull IdentityVerificationPatchModel patchModel, @Nullable RequestOptions options) throws IOException {
return getApiClient().updateIdentityVerification(id, patchModel, options);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/falu/services/MessagesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ public ResourceResponse<MessageTemplate> getMessageTemplate(@NotNull String temp
* @param patch the patch document.
* @param requestOptions additional info to add to the request.
*/
public ResourceResponse<MessageTemplate> updateMessageTemplate(@NotNull String templateId, @NotNull JsonPatchDocument<MessageTemplatePatchModel> patch, @NonNull RequestOptions requestOptions) throws IOException {
return getApiClient().updateMessageTemplate(templateId, patch, requestOptions);
public ResourceResponse<MessageTemplate> updateMessageTemplate(@NotNull String templateId, @NotNull MessageTemplatePatchModel patchModel, @NonNull RequestOptions requestOptions) throws IOException {
return getApiClient().updateMessageTemplate(templateId, patchModel, requestOptions);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/io/falu/services/PaymentsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ public ResourceResponse<Payment> createPayment(@NotNull PaymentCreateRequest req
* @param patch the patch document.
* @param options additional info to add to the request.
*/
public ResourceResponse<Payment> updatePayment(@NotNull String paymentId, @NotNull JsonPatchDocument<PaymentPatchModel> patch, @Nullable RequestOptions options) throws IOException {
return getApiClient().updatePayment(paymentId, patch, options);
public ResourceResponse<Payment> updatePayment(@NotNull String paymentId, @NotNull PaymentPatchModel patchModel,
@Nullable RequestOptions options) throws IOException {

return getApiClient().updatePayment(paymentId, patchModel, options);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/io/falu/services/TransfersService.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ public ResourceResponse<TransferReversal> getTransferReversal(@NotNull String re
* @param patch the patch document.
* @param requestOptions additional info to add to the request.
*/
public ResourceResponse<TransferReversal> updateTransferReversal(@NotNull String reversalId, @NotNull JsonPatchDocument<TransferReversalPatchModel> patch, @Nullable RequestOptions requestOptions) throws IOException {
return getApiClient().updateTransferReversal(reversalId, patch, requestOptions);
public ResourceResponse<TransferReversal> updateTransferReversal(@NotNull String reversalId,
@NotNull TransferReversalPatchModel patchModel, @Nullable RequestOptions requestOptions) throws IOException {

return getApiClient().updateTransferReversal(reversalId, patchModel, requestOptions);
}
}
6 changes: 4 additions & 2 deletions src/main/java/io/falu/services/WebhooksService.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ public ResourceResponse<WebhookEndpoint> getWebhookEndpoint(@NotNull String endp
* @param patch Details about what is to be changed.
* @param requestOptions Additional info to add to the request.
*/
public ResourceResponse<WebhookEndpoint> updateWebhookEndpoint(@NotNull String endpointId, JsonPatchDocument<WebhookEndpointPatchModel> patch, @Nullable RequestOptions requestOptions) throws IOException {
return getApiClient().updateWebhookEndpoint(endpointId, patch, requestOptions);
public ResourceResponse<WebhookEndpoint> updateWebhookEndpoint(@NotNull String endpointId,
WebhookEndpointPatchModel patchModel, @Nullable RequestOptions requestOptions) throws IOException {

return getApiClient().updateWebhookEndpoint(endpointId, patchModel, requestOptions);
}

/**
Expand Down
Loading

0 comments on commit 48afa72

Please sign in to comment.