-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: liran2000 <[email protected]>
- Loading branch information
Showing
15 changed files
with
668 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Unofficial ConfigCat OpenFeature Provider for Java | ||
|
||
[ConfigCat](https://configcat.com/) OpenFeature Provider can provide usage for ConfigCat via OpenFeature Java SDK. | ||
|
||
## Installation | ||
|
||
<!-- x-release-please-start-version --> | ||
|
||
```xml | ||
|
||
<dependency> | ||
<groupId>dev.openfeature.contrib.providers</groupId> | ||
<artifactId>configcat</artifactId> | ||
<version>0.0.1</version> | ||
</dependency> | ||
``` | ||
|
||
<!-- x-release-please-end-version --> | ||
|
||
## Usage | ||
ConfigCat OpenFeature Provider is using ConfigCat Java SDK. | ||
|
||
### Usage Example | ||
|
||
``` | ||
ConfigCatProviderConfig configCatProviderConfig = ConfigCatProviderConfig.builder().sdkKey(sdkKey).build(); | ||
configCatProvider = new ConfigCatProvider(configCatProviderConfig); | ||
OpenFeatureAPI.getInstance().setProviderAndWait(configCatProvider); | ||
boolean featureEnabled = client.getBooleanValue(FLAG_NAME, false); | ||
MutableContext evaluationContext = new MutableContext(); | ||
evaluationContext.setTargetingKey("[email protected]"); | ||
evaluationContext.add("Email", "[email protected]"); | ||
evaluationContext.add("Country", "someCountry"); | ||
featureEnabled = client.getBooleanValue(USERS_FLAG_NAME, false, evaluationContext); | ||
``` | ||
|
||
See [ConfigCatProviderTest.java](./src/test/java/dev/openfeature/contrib/providers/configcat/ConfigCatProviderTest.java) | ||
for more information. | ||
|
||
## Notes | ||
Some ConfigCat custom operations are supported from the provider client via: | ||
|
||
```java | ||
configCatProvider.getConfigCatClient()... | ||
``` | ||
|
||
## ConfigCat Provider Tests Strategies | ||
|
||
Unit test based on ConfigCat local features file. | ||
See [ConfigCatProviderTest.java](./src/test/java/dev/openfeature/contrib/providers/configcat/ConfigCatProviderTest.java) | ||
for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# This file is needed to avoid errors throw by findbugs when working with lombok. | ||
lombok.addSuppressWarnings = true | ||
lombok.addLombokGeneratedAnnotation = true | ||
config.stopBubbling = true | ||
lombok.extern.findbugs.addSuppressFBWarnings = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>dev.openfeature.contrib</groupId> | ||
<artifactId>parent</artifactId> | ||
<version>0.1.0</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
<groupId>dev.openfeature.contrib.providers</groupId> | ||
<artifactId>configcat</artifactId> | ||
<version>0.0.1</version> <!--x-release-please-version --> | ||
|
||
<name>configcat</name> | ||
<description>configcat provider for Java</description> | ||
<url>https://configcat.com/</url> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.configcat</groupId> | ||
<artifactId>configcat-java-client</artifactId> | ||
<version>8.3.0</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<version>2.0.9</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-slf4j2-impl</artifactId> | ||
<version>2.21.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
</project> |
155 changes: 155 additions & 0 deletions
155
...onfigcat/src/main/java/dev/openfeature/contrib/providers/configcat/ConfigCatProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package dev.openfeature.contrib.providers.configcat; | ||
|
||
import com.configcat.ConfigCatClient; | ||
import com.configcat.EvaluationDetails; | ||
import com.configcat.User; | ||
import dev.openfeature.sdk.EvaluationContext; | ||
import dev.openfeature.sdk.EventProvider; | ||
import dev.openfeature.sdk.Metadata; | ||
import dev.openfeature.sdk.ProviderEvaluation; | ||
import dev.openfeature.sdk.ProviderEventDetails; | ||
import dev.openfeature.sdk.ProviderState; | ||
import dev.openfeature.sdk.Value; | ||
import dev.openfeature.sdk.exceptions.GeneralError; | ||
import dev.openfeature.sdk.exceptions.ProviderNotReadyError; | ||
import lombok.Getter; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.ArrayList; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
/** | ||
* Provider implementation for ConfigCat. | ||
*/ | ||
@Slf4j | ||
public class ConfigCatProvider extends EventProvider { | ||
|
||
@Getter | ||
private static final String NAME = "ConfigCat"; | ||
|
||
public static final String PROVIDER_NOT_YET_INITIALIZED = "provider not yet initialized"; | ||
public static final String UNKNOWN_ERROR = "unknown error"; | ||
|
||
private ConfigCatProviderConfig configCatProviderConfig; | ||
|
||
@Getter | ||
private ConfigCatClient configCatClient; | ||
|
||
@Getter | ||
private ProviderState state = ProviderState.NOT_READY; | ||
|
||
private AtomicBoolean isInitialized = new AtomicBoolean(false); | ||
|
||
/** | ||
* Constructor. | ||
* @param configCatProviderConfig configCatProvider Config | ||
*/ | ||
public ConfigCatProvider(ConfigCatProviderConfig configCatProviderConfig) { | ||
this.configCatProviderConfig = configCatProviderConfig; | ||
} | ||
|
||
/** | ||
* Initialize the provider. | ||
* @param evaluationContext evaluation context | ||
* @throws Exception on error | ||
*/ | ||
@Override | ||
public void initialize(EvaluationContext evaluationContext) throws Exception { | ||
boolean initialized = isInitialized.getAndSet(true); | ||
if (initialized) { | ||
throw new GeneralError("already initialized"); | ||
} | ||
super.initialize(evaluationContext); | ||
configCatClient = ConfigCatClient.get(configCatProviderConfig.getSdkKey(), | ||
configCatProviderConfig.getOptions()); | ||
configCatProviderConfig.postInit(); | ||
state = ProviderState.READY; | ||
log.info("finished initializing provider, state: {}", state); | ||
|
||
configCatClient.getHooks().addOnConfigChanged(map -> { | ||
ProviderEventDetails providerEventDetails = ProviderEventDetails.builder() | ||
.flagsChanged(new ArrayList<>(map.keySet())) | ||
.message("config changed") | ||
.build(); | ||
emitProviderReady(providerEventDetails); | ||
}); | ||
|
||
configCatClient.getHooks().addOnError(errorMessage -> { | ||
ProviderEventDetails providerEventDetails = ProviderEventDetails.builder() | ||
.message(errorMessage) | ||
.build(); | ||
emitProviderError(providerEventDetails); | ||
}); | ||
} | ||
|
||
@Override | ||
public Metadata getMetadata() { | ||
return () -> NAME; | ||
} | ||
|
||
@Override | ||
public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defaultValue, EvaluationContext ctx) { | ||
return getEvaluation(Boolean.class, key, defaultValue, ctx); | ||
} | ||
|
||
@Override | ||
public ProviderEvaluation<String> getStringEvaluation(String key, String defaultValue, EvaluationContext ctx) { | ||
return getEvaluation(String.class, key, defaultValue, ctx); | ||
} | ||
|
||
@Override | ||
public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defaultValue, EvaluationContext ctx) { | ||
return getEvaluation(Integer.class, key, defaultValue, ctx); | ||
} | ||
|
||
@Override | ||
public ProviderEvaluation<Double> getDoubleEvaluation(String key, Double defaultValue, EvaluationContext ctx) { | ||
return getEvaluation(Double.class, key, defaultValue, ctx); | ||
} | ||
|
||
@SneakyThrows | ||
@Override | ||
public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultValue, EvaluationContext ctx) { | ||
return getEvaluation(Value.class, key, defaultValue, ctx); | ||
} | ||
|
||
private <T> ProviderEvaluation<T> getEvaluation(Class<T> classOfT, String key, T defaultValue, | ||
EvaluationContext ctx) { | ||
if (!ProviderState.READY.equals(state)) { | ||
if (ProviderState.NOT_READY.equals(state)) { | ||
throw new ProviderNotReadyError(PROVIDER_NOT_YET_INITIALIZED); | ||
} | ||
throw new GeneralError(UNKNOWN_ERROR); | ||
} | ||
User user = ctx == null ? null : ContextTransformer.transform(ctx); | ||
EvaluationDetails<T> evaluationDetails; | ||
T evaluatedValue; | ||
if (classOfT.isAssignableFrom(Value.class)) { | ||
String defaultValueStr = defaultValue == null ? null : ((Value)defaultValue).asString(); | ||
evaluationDetails = (EvaluationDetails<T>) configCatClient | ||
.getValueDetails(String.class, key, user, defaultValueStr); | ||
evaluatedValue = evaluationDetails.getValue() == null ? null : | ||
(T) Value.objectToValue(evaluationDetails.getValue()); | ||
} else { | ||
evaluationDetails = configCatClient | ||
.getValueDetails(classOfT, key, user, defaultValue); | ||
evaluatedValue = evaluationDetails.getValue(); | ||
} | ||
return ProviderEvaluation.<T>builder() | ||
.value(evaluatedValue) | ||
.variant(evaluationDetails.getVariationId()) | ||
.build(); | ||
} | ||
|
||
@SneakyThrows | ||
@Override | ||
public void shutdown() { | ||
super.shutdown(); | ||
log.info("shutdown"); | ||
if (configCatClient != null) { | ||
configCatClient.close(); | ||
} | ||
state = ProviderState.NOT_READY; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...at/src/main/java/dev/openfeature/contrib/providers/configcat/ConfigCatProviderConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package dev.openfeature.contrib.providers.configcat; | ||
|
||
import com.configcat.ConfigCatClient; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
import java.util.function.Consumer; | ||
|
||
|
||
/** | ||
* Options for initializing ConfigCat provider. | ||
*/ | ||
@Getter | ||
@Builder | ||
public class ConfigCatProviderConfig { | ||
private Consumer<ConfigCatClient.Options> options; | ||
|
||
// Only holding temporary for initialization | ||
private String sdkKey; | ||
|
||
public void postInit() { | ||
sdkKey = null; // for security, not holding key in memory for long-term | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...nfigcat/src/main/java/dev/openfeature/contrib/providers/configcat/ContextTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package dev.openfeature.contrib.providers.configcat; | ||
|
||
import com.configcat.User; | ||
import dev.openfeature.sdk.EvaluationContext; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Transformer from OpenFeature context to ConfigCat User. | ||
*/ | ||
public class ContextTransformer { | ||
|
||
public static final String CONTEXT_EMAIL = "Email"; | ||
public static final String CONTEXT_COUNTRY = "Country"; | ||
|
||
protected static User transform(EvaluationContext ctx) { | ||
User.Builder userBuilder = User.newBuilder(); | ||
Map<String, String> customMap = new HashMap<>(); | ||
ctx.asObjectMap().forEach((k, v) -> { | ||
switch (k) { | ||
case CONTEXT_COUNTRY: | ||
userBuilder.country(String.valueOf(v)); | ||
break; | ||
case CONTEXT_EMAIL: | ||
userBuilder.email(String.valueOf(v)); | ||
break; | ||
default: | ||
customMap.put(k, String.valueOf(v)); | ||
break; | ||
} | ||
}); | ||
userBuilder.custom(customMap); | ||
return userBuilder.build(ctx.getTargetingKey()); | ||
} | ||
|
||
} |
Oops, something went wrong.