|
1 | 1 | package dev.openfeature.contrib.providers.gofeatureflag;
|
2 | 2 |
|
3 | 3 | import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
|
| 4 | +import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED; |
4 | 5 |
|
5 | 6 | import com.fasterxml.jackson.databind.DeserializationFeature;
|
6 | 7 | import com.fasterxml.jackson.databind.ObjectMapper;
|
@@ -52,6 +53,9 @@ public class GoFeatureFlagProvider implements FeatureProvider {
|
52 | 53 | // httpClient is the instance of the OkHttpClient used by the provider
|
53 | 54 | private OkHttpClient httpClient;
|
54 | 55 |
|
| 56 | + // apiKey contains the token to use while calling GO Feature Flag relay proxy |
| 57 | + private String apiKey; |
| 58 | + |
55 | 59 | /**
|
56 | 60 | * Constructor of the provider.
|
57 | 61 | *
|
@@ -108,6 +112,7 @@ private void initializeProvider(GoFeatureFlagProviderOptions options) throws Inv
|
108 | 112 | if (this.parsedEndpoint == null) {
|
109 | 113 | throw new InvalidEndpoint();
|
110 | 114 | }
|
| 115 | + this.apiKey = options.getApiKey(); |
111 | 116 | }
|
112 | 117 |
|
113 | 118 | @Override
|
@@ -179,15 +184,21 @@ private <T> ProviderEvaluation<T> resolveEvaluationGoFeatureFlagProxy(
|
179 | 184 | .addEncodedPathSegment("eval")
|
180 | 185 | .build();
|
181 | 186 |
|
182 |
| - Request request = new Request.Builder() |
| 187 | + Request.Builder reqBuilder = new Request.Builder() |
183 | 188 | .url(url)
|
184 | 189 | .addHeader("Content-Type", "application/json")
|
185 | 190 | .post(RequestBody.create(
|
186 | 191 | requestMapper.writeValueAsBytes(goffRequest),
|
187 |
| - MediaType.get("application/json; charset=utf-8"))) |
188 |
| - .build(); |
| 192 | + MediaType.get("application/json; charset=utf-8"))); |
189 | 193 |
|
190 |
| - try (Response response = this.httpClient.newCall(request).execute()) { |
| 194 | + if (this.apiKey != null && !"".equals(this.apiKey)) { |
| 195 | + reqBuilder.addHeader("Authorization", "Bearer " + this.apiKey); |
| 196 | + } |
| 197 | + |
| 198 | + try (Response response = this.httpClient.newCall(reqBuilder.build()).execute()) { |
| 199 | + if (response.code() == HTTP_UNAUTHORIZED) { |
| 200 | + throw new GeneralError("invalid token used to contact GO Feature Flag relay proxy instance"); |
| 201 | + } |
191 | 202 | if (response.code() >= HTTP_BAD_REQUEST) {
|
192 | 203 | throw new GeneralError("impossible to contact GO Feature Flag relay proxy instance");
|
193 | 204 | }
|
|
0 commit comments