Package: featurehub-sdk
Official FeatureHub Python SDK.OverviewTo control the feature flags from the FeatureHub Admin console, either use ourdemoversion for evaluation or install the app using our guidehereVersions 3.7+ of Python are supported.SDK installationpip install featurehub-sdkOptions to get feature updatesThere are 2 ways to request for feature updates via this SDK:SSE (Server Sent Events) realtime updates mechanismIn this mode, you will make a connection to the FeatureHub Edge server using the EventSource (based on urllib3'ssseclient-py), and any updates to any features will come through innear realtime, automatically updating the feature values in the repository. This method is recommended for server applications.FeatureHub polling client (GET request updates)In this mode you can set an interval (from 0 - just once) to any number of seconds between polling. This is more useful for when you have short term single threaded
processes like command line tools. Batch tools that iterate over data sets and wish to control when updates happen can also benefit from this method.ExampleCheck our example Flask apphereQuick startConnecting to FeatureHubThere are 3 steps to connecting:Copy FeatureHub API Key from the FeatureHub Admin ConsoleCreate FeatureHub configCheck FeatureHub Repository readiness and request feature state1. API Key from the FeatureHub Admin ConsoleFind and copy your API Key from the FeatureHub Admin Console on the API Keys page -
you will use this in your code to configure feature updates for your environments.
It should look similar to this:default/71ed3c04-122b-4312-9ea8-06b2b8d6ceac/fsTmCrcZZoGyl56kPHxfKAkbHrJ7xZMKO3dlBiab5IqUXjgKvqpjxYdI8zdXiJqYCpv92Jrki0jY5taE.
There are two options - a Server Evaluated API Key and a Client Evaluated API Key. More on thishereClient Side evaluation is intended for use in secure environments (such as microservices) and is intended for rapid client side evaluation, per request for example.Server Side evaluation is more suitable when you are using aninsecure client. (e.g. command line tool). This also means you evaluate one user per client.2. Create FeatureHub config:CreateFeatureHubConfig. You need to provide the API Key and the URL of the FeatureHub Edge server.fromfeaturehub_sdk.featurehub_configimportFeatureHubConfigedge_url='http://localhost:8085/'client_eval_key='default/3f7a1a34-642b-4054-a82f-1ca2d14633ed/aH0l9TDXzauYq6rKQzVUPwbzmzGRqe*oPqyYqhUlVC50RxAzSmx'config=FeatureHubConfig(edge_url,[client_eval_key])asyncio.run(config.init())# run async command in syncBy default, this SDK will use SSE client. If you decide to use FeatureHub polling client, after initialising the config, you can add this:config.use_polling_edge_service(30)# ORconfig.use_polling_edge_service()# uses environment variable FEATUREHUB_POLL_INTERVAL or default of 30in this case it is configured for requesting an update every 30 seconds.3. Check FeatureHub Repository readiness and request feature stateCheck for FeatureHub Repository readiness:ifconfig.repository().is_ready():# do somethingIf you are not intending to use rollout strategies, you can pass empty context to the SDK:Synchronous function:defname_arg(name):ifconfig.new_context().build_sync().feature('FEATURE_TITLE_TO_UPPERCASE').get_flag:return"HELLO WORLD"else:return"hello world"Asynchronous function:asyncdefasync_name_arg(name):ctx=awaitconfig.new_context().build()ifctx.feature('FEATURE_TITLE_TO_UPPERCASE').get_flag:return"HELLO WORLD"else:return"hello world"If you are using rollout strategies and targeting rules they are all determined by the activeuser context. In this example we passuser_keyto the context :Synchronous function:defname_arg(name):ifconfig.new_context().user_key(name).build_sync().feature('FEATURE_TITLE_TO_UPPERCASE').get_flag:return"HELLO WORLD"else:return"hello world"Asynchronous function:asyncdefasync_name_arg(name):ctx=awaitconfig.new_context().user_key(name).build()ifctx.feature('FEATURE_TITLE_TO_UPPERCASE').get_flag:return"HELLO WORLD"else:return"hello world"See more options to request feature stateshere