Skip to content

Commit 5c828b4

Browse files
authored
Merge pull request #53 from GetStream/expose-http-client
[FEEDS-1373] allow users to provide a custom OkHttpClient
2 parents 66d8ba0 + 5a65825 commit 5c828b4

3 files changed

Lines changed: 55 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;

src/test/java/io/getstream/StreamHTTPClientTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
import io.getstream.models.TrackActivityMetricsRequest;
1010
import io.getstream.models.UpdateAppRequest;
1111
import io.getstream.services.framework.StreamHTTPClient;
12+
import io.getstream.services.framework.StreamSDKClient;
1213
import java.util.Date;
1314
import java.util.List;
1415
import java.util.Map;
16+
import java.util.concurrent.TimeUnit;
17+
import okhttp3.ConnectionPool;
18+
import okhttp3.OkHttpClient;
1519
import org.junit.jupiter.api.BeforeAll;
1620
import org.junit.jupiter.api.Test;
1721

@@ -135,6 +139,35 @@ void testTrackActivityMetricsRequestSerializedWithCustomMetric() throws JsonProc
135139
assertTrue(json.contains("\"delta\":3"), "Expected delta in: " + json);
136140
}
137141

142+
@Test
143+
void testCustomOkHttpClientPreservesConfig() {
144+
ConnectionPool customPool = new ConnectionPool(20, 120, TimeUnit.SECONDS);
145+
OkHttpClient customHttp =
146+
new OkHttpClient.Builder()
147+
.connectionPool(customPool)
148+
.connectTimeout(30, TimeUnit.SECONDS)
149+
.readTimeout(45, TimeUnit.SECONDS)
150+
.build();
151+
152+
var sdkClient =
153+
new StreamSDKClient(
154+
System.getenv("STREAM_API_KEY"), System.getenv("STREAM_API_SECRET"), customHttp);
155+
OkHttpClient builtClient = sdkClient.getHttpClient().getHttpClient();
156+
157+
assertSame(customPool, builtClient.connectionPool());
158+
assertEquals(30_000, builtClient.connectTimeoutMillis());
159+
assertEquals(45_000, builtClient.readTimeoutMillis());
160+
161+
assertFalse(
162+
builtClient.interceptors().isEmpty(), "SDK should add its interceptors to the client");
163+
}
164+
165+
@Test
166+
void testDefaultConstructorStillWorks() {
167+
assertNotNull(client.getHttpClient());
168+
assertFalse(client.getHttpClient().interceptors().isEmpty());
169+
}
170+
138171
@Test
139172
void testRFC3339TimestampParsing() throws Exception {
140173
// Create a JSON response with RFC 3339 formatted timestamp

0 commit comments

Comments
 (0)