diff --git a/flipt-java/src/main/java/examples/Main.java b/flipt-java/src/main/java/examples/Main.java index b4a019f..d892aae 100644 --- a/flipt-java/src/main/java/examples/Main.java +++ b/flipt-java/src/main/java/examples/Main.java @@ -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 context = new HashMap<>(); diff --git a/flipt-java/src/main/java/io/flipt/api/evaluation/Evaluation.java b/flipt-java/src/main/java/io/flipt/api/evaluation/Evaluation.java index 191448e..35d5671 100644 --- a/flipt-java/src/main/java/io/flipt/api/evaluation/Evaluation.java +++ b/flipt-java/src/main/java/io/flipt/api/evaluation/Evaluation.java @@ -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.*; @@ -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 makeRequest(Object request, String path, Class 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; - } } diff --git a/flipt-java/src/main/java/io/flipt/api/evaluation/EvaluationException.java b/flipt-java/src/main/java/io/flipt/api/evaluation/EvaluationException.java new file mode 100644 index 0000000..c442dd9 --- /dev/null +++ b/flipt-java/src/main/java/io/flipt/api/evaluation/EvaluationException.java @@ -0,0 +1,12 @@ +package io.flipt.api.evaluation; + +public class EvaluationException extends Exception { + + public EvaluationException() { + super(); + } + + public EvaluationException(Throwable cause) { + super(cause); + } +} diff --git a/flipt-java/src/test/java/TestFliptClient.java b/flipt-java/src/test/java/TestFliptClient.java index 79ead8e..6c861ff 100644 --- a/flipt-java/src/test/java/TestFliptClient.java +++ b/flipt-java/src/test/java/TestFliptClient.java @@ -29,11 +29,14 @@ void testVariantEvaluation() { Map 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()); @@ -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()); @@ -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);