OpenFeature Provider
OpenFeature is an open standard that provides a vendor-agnostic, community-driven API for feature flagging that works with DevCycle.
DevCycle provides a Python implementation of theOpenFeature Provider interface, if you prefer to use the OpenFeature API.
Usage
Installation
Install the OpenFeature Python SDK and DevCycle Provider:
$ pip install devcycle-python-server-sdk
(you may need to runpip
with root permission:sudo pip install devcycle-python-server-sdk
)
Getting Started
Initialize the DevCycle SDK and set the DevCycleProvider as the provider for OpenFeature:
from devcycle_python_sdkimport DevCycleLocalClient, DevCycleLocalOptions
from devcycle_python_sdk.models.userimport DevCycleUser
from openfeatureimport api
from openfeature.evaluation_contextimport EvaluationContext
import os
# Create an options object to do custom configurations, or use the defaults
options= DevCycleLocalOptions()
# create an instance of the DevCycleLocalClient class
devcycle_client= DevCycleLocalClient(os.environ["DEVCYCLE_SERVER_SDK_KEY"], options)
# set the provider for OpenFeature
api.set_provider(devcycle_client.get_openfeature_provider())
# get the OpenFeature client
open_feature_client= api.get_client()
Evaluate a Variable
Use a Variable value by setting the EvaluationContext, then passing the Variable key and default value to one of the OpenFeature flag evaluation methods.
context= EvaluationContext(
targeting_key="test-1234",
attributes={
"email":"[email protected]",
"name":"Test User",
},
)
open_feature_client.context= context
flag_value= client.get_boolean_value("boolean_flag",False)
Required Targeting Key
For the DevCycle SDK to work we require either atargeting_key
oruser_id
to be set on the OpenFeature context.This is used to identify the user as theuser_id
for aDevCycleUser
in DevCycle.
Context properties to DevCycleUser
The provider will automatically translate knownDevCycleUser
properties from the OpenFeature context to theDevCycleUser
object.DevCycleUser Python Interface
For example all these properties will be set on theDevCycleUser
:
# Pass context when querying values from the OpenFeature client
context= EvaluationContext(
targeting_key="test-1234",
attributes={
"email":"[email protected]",
"name":"Test User",
"language":"en",
"country":"CA",
"appVersion":"1.0.11",
"appBuild":1000,
"deviceModel":"Macbook",
"customData":{"custom":"data"},
"privateCustomData":{"private":"data"},
},
)
# Or set on the client with:
open_feature_client.context= context
Context properties that are not knownDevCycleUser
properties will be automaticallyadded to theCustomData
property of theDevCycleUser
.
Context Limitations
DevCycle only supports flat JSON Object properties used in the Context. Non-flat properties will be ignored.
For exampleobj
will be ignored:
context= EvaluationContext(
targeting_key="test-1234",
attributes={
"obj":{"key":"value"},
},
)
openfeature.NewEvaluationContext(
"user_id",
map[string]interface{}{
"obj":map[string]interface{}{"key":"value"},
},
)
JSON Flag Limitations
The OpenFeature spec for JSON flags allows for any type of valid JSON value to be set as the flag value.
For example the following are all valid default value types to use with OpenFeature:
# Invalid JSON values for the DevCycle SDK, will return defaults
open_feature_client.get_object_value("json-flag",["list"], context)
open_feature_client.get_object_value("json-flag",610, context)
open_feature_client.get_object_value("json-flag",False, context)
open_feature_client.get_object_value("json-flag","string", context)
open_feature_client.get_object_value("json-flag",None, context)
However, these are not valid types for the DevCycle SDK, the DevCycle SDK only supports JSON Objects:
# Valid JSON Object as the default value, will be evaluated by the DevCycle SDK
open_feature_client.get_object_value("json-flag",{"default":"value"}, context)