Skip to main content

Official Python SDK for FeatureHub

Project description

Official FeatureHub Python SDK.

Overview

To control the feature flags from the FeatureHub Admin console, either use our demo version for evaluation or install the app using our guide here

Versions 3.7+ of Python are supported.

SDK installation

pip install featurehub-sdk

Options to get feature updates

There are 2 ways to request for feature updates via this SDK:

  • SSE (Server Sent Events) realtime updates mechanism

    In this mode, you will make a connection to the FeatureHub Edge server using the EventSource (based on urllib3's sseclient-py), and any updates to any features will come through in near 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.

Example

Check our example Flask app here

Quick start

Connecting to FeatureHub

There are 3 steps to connecting:

  1. Copy FeatureHub API Key from the FeatureHub Admin Console
  2. Create FeatureHub config
  3. Check FeatureHub Repository readiness and request feature state

1. API Key from the FeatureHub Admin Console

Find 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 this here

Client 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 an insecure client. (e.g. command line tool). This also means you evaluate one user per client.

2. Create FeatureHub config:

Create FeatureHubConfig. You need to provide the API Key and the URL of the FeatureHub Edge server.

from featurehub_sdk.featurehub_config import FeatureHubConfig

edge_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 sync

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)
# OR
config.use_polling_edge_service() # uses environment variable FEATUREHUB_POLL_INTERVAL or default of 30 

in this case it is configured for requesting an update every 30 seconds.

3. Check FeatureHub Repository readiness and request feature state

Check for FeatureHub Repository readiness:

 if config.repository().is_ready():
    # do something

If you are not intending to use rollout strategies, you can pass empty context to the SDK:

Synchronous function:

    def name_arg(name):
        if config.new_context().build_sync().feature('FEATURE_TITLE_TO_UPPERCASE').get_flag:
            return "HELLO WORLD"
        else:
            return "hello world"

Asynchronous function:

async def async_name_arg(name):
        ctx = await config.new_context().build()
        if ctx.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 active user context. In this example we pass user_key to the context :

Synchronous function:

    def name_arg(name):
        if config.new_context().user_key(name).build_sync().feature('FEATURE_TITLE_TO_UPPERCASE').get_flag:
            return "HELLO WORLD"
        else:
            return "hello world"

Asynchronous function:

async def async_name_arg(name):
        ctx = await config.new_context().user_key(name).build()
        if ctx.feature('FEATURE_TITLE_TO_UPPERCASE').get_flag:
            return "HELLO WORLD"
        else:
            return "hello world"

See more options to request feature states here

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

featurehub_sdk-1.1.0.tar.gz (19.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

featurehub_sdk-1.1.0-py3-none-any.whl (21.9 kB view details)

Uploaded Python 3

File details

Details for the file featurehub_sdk-1.1.0.tar.gz.

File metadata

  • Download URL: featurehub_sdk-1.1.0.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for featurehub_sdk-1.1.0.tar.gz
Algorithm Hash digest
SHA256 470b7b1baae7e7fd49307632e97b65e9c3f745c6440523df3928f5b69f844935
MD5 68e43f13f968720513ec66a3763d4182
BLAKE2b-256 6d266a6a6bff4384b18cb7a366ec9be1240fceb8e3f6f27b314418c2952232a5

See more details on using hashes here.

Provenance

The following attestation bundles were made for featurehub_sdk-1.1.0.tar.gz:

Publisher: python-publish.yml on featurehub-io/featurehub-python-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file featurehub_sdk-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: featurehub_sdk-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 21.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for featurehub_sdk-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0c2ab220489bf3c2d9df2a5e3a9c5a989408f11d7b23de37ec4d6d2cd6e5cbfb
MD5 498a1f258b68d6dc5b0f48f397f2900c
BLAKE2b-256 1abe1cc7d191ece6899fc4b39101d151711479e5205e5dfd821153f1b98c42ff

See more details on using hashes here.

Provenance

The following attestation bundles were made for featurehub_sdk-1.1.0-py3-none-any.whl:

Publisher: python-publish.yml on featurehub-io/featurehub-python-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page