Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local Client Server Key verification #44

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.devcycle.sdk.server.common.api.IDVCApi;
import com.devcycle.sdk.server.common.exception.DVCException;
import com.devcycle.sdk.server.common.model.*;
import com.devcycle.sdk.server.common.model.Variable.TypeEnum;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -64,18 +65,21 @@ public <T> Variable<T> variable(User user, String key, T defaultValue) {
throw new IllegalArgumentException("defaultValue cannot be null");
}

TypeEnum variableType = TypeEnum.fromClass(defaultValue.getClass());
Variable<T> variable;

try {
Call<Variable> response = api.getVariableByKey(user, key, dvcOptions.getEnableEdgeDB());
variable = getResponse(response);
variable.setIsDefaulted(false);
} catch (Exception exception) {
variable = (Variable<T>) Variable.builder()
.key(key)
.value(defaultValue)
.isDefaulted(true)
.reasonUsingDefaultValue(exception.getMessage())
.build();
.key(key)
.type(variableType)
.value(defaultValue)
.defaultValue(defaultValue)
.isDefaulted(true)
.build();
}
return variable;
}
Expand All @@ -98,18 +102,21 @@ public Map<String, Variable> allVariables(User user) throws DVCException {
*
* @param user (required)
* @param event (required)
* @return DVCResponse
*/
public DVCResponse track(User user, Event event) throws DVCException {
public void track(User user, Event event) throws DVCException {
validateUser(user);

if (event == null || event.getType().equals("")) {
throw new IllegalArgumentException("Invalid Event");
}

UserAndEvents userAndEvents = UserAndEvents.builder()
.user(user)
.events(Collections.singletonList(event))
.build();

Call<DVCResponse> response = api.track(userAndEvents, dvcOptions.getEnableEdgeDB());
return getResponse(response);
getResponse(response);
}

private <T> T getResponse(Call<T> call) throws DVCException {
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/com/devcycle/sdk/server/common/model/Variable.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package com.devcycle.sdk.server.common.model;

import java.util.LinkedHashMap;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -29,7 +30,8 @@
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Variable<T> {
@Schema(required = true, description = "unique database id")
@Schema(required = false, description = "unique database id")
@JsonIgnore
@JsonProperty("_id")
private String id;

Expand All @@ -42,13 +44,12 @@ public class Variable<T> {
@Schema(required = true, description = "Variable type")
private TypeEnum type;

@JsonIgnore
@Schema(required = true, description = "Variable default value")
private T defaultValue;

@Builder.Default
private Boolean isDefaulted = false;

@JsonIgnore
private String reasonUsingDefaultValue;

public enum TypeEnum {
STRING("String"),
BOOLEAN("Boolean"),
Expand All @@ -71,13 +72,18 @@ public String toString() {
return String.valueOf(value);
}

public static TypeEnum fromValue(String text) {
for (TypeEnum b : TypeEnum.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
public static TypeEnum fromClass(Class<?> clazz) {
if (clazz == LinkedHashMap.class) {
return JSON;
} else if (clazz == Boolean.class) {
return BOOLEAN;
} else if (clazz == Integer.class || clazz == Double.class || clazz == Float.class) {
return NUMBER;
} else if (clazz == String.class) {
return STRING;
} else {
return null;
}
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Map;

import com.devcycle.sdk.server.common.model.*;
import com.devcycle.sdk.server.common.model.Variable.TypeEnum;
import com.devcycle.sdk.server.local.bucketing.LocalBucketing;
import com.devcycle.sdk.server.local.managers.EnvironmentConfigManager;
import com.devcycle.sdk.server.local.managers.EventQueueManager;
Expand All @@ -28,6 +29,13 @@ public DVCLocalClient(String serverKey) {
}

public DVCLocalClient(String serverKey, DVCLocalOptions dvcOptions) {
if(serverKey == null || serverKey.equals("")) {
throw new IllegalArgumentException("Missing sdk key! Call initialize with a valid sdk key");
}
if(!isValidServerKey(serverKey)) {
throw new IllegalArgumentException("Invalid sdk key provided. Please call initialize with a valid server sdk key");
}

localBucketing.setPlatformData(PlatformData.builder().build().toString());

configManager = new EnvironmentConfigManager(serverKey, localBucketing, dvcOptions);
Expand Down Expand Up @@ -82,17 +90,24 @@ public <T> Variable<T> variable(User user, String key, T defaultValue) {
System.out.println("Variable called before DVCClient has initialized, returning default value");
}

TypeEnum variableType = TypeEnum.fromClass(defaultValue.getClass());
Variable<T> defaultVariable = (Variable<T>) Variable.builder()
.key(key)
.type(variableType)
.value(defaultValue)
.defaultValue(defaultValue)
.isDefaulted(true)
.reasonUsingDefaultValue("Variable not found")
.build();

if (!isInitialized) {
return defaultVariable;
}

try {
BucketedUserConfig bucketedUserConfig = localBucketing.generateBucketedConfig(serverKey, user);
if (bucketedUserConfig.variables.containsKey(key)) {
Variable<T> variable = bucketedUserConfig.variables.get(key);
variable.setDefaultValue(defaultValue);
variable.setIsDefaulted(false);
eventQueueManager.queueAggregateEvent(Event.builder().type("aggVariableEvaluated").target(key).build(), bucketedUserConfig);
return variable;
Expand Down Expand Up @@ -139,6 +154,10 @@ public Map<String, Variable> allVariables(User user) {
public void track(User user, Event event) {
validateUser(user);

if (event == null || event.getType().equals("")) {
throw new IllegalArgumentException("Invalid Event");
}

try {
eventQueueManager.queueEvent(user, event);
} catch (Exception e) {
Expand Down Expand Up @@ -166,4 +185,8 @@ private void validateUser(User user) {
throw new IllegalArgumentException("userId cannot be empty");
}
}

private boolean isValidServerKey(String serverKey) {
return serverKey.startsWith("server") || serverKey.startsWith("dvc_server");
}
}