|
| 1 | +package datadog.smoketest.springboot.openfeature; |
| 2 | + |
| 3 | +import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; |
| 4 | + |
| 5 | +import dev.openfeature.sdk.Client; |
| 6 | +import dev.openfeature.sdk.EvaluationContext; |
| 7 | +import dev.openfeature.sdk.FeatureProvider; |
| 8 | +import dev.openfeature.sdk.FlagEvaluationDetails; |
| 9 | +import dev.openfeature.sdk.MutableContext; |
| 10 | +import dev.openfeature.sdk.OpenFeatureAPI; |
| 11 | +import dev.openfeature.sdk.Structure; |
| 12 | +import dev.openfeature.sdk.Value; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import org.slf4j.Logger; |
| 17 | +import org.slf4j.LoggerFactory; |
| 18 | +import org.springframework.http.ResponseEntity; |
| 19 | +import org.springframework.web.bind.annotation.GetMapping; |
| 20 | +import org.springframework.web.bind.annotation.PostMapping; |
| 21 | +import org.springframework.web.bind.annotation.RequestBody; |
| 22 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 23 | +import org.springframework.web.bind.annotation.RestController; |
| 24 | + |
| 25 | +@RestController |
| 26 | +@RequestMapping("/openfeature") |
| 27 | +public class OpenFeatureController { |
| 28 | + |
| 29 | + private static final Logger LOGGER = LoggerFactory.getLogger(OpenFeatureController.class); |
| 30 | + |
| 31 | + private final Client client; |
| 32 | + |
| 33 | + public OpenFeatureController() { |
| 34 | + OpenFeatureAPI api = OpenFeatureAPI.getInstance(); |
| 35 | + api.setProviderAndWait(new datadog.trace.api.openfeature.Provider()); |
| 36 | + this.client = api.getClient(); |
| 37 | + } |
| 38 | + |
| 39 | + @GetMapping("/provider-metadata") |
| 40 | + public Map<String, Object> getProviderMetadata() { |
| 41 | + FeatureProvider provider = OpenFeatureAPI.getInstance().getProvider(); |
| 42 | + Map<String, Object> response = new HashMap<>(); |
| 43 | + response.put("providerClass", provider.getClass()); |
| 44 | + response.put("metadata", provider.getMetadata().getName()); |
| 45 | + return response; |
| 46 | + } |
| 47 | + |
| 48 | + @PostMapping( |
| 49 | + value = "/evaluate", |
| 50 | + consumes = APPLICATION_JSON_VALUE, |
| 51 | + produces = APPLICATION_JSON_VALUE) |
| 52 | + public ResponseEntity<?> evaluate(@RequestBody final EvaluateRequest request) { |
| 53 | + try { |
| 54 | + final EvaluationContext context = context(request); |
| 55 | + FlagEvaluationDetails<?> details; |
| 56 | + String reason; |
| 57 | + switch (request.getVariationType()) { |
| 58 | + case "BOOLEAN": |
| 59 | + details = |
| 60 | + client.getBooleanDetails( |
| 61 | + request.getFlag(), (Boolean) request.getDefaultValue(), context); |
| 62 | + break; |
| 63 | + case "STRING": |
| 64 | + details = |
| 65 | + client.getStringDetails( |
| 66 | + request.getFlag(), (String) request.getDefaultValue(), context); |
| 67 | + break; |
| 68 | + case "INTEGER": |
| 69 | + final Number integerEval = (Number) request.getDefaultValue(); |
| 70 | + details = client.getIntegerDetails(request.getFlag(), integerEval.intValue(), context); |
| 71 | + break; |
| 72 | + case "NUMERIC": |
| 73 | + final Number doubleEval = (Number) request.getDefaultValue(); |
| 74 | + details = client.getDoubleDetails(request.getFlag(), doubleEval.doubleValue(), context); |
| 75 | + break; |
| 76 | + case "JSON": |
| 77 | + details = |
| 78 | + client.getObjectDetails( |
| 79 | + request.getFlag(), Value.objectToValue(request.getDefaultValue()), context); |
| 80 | + break; |
| 81 | + default: |
| 82 | + throw new IllegalArgumentException( |
| 83 | + "Unsupported variation type: " + request.getVariationType()); |
| 84 | + } |
| 85 | + |
| 86 | + final Object value = details.getValue(); |
| 87 | + final Map<String, Object> result = new HashMap<>(); |
| 88 | + result.put("flagKey", details.getFlagKey()); |
| 89 | + result.put("variant", details.getVariant()); |
| 90 | + result.put("reason", details.getReason()); |
| 91 | + result.put("value", value instanceof Value ? context.convertValue((Value) value) : value); |
| 92 | + result.put("errorCode", details.getErrorCode()); |
| 93 | + result.put("errorMessage", details.getErrorMessage()); |
| 94 | + result.put("flagMetadata", details.getFlagMetadata().asUnmodifiableMap()); |
| 95 | + return ResponseEntity.ok(result); |
| 96 | + } catch (Throwable e) { |
| 97 | + LOGGER.error("Error on resolution", e); |
| 98 | + return ResponseEntity.internalServerError().body(e.getMessage()); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private static EvaluationContext context(final EvaluateRequest request) { |
| 103 | + final MutableContext context = new MutableContext(); |
| 104 | + context.setTargetingKey(request.getTargetingKey()); |
| 105 | + if (request.attributes != null) { |
| 106 | + request.attributes.forEach( |
| 107 | + (key, value) -> { |
| 108 | + if (value instanceof Boolean) { |
| 109 | + context.add(key, (Boolean) value); |
| 110 | + } else if (value instanceof Integer) { |
| 111 | + context.add(key, (Integer) value); |
| 112 | + } else if (value instanceof Double) { |
| 113 | + context.add(key, (Double) value); |
| 114 | + } else if (value instanceof String) { |
| 115 | + context.add(key, (String) value); |
| 116 | + } else if (value instanceof Map) { |
| 117 | + context.add(key, Value.objectToValue(value).asStructure()); |
| 118 | + } else if (value instanceof List) { |
| 119 | + context.add(key, Value.objectToValue(value).asList()); |
| 120 | + } else { |
| 121 | + context.add(key, (Structure) null); |
| 122 | + } |
| 123 | + }); |
| 124 | + } |
| 125 | + return context; |
| 126 | + } |
| 127 | + |
| 128 | + public static class EvaluateRequest { |
| 129 | + private String flag; |
| 130 | + private String variationType; |
| 131 | + private Object defaultValue; |
| 132 | + private String targetingKey; |
| 133 | + private Map<String, Object> attributes; |
| 134 | + |
| 135 | + public Map<String, Object> getAttributes() { |
| 136 | + return attributes; |
| 137 | + } |
| 138 | + |
| 139 | + public void setAttributes(Map<String, Object> attributes) { |
| 140 | + this.attributes = attributes; |
| 141 | + } |
| 142 | + |
| 143 | + public Object getDefaultValue() { |
| 144 | + return defaultValue; |
| 145 | + } |
| 146 | + |
| 147 | + public void setDefaultValue(Object defaultValue) { |
| 148 | + this.defaultValue = defaultValue; |
| 149 | + } |
| 150 | + |
| 151 | + public String getFlag() { |
| 152 | + return flag; |
| 153 | + } |
| 154 | + |
| 155 | + public void setFlag(String flag) { |
| 156 | + this.flag = flag; |
| 157 | + } |
| 158 | + |
| 159 | + public String getTargetingKey() { |
| 160 | + return targetingKey; |
| 161 | + } |
| 162 | + |
| 163 | + public void setTargetingKey(String targetingKey) { |
| 164 | + this.targetingKey = targetingKey; |
| 165 | + } |
| 166 | + |
| 167 | + public String getVariationType() { |
| 168 | + return variationType; |
| 169 | + } |
| 170 | + |
| 171 | + public void setVariationType(String variationType) { |
| 172 | + this.variationType = variationType; |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments