Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ public StreamHTTPClient(@NotNull String apiKey, @NotNull String apiSecret) {
setCredetials(apiKey, apiSecret);
}

public StreamHTTPClient(
@NotNull String apiKey, @NotNull String apiSecret, @NotNull OkHttpClient httpClient) {
this.apiKey = apiKey;
this.apiSecret = apiSecret;
var jwtToken = buildJWT(apiSecret);
this.client = buildHTTPClient(jwtToken, httpClient.newBuilder());
}

// default constructor using ENV or System properties
// env vars have priority over system properties
public StreamHTTPClient() {
Expand Down Expand Up @@ -120,7 +128,13 @@ private void setCredetials(@NotNull String apiKey, @NotNull String apiSecret) {
this.apiKey = apiKey;
this.apiSecret = apiSecret;
var jwtToken = buildJWT(apiSecret);
this.client = buildHTTPClient(jwtToken);
this.client = buildHTTPClient(jwtToken, defaultHttpClientBuilder());
}

private OkHttpClient.Builder defaultHttpClientBuilder() {
return new OkHttpClient.Builder()
.connectionPool(new ConnectionPool(5, 59, TimeUnit.SECONDS))
.callTimeout(timeout, TimeUnit.MILLISECONDS);
}

private void readPropertiesAndEnv(Properties properties) {
Expand Down Expand Up @@ -159,11 +173,7 @@ private void readPropertiesAndEnv(Properties properties) {
return HttpLoggingInterceptor.Level.valueOf(logLevel);
}

private OkHttpClient buildHTTPClient(String jwtToken) {
OkHttpClient.Builder httpClient =
new OkHttpClient.Builder()
.connectionPool(new ConnectionPool(5, 59, TimeUnit.SECONDS))
.callTimeout(timeout, TimeUnit.MILLISECONDS);
private OkHttpClient buildHTTPClient(String jwtToken, OkHttpClient.Builder httpClient) {
httpClient.interceptors().clear();

HttpLoggingInterceptor loggingInterceptor =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.getstream.services.*;
import java.util.Properties;
import okhttp3.OkHttpClient;
import org.jetbrains.annotations.NotNull;

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

public StreamSDKClient(
@NotNull String apiKey, @NotNull String apiSecret, @NotNull OkHttpClient httpClient) {
this(new StreamHTTPClient(apiKey, apiSecret, httpClient));
}

public StreamSDKClient(StreamHTTPClient httpClient) {
super(httpClient);
this.httpClient = httpClient;
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/io/getstream/StreamHTTPClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
import io.getstream.models.TrackActivityMetricsRequest;
import io.getstream.models.UpdateAppRequest;
import io.getstream.services.framework.StreamHTTPClient;
import io.getstream.services.framework.StreamSDKClient;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

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

@Test
void testCustomOkHttpClientPreservesConfig() {
ConnectionPool customPool = new ConnectionPool(20, 120, TimeUnit.SECONDS);
OkHttpClient customHttp =
new OkHttpClient.Builder()
.connectionPool(customPool)
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(45, TimeUnit.SECONDS)
.build();

var sdkClient =
new StreamSDKClient(
System.getenv("STREAM_API_KEY"), System.getenv("STREAM_API_SECRET"), customHttp);
OkHttpClient builtClient = sdkClient.getHttpClient().getHttpClient();

assertSame(customPool, builtClient.connectionPool());
assertEquals(30_000, builtClient.connectTimeoutMillis());
assertEquals(45_000, builtClient.readTimeoutMillis());

assertFalse(
builtClient.interceptors().isEmpty(), "SDK should add its interceptors to the client");
}

@Test
void testDefaultConstructorStillWorks() {
assertNotNull(client.getHttpClient());
assertFalse(client.getHttpClient().interceptors().isEmpty());
}

@Test
void testRFC3339TimestampParsing() throws Exception {
// Create a JSON response with RFC 3339 formatted timestamp
Expand Down
Loading