Skip to content

Adding POC for a client options configurer. #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/main/java/com/merge/api/MergeApiClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

import com.merge.api.core.ClientOptions;
import com.merge.api.core.Environment;
import okhttp3.OkHttpClient;

import java.util.function.Consumer;

public final class MergeApiClientBuilder {
private ClientOptions.Builder clientOptionsBuilder = ClientOptions.builder();
Expand All @@ -15,6 +18,8 @@ public final class MergeApiClientBuilder {

private Environment environment = Environment.PRODUCTION;

private Consumer<OkHttpClient.Builder> okHttpConfigurer;

/**
* Sets apiKey
*/
Expand All @@ -41,6 +46,11 @@ public MergeApiClientBuilder url(String url) {
return this;
}

public MergeApiClientBuilder okHttpConfigurer(Consumer<OkHttpClient.Builder> okHttpConfigurer) {
this.okHttpConfigurer = okHttpConfigurer;
return this;
}

public MergeApiClient build() {
if (apiKey == null) {
throw new RuntimeException("Please provide apiKey");
Expand All @@ -50,6 +60,9 @@ public MergeApiClient build() {
this.clientOptionsBuilder.addHeader("X-Account-Token", this.accountToken);
}
clientOptionsBuilder.environment(this.environment);
if (okHttpConfigurer != null) {
clientOptionsBuilder.addOkHttpConfigurer(okHttpConfigurer);
}
return new MergeApiClient(clientOptionsBuilder.build());
}
}
20 changes: 16 additions & 4 deletions src/main/java/com/merge/api/core/ClientOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Supplier;
import okhttp3.OkHttpClient;

Expand Down Expand Up @@ -76,6 +77,8 @@ public static Builder builder() {
public static final class Builder {
private Environment environment;

private Consumer<OkHttpClient.Builder> okHttpConfigurer;

private final Map<String, String> headers = new HashMap<>();

private final Map<String, Supplier<String>> headerSuppliers = new HashMap<>();
Expand All @@ -95,11 +98,20 @@ public Builder addHeader(String key, Supplier<String> value) {
return this;
}

public Builder addOkHttpConfigurer(Consumer<OkHttpClient.Builder> okHttpConfigurer) {
this.okHttpConfigurer = okHttpConfigurer;
return this;
}

public ClientOptions build() {
OkHttpClient okhttpClient = new OkHttpClient.Builder()
.addInterceptor(new RetryInterceptor(3))
.build();
return new ClientOptions(environment, headers, headerSuppliers, okhttpClient);
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder()
.addInterceptor(new RetryInterceptor(3));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be configurable as well in case I want to implement my own retry logic? And this one as a default retry mechanism that can be replaced if needed

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory you could add a new interceptor that handles retry in your way, but I'm not sure if you could clear any existing interceptors already, but you might be able to if this got merged.

Don't hold your breath though @bitsal, there's been no movement on this PR or acknowledgement of this from the merge team since I've raised the idea.


if (okHttpConfigurer != null) {
okHttpConfigurer.accept(okHttpClientBuilder);
}

return new ClientOptions(environment, headers, headerSuppliers, okHttpClientBuilder.build());
}
}
}