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