|
| 1 | +package dev.openfeature.contrib.providers.ofrep.internal; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import com.google.common.collect.ImmutableList; |
| 7 | +import com.google.common.collect.ImmutableMap; |
| 8 | +import dev.openfeature.sdk.exceptions.GeneralError; |
| 9 | +import java.io.IOException; |
| 10 | +import java.net.ProxySelector; |
| 11 | +import java.net.URI; |
| 12 | +import java.net.http.HttpClient; |
| 13 | +import java.net.http.HttpRequest; |
| 14 | +import java.net.http.HttpResponse; |
| 15 | +import java.time.Duration; |
| 16 | +import java.time.Instant; |
| 17 | +import java.util.concurrent.Executor; |
| 18 | +import lombok.extern.slf4j.Slf4j; |
| 19 | + |
| 20 | +/** |
| 21 | + * The OfrepApi class is responsible for communicating with the OFREP server to evaluate flags. |
| 22 | + */ |
| 23 | +@Slf4j |
| 24 | +public class OfrepApi { |
| 25 | + |
| 26 | + private static final String path = "/ofrep/v1/evaluate/flags/"; |
| 27 | + private final ObjectMapper serializer; |
| 28 | + private final ObjectMapper deserializer; |
| 29 | + private final HttpClient httpClient; |
| 30 | + private Instant nextAllowedRequestTime = Instant.now(); |
| 31 | + |
| 32 | + /** |
| 33 | + * Constructs an OfrepApi instance with a HTTP client and JSON serializers. |
| 34 | + * |
| 35 | + * @param timeout - The timeout duration for establishing HTTP connection. |
| 36 | + * @param proxySelector - The ProxySelector to use for HTTP requests. |
| 37 | + * @param executor - The Executor to use for operations. |
| 38 | + */ |
| 39 | + public OfrepApi(Duration timeout, ProxySelector proxySelector, Executor executor) { |
| 40 | + httpClient = HttpClient.newBuilder() |
| 41 | + .connectTimeout(timeout) |
| 42 | + .proxy(proxySelector) |
| 43 | + .executor(executor) |
| 44 | + .build(); |
| 45 | + serializer = new ObjectMapper(); |
| 46 | + deserializer = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * prepareHttpRequest is preparing the request to be sent to the OFREP Server. |
| 51 | + * |
| 52 | + * @param url - url of the request |
| 53 | + * @param headers - headers to be included in the request |
| 54 | + * @param requestBody - body of the request |
| 55 | + * |
| 56 | + * @return HttpRequest ready to be sent |
| 57 | + * @throws JsonProcessingException - if an error occurred while processing the json. |
| 58 | + */ |
| 59 | + private <T> HttpRequest prepareHttpRequest( |
| 60 | + final URI url, ImmutableMap<String, ImmutableList<String>> headers, final T requestBody) |
| 61 | + throws JsonProcessingException { |
| 62 | + |
| 63 | + HttpRequest.Builder reqBuilder = HttpRequest.newBuilder() |
| 64 | + .uri(url) |
| 65 | + .header("Content-Type", "application/json; charset=utf-8") |
| 66 | + .POST(HttpRequest.BodyPublishers.ofByteArray(serializer.writeValueAsBytes(requestBody))); |
| 67 | + |
| 68 | + for (ImmutableMap.Entry<String, ImmutableList<String>> entry : headers.entrySet()) { |
| 69 | + String key = entry.getKey(); |
| 70 | + for (String value : entry.getValue()) { |
| 71 | + reqBuilder.header(key, value); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return reqBuilder.build(); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * resolve is the method that interacts with the OFREP server to evaluate flags. |
| 80 | + * |
| 81 | + * @param baseUrl - The base URL of the OFREP server. |
| 82 | + * @param headers - headers to include in the request. |
| 83 | + * @param key - The flag key to evaluate. |
| 84 | + * @param requestBody - The evaluation context as a map of key-value pairs. |
| 85 | + * |
| 86 | + * @return Resolution object, containing the response status, headers, and body. |
| 87 | + */ |
| 88 | + public Resolution resolve( |
| 89 | + String baseUrl, |
| 90 | + ImmutableMap<String, ImmutableList<String>> headers, |
| 91 | + String key, |
| 92 | + final OfrepRequest requestBody) { |
| 93 | + if (nextAllowedRequestTime.isAfter(Instant.now())) { |
| 94 | + throw new GeneralError("Rate limit exceeded. Please wait before making another request."); |
| 95 | + } |
| 96 | + |
| 97 | + try { |
| 98 | + String fullPath = baseUrl + path + key; |
| 99 | + URI uri = URI.create(fullPath); |
| 100 | + |
| 101 | + HttpRequest request = prepareHttpRequest(uri, headers, requestBody); |
| 102 | + |
| 103 | + HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); |
| 104 | + OfrepResponse responseBody = deserializer.readValue(response.body(), OfrepResponse.class); |
| 105 | + |
| 106 | + return new Resolution(response.statusCode(), response.headers(), responseBody); |
| 107 | + } catch (JsonProcessingException e) { |
| 108 | + throw new GeneralError("Error processing JSON: " + e.getMessage()); |
| 109 | + } catch (IOException e) { |
| 110 | + throw new GeneralError("IO error: " + e.getMessage()); |
| 111 | + } catch (InterruptedException e) { |
| 112 | + throw new GeneralError("Request interrupted: " + e.getMessage()); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Sets the next allowed request time based on the Retry-After header. |
| 118 | + * If the provided time is later than the current next allowed request time, it updates it. |
| 119 | + * |
| 120 | + * @param retryAfter The value of the Retry-After header, which can be a number of seconds or a date string. |
| 121 | + */ |
| 122 | + public void setNextAllowedRequestTime(Instant retryAfter) { |
| 123 | + if (retryAfter.isAfter(nextAllowedRequestTime)) { |
| 124 | + nextAllowedRequestTime = retryAfter; |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments