| 
10 | 10 | import java.io.IOException;  | 
11 | 11 | import java.net.MalformedURLException;  | 
12 | 12 | import java.net.URL;  | 
 | 13 | +import java.util.Map;  | 
13 | 14 | import okhttp3.*;  | 
14 | 15 | 
 
  | 
15 | 16 | public class Evaluation {  | 
16 | 17 |  private final OkHttpClient httpClient;  | 
17 | 18 |  private final String baseURL;  | 
18 | 19 |  private final AuthenticationStrategy authenticationStrategy;  | 
 | 20 | + private final Map<String, String> headers;  | 
19 | 21 |  private final ObjectMapper objectMapper;  | 
20 | 22 | 
 
  | 
21 |  | - public Evaluation(  | 
22 |  | -  OkHttpClient httpClient, String baseURL, AuthenticationStrategy authenticationStrategy) {  | 
23 |  | - this.httpClient = httpClient;  | 
24 |  | - this.baseURL = baseURL;  | 
25 |  | - this.authenticationStrategy = authenticationStrategy;  | 
 | 23 | + private Evaluation(EvaluationBuilder builder) {  | 
 | 24 | + this.httpClient = builder.httpClient;  | 
 | 25 | + this.baseURL = builder.baseURL;  | 
 | 26 | + this.authenticationStrategy = builder.authenticationStrategy;  | 
 | 27 | + this.headers = builder.headers;  | 
26 | 28 |  this.objectMapper =  | 
27 | 29 |  JsonMapper.builder()  | 
28 | 30 |  .addModule(new Jdk8Module())  | 
29 | 31 |  .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)  | 
30 | 32 |  .build();  | 
31 | 33 |  }  | 
32 | 34 | 
 
  | 
 | 35 | + public static EvaluationBuilder builder() {  | 
 | 36 | + return new EvaluationBuilder();  | 
 | 37 | + }  | 
 | 38 | + | 
 | 39 | + public static class EvaluationBuilder {  | 
 | 40 | + private OkHttpClient httpClient;  | 
 | 41 | + private String baseURL;  | 
 | 42 | + private AuthenticationStrategy authenticationStrategy;  | 
 | 43 | + private Map<String, String> headers;  | 
 | 44 | + | 
 | 45 | + private EvaluationBuilder() {}  | 
 | 46 | + | 
 | 47 | + public EvaluationBuilder httpClient(OkHttpClient httpClient) {  | 
 | 48 | + this.httpClient = httpClient;  | 
 | 49 | + return this;  | 
 | 50 | + }  | 
 | 51 | + | 
 | 52 | + public EvaluationBuilder baseURL(String baseURL) {  | 
 | 53 | + this.baseURL = baseURL;  | 
 | 54 | + return this;  | 
 | 55 | + }  | 
 | 56 | + | 
 | 57 | + public EvaluationBuilder authenticationStrategy(AuthenticationStrategy authenticationStrategy) {  | 
 | 58 | + this.authenticationStrategy = authenticationStrategy;  | 
 | 59 | + return this;  | 
 | 60 | + }  | 
 | 61 | + | 
 | 62 | + public EvaluationBuilder headers(Map<String, String> headers) {  | 
 | 63 | + this.headers = headers;  | 
 | 64 | + return this;  | 
 | 65 | + }  | 
 | 66 | + | 
 | 67 | + public Evaluation build() {  | 
 | 68 | + return new Evaluation(this);  | 
 | 69 | + }  | 
 | 70 | + }  | 
 | 71 | + | 
33 | 72 |  @SuppressWarnings("resource")  | 
34 | 73 |  public VariantEvaluationResponse evaluateVariant(EvaluationRequest request) {  | 
35 | 74 |  URL url;  | 
36 | 75 | 
 
  | 
 | 76 | + final String path = "/evaluate/v1/variant";  | 
 | 77 | + | 
37 | 78 |  try {  | 
38 |  | - url = new URL(String.format("%s%s", this.baseURL, "/evaluate/v1/variant"));  | 
 | 79 | + url = new URL(String.format("%s%s", this.baseURL, path));  | 
39 | 80 |  } catch (MalformedURLException e) {  | 
40 | 81 |  throw new RuntimeException(e);  | 
41 | 82 |  }  | 
@@ -66,8 +107,10 @@ public VariantEvaluationResponse evaluateVariant(EvaluationRequest request) {  | 
66 | 107 |  public BooleanEvaluationResponse evaluateBoolean(EvaluationRequest request) {  | 
67 | 108 |  URL url;  | 
68 | 109 | 
 
  | 
 | 110 | + final String path = "/evaluate/v1/boolean";  | 
 | 111 | + | 
69 | 112 |  try {  | 
70 |  | - url = new URL(String.format("%s%s", this.baseURL, "/evaluate/v1/boolean"));  | 
 | 113 | + url = new URL(String.format("%s%s", this.baseURL, path));  | 
71 | 114 |  } catch (MalformedURLException e) {  | 
72 | 115 |  throw new RuntimeException(e);  | 
73 | 116 |  }  | 
@@ -106,14 +149,21 @@ public BatchEvaluationResponse evaluateBatch(BatchEvaluationRequest request) {  | 
106 | 149 |  }  | 
107 | 150 | 
 
  | 
108 | 151 |  URL url;  | 
 | 152 | + | 
 | 153 | + final String path = "/evaluate/v1/batch";  | 
 | 154 | + | 
109 | 155 |  try {  | 
110 |  | - url = new URL(String.format("%s%s", this.baseURL, "/evaluate/v1/batch"));  | 
 | 156 | + url = new URL(String.format("%s%s", this.baseURL, path));  | 
111 | 157 |  } catch (MalformedURLException e) {  | 
112 | 158 |  throw new RuntimeException(e);  | 
113 | 159 |  }  | 
114 | 160 | 
 
  | 
115 | 161 |  Request.Builder httpRequest = new Request.Builder().url(url).method("POST", body);  | 
116 | 162 | 
 
  | 
 | 163 | + if (this.headers != null) {  | 
 | 164 | + this.headers.forEach(httpRequest::addHeader);  | 
 | 165 | + }  | 
 | 166 | + | 
117 | 167 |  if (this.authenticationStrategy != null) {  | 
118 | 168 |  httpRequest.addHeader("Authorization", this.authenticationStrategy.getAuthorizationHeader());  | 
119 | 169 |  }  | 
@@ -151,6 +201,10 @@ private Request.Builder makeRequest(EvaluationRequest request, URL url) {  | 
151 | 201 | 
 
  | 
152 | 202 |  Request.Builder httpRequest = new Request.Builder().url(url).method("POST", body);  | 
153 | 203 | 
 
  | 
 | 204 | + if (this.headers != null) {  | 
 | 205 | + this.headers.forEach(httpRequest::addHeader);  | 
 | 206 | + }  | 
 | 207 | + | 
154 | 208 |  if (this.authenticationStrategy != null) {  | 
155 | 209 |  httpRequest.addHeader("Authorization", this.authenticationStrategy.getAuthorizationHeader());  | 
156 | 210 |  }  | 
 | 
0 commit comments