Skip to content

Commit

Permalink
fix(java-sdk): don't create a new HttpClient on every request (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhamzeh authored Jan 26, 2024
2 parents 6ff0871 + afa957f commit 887f015
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions config/clients/java/config.overrides.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@
"destinationFilename": "src/test-integration/java/dev/openfga/sdk/api/client/OpenFgaClientIntegrationTest.java",
"templateType": "SupportingFiles"
},
"client-ApiClientTest.java.mustache" : {
"destinationFilename": "src/test/java/dev/openfga/sdk/api/client/ApiClientTest.java",
"templateType": "SupportingFiles"
},
"creds-AccessToken.java.mustache" : {
"destinationFilename": "src/main/java/dev/openfga/sdk/api/auth/AccessToken.java",
"templateType": "SupportingFiles"
Expand Down
27 changes: 27 additions & 0 deletions config/clients/java/template/client-ApiClientTest.java.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{{>licenseInfo}}
package {{invokerPackage}};

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import java.net.http.HttpClient;
import org.junit.jupiter.api.Test;

class ApiClientTest {
@Test
public void returnSameHttpClient() {
ApiClient apiClient = new ApiClient();
assertEquals(apiClient.getHttpClient(), apiClient.getHttpClient());
}

@Test
public void newHttpClientWhenBuilderModified() {
ApiClient apiClient = new ApiClient();
HttpClient client1 = apiClient.getHttpClient();
apiClient.setHttpClientBuilder(HttpClient.newBuilder());
assertNotEquals(client1, apiClient.getHttpClient());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import static java.nio.charset.StandardCharsets.UTF_8;
public class ApiClient {
private HttpClient.Builder builder;
private HttpClient client;
private ObjectMapper mapper;
private Consumer<HttpRequest.Builder> interceptor;
private Consumer<HttpResponse<InputStream>> responseInterceptor;
Expand All @@ -59,6 +60,7 @@ public class ApiClient {
public ApiClient() {
this.builder = createDefaultHttpClientBuilder();
this.mapper = createDefaultObjectMapper();
this.client = this.builder.build();
interceptor = null;
responseInterceptor = null;
asyncResponseInterceptor = null;
Expand All @@ -77,6 +79,7 @@ public class ApiClient {
public ApiClient(HttpClient.Builder builder, ObjectMapper mapper) {
this.builder = builder;
this.mapper = mapper;
this.client = this.builder.build();
interceptor = null;
responseInterceptor = null;
asyncResponseInterceptor = null;
Expand Down Expand Up @@ -174,6 +177,7 @@ public class ApiClient {
*/
public ApiClient setHttpClientBuilder(HttpClient.Builder builder) {
this.builder = builder;
this.client = this.builder.build();
return this;
}

Expand All @@ -185,7 +189,7 @@ public class ApiClient {
* @return The HTTP client.
*/
public HttpClient getHttpClient() {
return builder.build();
return client;
}

/**
Expand Down

0 comments on commit 887f015

Please sign in to comment.