Skip to content

Commit 0f438cc

Browse files
handle 304
Signed-off-by: Thomas Poignant <[email protected]>
1 parent 76ec5bf commit 0f438cc

File tree

1 file changed

+26
-14
lines changed
  • providers/go-feature-flag/src/main/java/dev/openfeature/contrib/providers/gofeatureflag/api

1 file changed

+26
-14
lines changed

providers/go-feature-flag/src/main/java/dev/openfeature/contrib/providers/gofeatureflag/api/GoFeatureFlagApi.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ public FlagConfigResponse retrieveFlagConfiguration(final String etag, final Lis
184184
HttpResponse<String> response =
185185
this.httpClient.send(reqBuilder.build(), HttpResponse.BodyHandlers.ofString());
186186
String body = response.body();
187-
188187
switch (response.statusCode()) {
189188
case HttpURLConnection.HTTP_OK:
189+
case HttpURLConnection.HTTP_NOT_MODIFIED:
190190
return handleFlagConfigurationSuccess(response, body);
191191
case HttpURLConnection.HTTP_NOT_FOUND:
192192
throw new FlagConfigurationEndpointNotFound();
@@ -259,25 +259,37 @@ public void sendEventToDataCollector(final List<IEvent> eventsList, final Map<St
259259
*/
260260
private FlagConfigResponse handleFlagConfigurationSuccess(final HttpResponse<String> response, final String body)
261261
throws JsonProcessingException {
262-
val goffResp = Const.DESERIALIZE_OBJECT_MAPPER.readValue(body, FlagConfigApiResponse.class);
263-
val etagHeader = response.headers().firstValue(Const.HTTP_HEADER_ETAG).orElse(null);
264-
Date lastUpdated;
262+
var result = FlagConfigResponse.builder()
263+
.etag(response.headers().firstValue(Const.HTTP_HEADER_ETAG).orElse(null))
264+
.lastUpdated(extractLastUpdatedFromHeaders(response))
265+
.build();
266+
267+
if (response.statusCode() == HttpURLConnection.HTTP_OK) {
268+
val goffResp = Const.DESERIALIZE_OBJECT_MAPPER.readValue(body, FlagConfigApiResponse.class);
269+
result.setFlags(goffResp.getFlags());
270+
result.setEvaluationContextEnrichment(goffResp.getEvaluationContextEnrichment());
271+
}
272+
273+
return result;
274+
}
275+
276+
/**
277+
* extractLastUpdatedFromHeaders is extracting the Last-Modified header from the response.
278+
*
279+
* @param response - the HTTP response
280+
* @return Date - the parsed Last-Modified date, or null if not present or parsing fails
281+
*/
282+
private Date extractLastUpdatedFromHeaders(final HttpResponse<String> response) {
265283
try {
266-
val headerValue = response.headers()
284+
String headerValue = response.headers()
267285
.firstValue(Const.HTTP_HEADER_LAST_MODIFIED)
268286
.orElse(null);
269-
val lastModifiedHeaderFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
270-
lastUpdated = headerValue != null ? lastModifiedHeaderFormatter.parse(headerValue) : null;
287+
SimpleDateFormat lastModifiedHeaderFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
288+
return headerValue != null ? lastModifiedHeaderFormatter.parse(headerValue) : null;
271289
} catch (Exception e) {
272290
log.debug("Error parsing Last-Modified header: {}", e.getMessage());
273-
lastUpdated = null;
291+
return null;
274292
}
275-
return FlagConfigResponse.builder()
276-
.flags(goffResp.getFlags())
277-
.etag(etagHeader)
278-
.evaluationContextEnrichment(goffResp.getEvaluationContextEnrichment())
279-
.lastUpdated(lastUpdated)
280-
.build();
281293
}
282294

283295
/**

0 commit comments

Comments
 (0)