This SDK provides a Python implementation of the OpenFeature Provider interface.
See the example app for a working example using DevCycle Python SDK OpenFeature Provider.
See our documentation for more information.
Instantiate and configure the DevCycle SDK client first, either DevCycleLocalClient
or DevCycleCloudClient
.
Once the DevCycle client is configured, call the devcycle_client.get_openfeature_provider()
function to obtain the OpenFeature provider.
from openfeature import api
from openfeature.evaluation_context import EvaluationContext
from devcycle_python_sdk import DevCycleLocalClient, DevCycleLocalOptions
# Initialize the DevCycle SDK
devcycle_client = DevCycleLocalClient("DEVCYCLE_SERVER_SDK_KEY", DevCycleLocalOptions())
# Set the initialzed DevCycle client as the provider for OpenFeature
api.set_provider(devcycle_client.get_openfeature_provider())
# Get the OpenFeature client
open_feature_client = api.get_client()
# Set the global context for the OpenFeature client, you can use "targetingKey" or "user_id"
# This can also be done on a request basis instead
api.set_evaluation_context(EvaluationContext(targeting_key="test-1234"))
# Retrieve a boolean flag from the OpenFeature client
bool_flag = open_feature_client.get_boolean_value("bool-flag", False)
For DevCycle SDK to work we require either a targeting_key
or user_id
attribute to be set on the OpenFeature context.
This value is used to identify the user as the user_id
property for a DevCycleUser
in DevCycle.
The provider will automatically translate known DevCycleUser
properties from the OpenFeature context to the DevCycleUser
object for use in targeting and segmentation.
For example all these properties will be set on the DevCycleUser
:
context = EvaluationContext(targeting_key="test-1234", attributes={
"email": "[email protected]",
"name": "Test User",
"language": "en",
"country": "CA",
"appVersion": "1.0.11",
"appBuild": 1,
"customData": {"custom": "data"},
"privateCustomData": {"private": "data"}
})
Context attributes that do not map to known DevCycleUser
properties will be automatically
added to the customData
dictionary of the DevCycleUser
object.
DevCycle allows the following data types for custom data values: boolean, integer, float, and str. Other data types will be ignored
DevCycle only supports flat JSON Object properties used in the Context. Non-flat properties will be ignored.
For example obj
will be ignored:
context = EvaluationContext(targeting_key="test-1234", attributes={
"obj": { "key": "value" }
})
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", ["array"])
open_feature_client.get_object_value("json-flag", 610)
open_feature_client.get_object_value("json-flag", false)
open_feature_client.get_object_value("json-flag", "string")
open_feature_client.get_object_value("json-flag", None)
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" })