Skip to content

Commit

Permalink
fix(java): expose EvaluationException instead of the RuntimeException (
Browse files Browse the repository at this point in the history
  • Loading branch information
erka authored Oct 20, 2024
1 parent 52f89f2 commit e672cff
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 127 deletions.
3 changes: 2 additions & 1 deletion flipt-java/src/main/java/examples/Main.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package examples;

import io.flipt.api.FliptClient;
import io.flipt.api.evaluation.EvaluationException;
import io.flipt.api.evaluation.models.*;
import java.util.*;

public class Main {
public static void main(String[] args) {
public static void main(String[] args) throws EvaluationException {
FliptClient fliptClient = FliptClient.builder().build();
Map<String, String> context = new HashMap<>();

Expand Down
146 changes: 31 additions & 115 deletions flipt-java/src/main/java/io/flipt/api/evaluation/Evaluation.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import io.flipt.api.evaluation.models.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;
import okhttp3.*;
Expand Down Expand Up @@ -69,146 +71,60 @@ public Evaluation build() {
}
}

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

final String path = "/evaluate/v1/variant";

try {
url = new URL(String.format("%s%s", this.baseURL, path));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}

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

Response response = null;
public VariantEvaluationResponse evaluateVariant(EvaluationRequest request)
throws EvaluationException {
return this.makeRequest(request, "/evaluate/v1/variant", VariantEvaluationResponse.class);
}

try {
response = httpClient.newCall(requestBuilder.build()).execute();
assert response.body() != null;
public BooleanEvaluationResponse evaluateBoolean(EvaluationRequest request)
throws EvaluationException {
return this.makeRequest(request, "/evaluate/v1/boolean", BooleanEvaluationResponse.class);
}

if (!response.isSuccessful()) {
Error error = this.objectMapper.readValue(response.body().string(), Error.class);
throw new RuntimeException(error);
}
return this.objectMapper.readValue(response.body().string(), VariantEvaluationResponse.class);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (response != null) {
response.close();
}
}
public BatchEvaluationResponse evaluateBatch(BatchEvaluationRequest request)
throws EvaluationException {
return this.makeRequest(request, "/evaluate/v1/batch", BatchEvaluationResponse.class);
}

@SuppressWarnings("resource")
public BooleanEvaluationResponse evaluateBoolean(EvaluationRequest request) {
private <T> T makeRequest(Object request, String path, Class<T> clazz)
throws EvaluationException {
URL url;

final String path = "/evaluate/v1/boolean";

try {
url = new URL(String.format("%s%s", this.baseURL, path));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
url = new URI(String.format("%s%s", this.baseURL, path)).toURL();
} catch (URISyntaxException | MalformedURLException e) {
throw new EvaluationException(e);
}

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

Response response = null;
try {
response = httpClient.newCall(requestBuilder.build()).execute();
assert response.body() != null;
if (!response.isSuccessful()) {
Error error = this.objectMapper.readValue(response.body().string(), Error.class);
throw new RuntimeException(error);
}

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;

try {
body =
RequestBody body =
RequestBody.create(
this.objectMapper.writeValueAsString(request), MediaType.parse("application/json"));
} catch (Exception e) {
throw new RuntimeException(e);
}

URL url;

final String path = "/evaluate/v1/batch";
Request.Builder httpRequest = new Request.Builder().url(url).method("POST", body);

try {
url = new URL(String.format("%s%s", this.baseURL, path));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}

Request.Builder httpRequest = new Request.Builder().url(url).method("POST", body);

if (this.headers != null) {
this.headers.forEach(httpRequest::addHeader);
}

if (this.authenticationStrategy != null) {
httpRequest.addHeader("Authorization", this.authenticationStrategy.getAuthorizationHeader());
}
if (this.headers != null) {
this.headers.forEach(httpRequest::addHeader);
}

Response response = null;
if (this.authenticationStrategy != null) {
httpRequest.addHeader(
"Authorization", this.authenticationStrategy.getAuthorizationHeader());
}

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

if (!response.isSuccessful()) {
Error error = this.objectMapper.readValue(response.body().string(), Error.class);
throw new RuntimeException(error);
throw new EvaluationException(error);
}

return this.objectMapper.readValue(response.body().string(), BatchEvaluationResponse.class);
return this.objectMapper.readValue(response.body().string(), clazz);
} catch (IOException e) {
throw new RuntimeException(e);
throw new EvaluationException(e);
} finally {
if (response != null) {
response.close();
}
}
}

private Request.Builder makeRequest(EvaluationRequest request, URL url) {
RequestBody body;

try {
body =
RequestBody.create(
this.objectMapper.writeValueAsString(request), MediaType.parse("application/json"));
} catch (Exception e) {
throw new RuntimeException(e);
}

Request.Builder httpRequest = new Request.Builder().url(url).method("POST", body);

if (this.headers != null) {
this.headers.forEach(httpRequest::addHeader);
}

if (this.authenticationStrategy != null) {
httpRequest.addHeader("Authorization", this.authenticationStrategy.getAuthorizationHeader());
}

return httpRequest;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.flipt.api.evaluation;

public class EvaluationException extends Exception {

public EvaluationException() {
super();
}

public EvaluationException(Throwable cause) {
super(cause);
}
}
30 changes: 19 additions & 11 deletions flipt-java/src/test/java/TestFliptClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ void testVariantEvaluation() {

Map<String, String> context = new HashMap<>();
context.put("fizz", "buzz");
VariantEvaluationResponse variant =
fc.evaluation()
.evaluateVariant(
new EvaluationRequest("default", "flag1", "entity", context, Optional.empty()));

VariantEvaluationResponse variant =
Assertions.assertDoesNotThrow(
() ->
fc.evaluation()
.evaluateVariant(
new EvaluationRequest(
"default", "flag1", "entity", context, Optional.empty())));
Assertions.assertTrue(variant.isMatch());
Assertions.assertEquals("flag1", variant.getFlagKey());
Assertions.assertEquals("MATCH_EVALUATION_REASON", variant.getReason().toString());
Expand Down Expand Up @@ -65,10 +68,12 @@ void testBooleanEvaluation() {
context.put("fizz", "buzz");

BooleanEvaluationResponse booleanEvaluation =
fc.evaluation()
.evaluateBoolean(
new EvaluationRequest(
"default", "flag_boolean", "entity", context, Optional.empty()));
Assertions.assertDoesNotThrow(
() ->
fc.evaluation()
.evaluateBoolean(
new EvaluationRequest(
"default", "flag_boolean", "entity", context, Optional.empty())));

Assertions.assertTrue(booleanEvaluation.isEnabled());
Assertions.assertEquals("flag_boolean", booleanEvaluation.getFlagKey());
Expand Down Expand Up @@ -111,9 +116,12 @@ void testBatchEvaluation() {
evaluationRequests.add(errorEvaluationRequest);

BatchEvaluationResponse batch =
fc.evaluation()
.evaluateBatch(
new BatchEvaluationRequest(Optional.of(""), evaluationRequests, Optional.empty()));
Assertions.assertDoesNotThrow(
() ->
fc.evaluation()
.evaluateBatch(
new BatchEvaluationRequest(
Optional.of(""), evaluationRequests, Optional.empty())));

// Variant
EvaluationResponse first = batch.getResponses().get(0);
Expand Down

0 comments on commit e672cff

Please sign in to comment.