Skip to content

Commit

Permalink
formatting and code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-hoefgen committed Oct 24, 2023
1 parent 895d01e commit 6986077
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 89 deletions.
3 changes: 1 addition & 2 deletions src/examples/java/com/devcycle/examples/CloudExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.devcycle.sdk.server.cloud.api.DevCycleCloudClient;
import com.devcycle.sdk.server.cloud.model.DevCycleCloudOptions;
import com.devcycle.sdk.server.common.exception.DevCycleException;
import com.devcycle.sdk.server.common.model.DevCycleUser;

public class CloudExample {
Expand Down Expand Up @@ -34,7 +33,7 @@ public static void main(String[] args) throws InterruptedException {
Boolean variableValue = false;
try {
variableValue = client.variableValue(user, VARIABLE_KEY, defaultValue);
} catch(IllegalArgumentException e) {
} catch (IllegalArgumentException e) {
System.err.println("Error fetching variable value: " + e.getMessage());
System.exit(1);
}
Expand Down
4 changes: 2 additions & 2 deletions src/examples/java/com/devcycle/examples/LocalExample.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.devcycle.examples;

import com.devcycle.sdk.server.common.model.DevCycleUser;
import com.devcycle.sdk.server.local.api.DevCycleLocalClient;
import com.devcycle.sdk.server.local.model.DevCycleLocalOptions;
import com.devcycle.sdk.server.common.model.DevCycleUser;

public class LocalExample {
public static String VARIABLE_KEY = "test-boolean-variable";
Expand All @@ -29,7 +29,7 @@ public static void main(String[] args) throws InterruptedException {
DevCycleLocalClient client = new DevCycleLocalClient(server_sdk_key, options);

for (int i = 0; i < 10; i++) {
if(client.isInitialized()) {
if (client.isInitialized()) {
break;
}
Thread.sleep(500);
Expand Down
18 changes: 10 additions & 8 deletions src/examples/java/com/devcycle/examples/OpenFeatureExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,37 @@ public static void main(String[] args) throws InterruptedException {
System.exit(1);
}

DevCycleLocalOptions options = DevCycleLocalOptions.builder().configPollingIntervalMs(60000)
DevCycleLocalOptions options = DevCycleLocalOptions.builder().configPollingIntervalMS(60000)
.disableAutomaticEventLogging(false).disableCustomEventLogging(false).build();

// Initialize DevCycle Client
DevCycleLocalClient devCycleClient = new DevCycleLocalClient(server_sdk_key, options);

OpenFeatureAPI api = OpenFeatureAPI.getInstance();

for (int i = 0; i < 10; i++) {
if(devCycleClient.isInitialized()) {
if (devCycleClient.isInitialized()) {
break;
}
Thread.sleep(500);
}

Map<String, Value> apiAttrs = new LinkedHashMap();
// Setup OpenFeature with the DevCycle Provider
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
api.setProvider(new DevCycleProvider(devCycleClient));

// Create the evaluation context to use for fetching variable values
Map<String, Value> apiAttrs = new LinkedHashMap<>();
apiAttrs.put("email", new Value("[email protected]"));
apiAttrs.put("country", new Value("US"));

EvaluationContext ctx = new ImmutableContext("test-1234", apiAttrs);
EvaluationContext context = new ImmutableContext("test-1234", apiAttrs);

// The default value can be of type string, boolean, number, or JSON
Boolean defaultValue = false;

api.setProvider(new DevCycleProvider(devCycleClient));

// Fetch variable values using the identifier key, with a default value and user
// object. The default value can be of type string, boolean, number, or JSON
Boolean variableValue = api.getClient().getBooleanValue(VARIABLE_KEY, defaultValue, ctx);
Boolean variableValue = api.getClient().getBooleanValue(VARIABLE_KEY, defaultValue, context);

// Use variable value
if (variableValue) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
package com.devcycle.sdk.server.common.api;

import com.devcycle.sdk.server.common.exception.DevCycleException;
import com.devcycle.sdk.server.common.model.DevCycleUser;
import com.devcycle.sdk.server.common.model.Variable;

/**
* Base interface for DevCycle clients that can be used to evaluate Features and retrieve variables values.
*/
public interface IDevCycleClient {
public boolean isInitialized();
/**
* @return true if the client is initialized and ready to be used. Clients should
* return a default value if they are not initialized.
*/
boolean isInitialized();

public <T> T variableValue(DevCycleUser user, String key, T defaultValue);
/**
* @param user (required) The user context for the evaluation.
* @param key (required) The key of the feature variable to evaluate.
* @param defaultValue (required) The default value to return if the feature variable is not found or the user
* does not segment into the feature
* @return the value of the variable for the given user, or the default value if the variable is not found.
*/
<T> T variableValue(DevCycleUser user, String key, T defaultValue);

public <T> Variable<T> variable(DevCycleUser user, String key, T defaultValue);
/**
* @param user (required) The user context for the evaluation.
* @param key (required) The key of the feature variable to evaluate.
* @param defaultValue (required) The default value to return if the feature variable is not found or the user
* does not segment into the feature
* @return the variable for the given user, or the default variable if the variable is not found.
*/
<T> Variable<T> variable(DevCycleUser user, String key, T defaultValue);

public void close();
/**
* Close the client and release any resources.
*/
void close();
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.devcycle.sdk.server.openfeature;

import dev.openfeature.sdk.*;
import com.devcycle.sdk.server.common.api.IDevCycleClient;
import com.devcycle.sdk.server.common.model.*;
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;

public class DevCycleProvider implements FeatureProvider
{
public class DevCycleProvider implements FeatureProvider {
private static final String PROVIDER_NAME = "DevCycleProvider";

private final IDevCycleClient devcycleClient;
Expand Down Expand Up @@ -51,13 +51,13 @@ public void shutdown() {
}

<T> ProviderEvaluation<T> resolve(String key, T defaultValue, EvaluationContext ctx) {
if(devcycleClient.isInitialized()) {
if (devcycleClient.isInitialized()) {
try {
DevCycleUser user = UserFactory.createUser(ctx);
DevCycleUser user = DevCycleUserFactory.createUser(ctx);

Variable<T> variable = devcycleClient.variable(user, key, defaultValue);

if(variable == null || variable.getIsDefaulted()) {
if (variable == null || variable.getIsDefaulted()) {
return ProviderEvaluation.<T>builder()
.value(defaultValue)
.reason(Reason.DEFAULT.toString())
Expand All @@ -76,8 +76,7 @@ <T> ProviderEvaluation<T> resolve(String key, T defaultValue, EvaluationContext
.errorMessage(e.getMessage())
.build();
}
}
else {
} else {
throw new ProviderNotReadyError("DevCycle client not initialized");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,100 +9,81 @@
import java.util.LinkedHashMap;
import java.util.Map;

public class UserFactory {
/**
* Utility class for creating a DevCycleUser from an EvaluationContext
*/
class DevCycleUserFactory {
static void setCustomValue(Map<String, Object> customData, String key, Value value) {
// Only support boolean, number, and string types for custom data values
// ignore all other data
if(customData != null && key != null && value != null)
{
if(value.isBoolean()){
if (customData != null && key != null && value != null) {
if (value.isBoolean()) {
customData.put(key, value.asBoolean());
}
else if(value.isNumber())
{
} else if (value.isNumber()) {
customData.put(key, value.asDouble());
}
else if(value.isString())
{
} else if (value.isString()) {
customData.put(key, value.asString());
}
}
}

public static DevCycleUser createUser(EvaluationContext ctx) {
static DevCycleUser createUser(EvaluationContext ctx) {
String userId = "";

if (ctx != null && ctx.getTargetingKey() != null) {
userId = ctx.getTargetingKey();
}
else if(ctx != null && ctx.getValue("user_id") != null) {
} else if (ctx != null && ctx.getValue("user_id") != null) {
userId = ctx.getValue("user_id").asString();
}

if(userId == null || userId.isEmpty()) {
if (userId == null || userId.isEmpty()) {
throw new TargetingKeyMissingError();
}

DevCycleUser user = DevCycleUser.builder().userId(userId).build();

Map<String,Object> customData = new LinkedHashMap();
Map<String,Object> privateCustomData = new LinkedHashMap();
Map<String, Object> customData = new LinkedHashMap<>();
Map<String, Object> privateCustomData = new LinkedHashMap<>();

for(String key : ctx.keySet()) {
if(key.equals("user_id")) {
for (String key : ctx.keySet()) {
if (key.equals("user_id")) {
continue;
}

Value value = ctx.getValue(key);

if(key == "email" && value.isString())
{
if (key.equals("email") && value.isString()) {
user.setEmail(value.asString());
}
else if(key == "name" && value.isString())
{
} else if (key.equals("name") && value.isString()) {
user.setName(value.asString());
}
else if(key == "language" && value.isString())
{
} else if (key.equals("language") && value.isString()) {
user.setLanguage(value.asString());
}
else if(key == "country" && value.isString())
{
} else if (key.equals("country") && value.isString()) {
user.setCountry(value.asString());
}
else if(key == "appVersion" && value.isString())
{
} else if (key.equals("appVersion") && value.isString()) {
user.setAppVersion(value.asString());
}
else if(key == "appBuild" && value.isString())
{
} else if (key.equals("appBuild") && value.isString()) {
user.setAppBuild(value.asString());
}
else if(key == "customData" && value.isStructure())
{
} else if (key.equals("customData") && value.isStructure()) {
Structure customDataStructure = value.asStructure();
for(String dataKey : customDataStructure.keySet()) {
for (String dataKey : customDataStructure.keySet()) {
setCustomValue(customData, dataKey, customDataStructure.getValue(dataKey));
}
}
else if(key == "privateCustomData" && value.isStructure())
{
} else if (key.equals("privateCustomData") && value.isStructure()) {
Structure privateDataStructure = value.asStructure();
for(String dataKey : privateDataStructure.keySet()) {
for (String dataKey : privateDataStructure.keySet()) {
setCustomValue(privateCustomData, dataKey, privateDataStructure.getValue(dataKey));
}
}
else {
} else {
setCustomValue(customData, key, value);
}
}

if(customData.size() > 0){
if (!customData.isEmpty()) {
user.setCustomData(customData);
}

if(privateCustomData.size() > 0){
if (!privateCustomData.isEmpty()) {
user.setPrivateCustomData(privateCustomData);
}

Expand Down
Loading

0 comments on commit 6986077

Please sign in to comment.