com.devcycle.sdk.server.openfeature.DevCycleProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-server-sdk Show documentation
Show all versions of java-server-sdk Show documentation
Server side SDK to interact with DevCycle.
package com.devcycle.sdk.server.openfeature;
import com.devcycle.sdk.server.common.api.IDevCycleClient;
import com.devcycle.sdk.server.common.model.DevCycleUser;
import com.devcycle.sdk.server.common.model.Variable;
import dev.openfeature.sdk.*;
import dev.openfeature.sdk.exceptions.ProviderNotReadyError;
import dev.openfeature.sdk.exceptions.TypeMismatchError;
import java.util.Map;
public class DevCycleProvider implements FeatureProvider {
private static final String PROVIDER_NAME = "DevCycle";
private final IDevCycleClient devcycleClient;
public DevCycleProvider(IDevCycleClient devcycleClient) {
this.devcycleClient = devcycleClient;
}
@Override
public Metadata getMetadata() {
return () -> PROVIDER_NAME + " " + devcycleClient.getSDKPlatform();
}
@Override
public ProviderEvaluation getBooleanEvaluation(String key, Boolean defaultValue, EvaluationContext ctx) {
return resolvePrimitiveVariable(key, defaultValue, ctx);
}
@Override
public ProviderEvaluation getStringEvaluation(String key, String defaultValue, EvaluationContext ctx) {
return resolvePrimitiveVariable(key, defaultValue, ctx);
}
@Override
public ProviderEvaluation getIntegerEvaluation(String key, Integer defaultValue, EvaluationContext ctx) {
ProviderEvaluation eval = resolvePrimitiveVariable(key, defaultValue, ctx);
return eval;
}
@Override
public ProviderEvaluation getDoubleEvaluation(String key, Double defaultValue, EvaluationContext ctx) {
return resolvePrimitiveVariable(key, defaultValue, ctx);
}
@Override
public ProviderEvaluation getObjectEvaluation(String key, Value defaultValue, EvaluationContext ctx) {
/*
* JSON objects have special rules in the DevCycle SDK and must be handled differently
* - must always be an object, no lists or literal values
* - must only contain strings, numbers, and booleans
*/
if (!defaultValue.isStructure()) {
throw new TypeMismatchError("Default value must be a OpenFeature structure");
}
for (String k : defaultValue.asStructure().keySet()) {
Value v = defaultValue.asStructure().getValue(k);
if (!(v.isString() || v.isNumber() || v.isBoolean() || v.isNull())) {
throw new TypeMismatchError("DevCycle JSON objects may only contain strings, numbers, booleans and nulls");
}
}
if (devcycleClient.isInitialized()) {
try {
DevCycleUser user = DevCycleUser.fromEvaluationContext(ctx);
Variable