-
Notifications
You must be signed in to change notification settings - Fork 60
feat: Add ConfigCat provider #521
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
Merged
Merged
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d174872
Add ConfigCat provider
liran2000 e2a9f7c
Add ConfigCat provider - cont.
liran2000 13d96d1
some updates
liran2000 53cef7f
Merge branch 'open-feature:main' into feature/configcat
liran2000 6f46c02
Merge branch 'open-feature:main' into feature/configcat
liran2000 575959a
update ConfigCat provider repo files
liran2000 c9b96e4
clean unused
liran2000 11a6232
clean unused - cont.
liran2000 4b255ba
clean unused - cont.
liran2000 44a3500
- some refactor
liran2000 9e355df
enrich tests
liran2000 05eca76
fix userId
liran2000 6082115
fix userId
liran2000 766c373
Merge branch 'main' into feature/configcat
liran2000 f45de04
Merge branch 'main' into feature/configcat
toddbaert a5f3fff
minor updates
liran2000 01dd5e1
Merge branch 'main' into feature/configcat
liran2000 1468f66
Merge branch 'main' into feature/configcat
toddbaert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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> |
156 changes: 156 additions & 0 deletions
156
...onfigcat/src/main/java/dev/openfeature/contrib/providers/configcat/ConfigCatProvider.java
This file contains hidden or 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,156 @@ | ||
| 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); | ||
toddbaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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())) | ||
toddbaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .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) { | ||
toddbaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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) { | ||
toddbaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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); | ||
toddbaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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()) | ||
| .errorMessage(evaluationDetails.getError()) | ||
toddbaert marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .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 hidden or 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 | ||
toddbaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
37 changes: 37 additions & 0 deletions
37
...nfigcat/src/main/java/dev/openfeature/contrib/providers/configcat/ContextTransformer.java
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.