OpenFeature Provider for Kotlin
ConfigCat OpenFeature Provider for Kotlin on GitHub
Getting started
1. Install the provider
dependencies {
implementation("com.configcat:configcat-openfeature-provider:$providerVersion")
}
2. Initialize the provider
The ConfigCatProvider
function takes the SDK key and an optional ConfigCatOptions
argument containing the additional configuration options for
the ConfigCat Kotlin SDK:
import com.configcat.*
import dev.openfeature.sdk.*
coroutineScope.launch(Dispatchers.IO) {
// Configure the provider.
val provider = ConfigCatProvider("<YOUR-CONFIGCAT-SDK-KEY>") {
pollingMode = autoPoll { pollingInterval = 60.seconds }
}
// Configure the OpenFeature API with the ConfigCat provider.
OpenFeatureAPI.setProviderAndWait(provider)
// Create a client.
val client = OpenFeatureAPI.getClient()
}
For more information about all the configuration options, see the Kotlin SDK documentation.
3. Evaluate your feature flag
val isAwesomeFeatureEnabled = client.getBooleanValue("isAwesomeFeatureEnabled", false)
if (isAwesomeFeatureEnabled) {
doTheNewThing()
} else {
doTheOldThing()
}
Evaluation Context
An evaluation context in the OpenFeature specification is a container for arbitrary contextual data that can be used as a basis for feature flag evaluation. The ConfigCat provider translates these evaluation contexts to ConfigCat User Objects.
The following table shows how the different context attributes are mapped to User Object attributes.
Evaluation context | User Object | Required |
---|---|---|
targetingKey | identifier | ☑ |
Email | email | |
Country | country | |
Any other | custom |
To evaluate feature flags for a context, use the setEvaluationContext()
method on the OpenFeatureAPI
to set the context for the provider:
val context = ImmutableContext(
targetingKey = "#SOME-USER-ID#",
attributes = mapOf(
"Email" to Value.String("[email protected]"),
"Country" to Value.String("CountryID"),
"Rating" to Value.Double(4.5),
"RegisteredAt" to Value.Date(Date.from(Instant.parse("2023-11-22T12:34:56+00:00"))),
"Roles" to Value.List(listOf(Value.String("Role1"), Value.String("Role2")))
),
)
OpenFeatureAPI.setEvaluationContext(context)