Skip to content

Commit

Permalink
fix: close http response object in java SDK (#110)
Browse files Browse the repository at this point in the history
* fix: close http response object in java SDK

* chore: fmt
  • Loading branch information
markphelps authored Feb 15, 2024
1 parent bc1e7d2 commit 9b23287
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions flipt-java/src/main/java/io/flipt/api/evaluation/Evaluation.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public Evaluation(
.build();
}

@SuppressWarnings("resource")
public VariantEvaluationResponse evaluateVariant(EvaluationRequest request) {
URL url;

Expand All @@ -41,8 +42,10 @@ public VariantEvaluationResponse evaluateVariant(EvaluationRequest request) {

Request.Builder requestBuilder = makeRequest(request, url);

Response response = null;

try {
Response response = httpClient.newCall(requestBuilder.build()).execute();
response = httpClient.newCall(requestBuilder.build()).execute();
assert response.body() != null;

if (!response.isSuccessful()) {
Expand All @@ -52,9 +55,14 @@ public VariantEvaluationResponse evaluateVariant(EvaluationRequest request) {
return this.objectMapper.readValue(response.body().string(), VariantEvaluationResponse.class);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (response != null) {
response.close();
}
}
}

@SuppressWarnings("resource")
public BooleanEvaluationResponse evaluateBoolean(EvaluationRequest request) {
URL url;

Expand All @@ -66,8 +74,9 @@ public BooleanEvaluationResponse evaluateBoolean(EvaluationRequest request) {

Request.Builder requestBuilder = makeRequest(request, url);

Response response = null;
try {
Response response = httpClient.newCall(requestBuilder.build()).execute();
response = httpClient.newCall(requestBuilder.build()).execute();
assert response.body() != null;
if (!response.isSuccessful()) {
Error error = this.objectMapper.readValue(response.body().string(), Error.class);
Expand All @@ -77,9 +86,14 @@ public BooleanEvaluationResponse evaluateBoolean(EvaluationRequest request) {
return this.objectMapper.readValue(response.body().string(), BooleanEvaluationResponse.class);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (response != null) {
response.close();
}
}
}

@SuppressWarnings("resource")
public BatchEvaluationResponse evaluateBatch(BatchEvaluationRequest request) {
RequestBody body;

Expand All @@ -104,8 +118,10 @@ public BatchEvaluationResponse evaluateBatch(BatchEvaluationRequest request) {
httpRequest.addHeader("Authorization", this.authenticationStrategy.getAuthorizationHeader());
}

Response response = null;

try {
Response response = httpClient.newCall(httpRequest.build()).execute();
response = httpClient.newCall(httpRequest.build()).execute();
assert response.body() != null;
if (!response.isSuccessful()) {
Error error = this.objectMapper.readValue(response.body().string(), Error.class);
Expand All @@ -115,6 +131,10 @@ public BatchEvaluationResponse evaluateBatch(BatchEvaluationRequest request) {
return this.objectMapper.readValue(response.body().string(), BatchEvaluationResponse.class);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (response != null) {
response.close();
}
}
}

Expand Down

0 comments on commit 9b23287

Please sign in to comment.