FeatureHub is a cloud-native platform that helps software teams manage their features, from feature flags to A/B experiments and remote or centralized configuration. In this tutorial, we will learn how to use the targeting by country rule and implementation code with the Python FeatureHub SDK.
Before we begin, make sure you have the following:
Python 3.x installed
FeatureHub either self-hosted or FeatureHub SaaS cloud version
Tip #1 - Install the FeatureHub Python SDK
To control the feature flags from the FeatureHub Admin console, we need to install the FeatureHub Python SDK. We can install it using pip:
pip install featurehub-sdk
Tip #2 - Create a Feature in the FeatureHub Admin Console
Next, we need to create a feature in the FeatureHub Admin Console. Follow these steps:
Log in to your FeatureHub account.
Make sure you have an application and environment.
Create a new feature and give it a name, e.g. FEATURE_UPPERCASE.
Add a targeting rule for the country. You can choose one or more countries to target. In this example we target single country - United States.
Once you added the targeting rule, set the rollout strategy value to "ON" and set default value to "OFF". By doing this, we can achieve our goal of targeting only users whose country is United States and turn the feature "ON" for them. For the rest of the users, the feature will be in "OFF" state, as this is our default value.
This is how you feature setup should look like on the dashboard
Tip #3 - Implement the Feature in Your Python Code
Now that we have created a feature with a targeting rule, we can implement it in our Python code. Here's an example:
from featurehub_sdk.featurehub_config import FeatureHubConfig # Replace edge_url depending on where your edge server is edge_url = 'http://localhost:8085/' # Replace <API_KEY> with your FeatureHub API key client_eval_key = '<API_KEY>' config = FeatureHubConfig(edge_url, [client_eval_key]) # By 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) asyncio.run(config.init()) # run async command in sync # Check for FeatureHub Repository readiness: if config.repository().is_ready(): # do something # If you are using rollout strategies and targeting rules they are # all determined by the active user context. In this example we # pass user_key to the context : def name_arg(country_enum_name): if config.new_context().country(country_enum_name).build_sync().feature('FEATURE_UPPERCASE').get_flag: return "HELLO WORLD" else: return "hello world" # Or use asynchronously: async def async_name_arg(country_enum_name): ctx = await config.new_context().country(country_enum_name).build() if ctx.feature('FEATURE_UPPERCASE').get_flag: return "HELLO WORLD" else: return "hello world"
In this example, we create a FeatureHubConfig object with our API key and check for Feature Repository readiness. We then get the feature with the specified key and check if it is enabled for the targeted country. If it is enabled, we can add our code to execute. If it is disabled, we can add our code to handle the disabled feature.
In this tutorial, we learned how to use the targeting by country rule and implementation code with the Python FeatureHub SDK. We installed the SDK, created a feature with a targeting rule in the FeatureHub Admin Console, and implemented the feature in our Python code. With this knowledge, you can now use FeatureHub to manage your features and target them to specific countries.