Skip to content

Commit 780443e

Browse files
committed
feat: allow users to provide a custom OkHttpClient
Accept a user-supplied OkHttpClient via new constructors on StreamHTTPClient and StreamSDKClient. The SDK derives a new builder from the provided client (preserving its connection pool, timeouts, proxy, SSL config, etc.) and adds the required auth/logging/header interceptors on top. Made-with: Cursor
1 parent 66d8ba0 commit 780443e

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

src/main/java/io/getstream/services/framework/StreamHTTPClient.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public StreamHTTPClient(@NotNull String apiKey, @NotNull String apiSecret) {
5656
setCredetials(apiKey, apiSecret);
5757
}
5858

59+
public StreamHTTPClient(
60+
@NotNull String apiKey, @NotNull String apiSecret, @NotNull OkHttpClient httpClient) {
61+
this.apiKey = apiKey;
62+
this.apiSecret = apiSecret;
63+
var jwtToken = buildJWT(apiSecret);
64+
this.client = buildHTTPClient(jwtToken, httpClient.newBuilder());
65+
}
66+
5967
// default constructor using ENV or System properties
6068
// env vars have priority over system properties
6169
public StreamHTTPClient() {
@@ -120,7 +128,13 @@ private void setCredetials(@NotNull String apiKey, @NotNull String apiSecret) {
120128
this.apiKey = apiKey;
121129
this.apiSecret = apiSecret;
122130
var jwtToken = buildJWT(apiSecret);
123-
this.client = buildHTTPClient(jwtToken);
131+
this.client = buildHTTPClient(jwtToken, defaultHttpClientBuilder());
132+
}
133+
134+
private OkHttpClient.Builder defaultHttpClientBuilder() {
135+
return new OkHttpClient.Builder()
136+
.connectionPool(new ConnectionPool(5, 59, TimeUnit.SECONDS))
137+
.callTimeout(timeout, TimeUnit.MILLISECONDS);
124138
}
125139

126140
private void readPropertiesAndEnv(Properties properties) {
@@ -159,11 +173,7 @@ private void readPropertiesAndEnv(Properties properties) {
159173
return HttpLoggingInterceptor.Level.valueOf(logLevel);
160174
}
161175

162-
private OkHttpClient buildHTTPClient(String jwtToken) {
163-
OkHttpClient.Builder httpClient =
164-
new OkHttpClient.Builder()
165-
.connectionPool(new ConnectionPool(5, 59, TimeUnit.SECONDS))
166-
.callTimeout(timeout, TimeUnit.MILLISECONDS);
176+
private OkHttpClient buildHTTPClient(String jwtToken, OkHttpClient.Builder httpClient) {
167177
httpClient.interceptors().clear();
168178

169179
HttpLoggingInterceptor loggingInterceptor =

src/main/java/io/getstream/services/framework/StreamSDKClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.getstream.services.*;
44
import java.util.Properties;
5+
import okhttp3.OkHttpClient;
56
import org.jetbrains.annotations.NotNull;
67

78
public class StreamSDKClient extends CommonImpl implements Common {
@@ -19,6 +20,11 @@ public StreamSDKClient(Properties properties) {
1920
this(new StreamHTTPClient(properties));
2021
}
2122

23+
public StreamSDKClient(
24+
@NotNull String apiKey, @NotNull String apiSecret, @NotNull OkHttpClient httpClient) {
25+
this(new StreamHTTPClient(apiKey, apiSecret, httpClient));
26+
}
27+
2228
public StreamSDKClient(StreamHTTPClient httpClient) {
2329
super(httpClient);
2430
this.httpClient = httpClient;

0 commit comments

Comments
 (0)