Skip to main content

Python client for the Unleash feature toggle system!

Project description

unleash-client-python

Coverage Status PyPI version PyPI - Python Version License: MIT

This is the Python client for Unleash. It implements Client Specifications 1.0 and checks compliance based on spec in unleash/client-specifications

What it supports:

  • Default activation strategies using 32-bit Murmurhash3
  • Custom strategies
  • Full client lifecycle:
    • Client registers with Unleash server
    • Client periodically fetches feature toggles and stores to on-disk cache
    • Client periodically sends metrics to Unleash Server
  • Tested on Linux (Ubuntu), OSX, and Windows

Check out the project documentation and the changelog.

Installation

Check out the package on Pypi!

pip install UnleashClient

For Flask Users

If you're looking into running Unleash from Flask, you might want to take a look at Flask-Unleash, the Unleash Flask extension. The extension builds upon this SDK to reduce the amount of boilerplate code you need to write to integrate with Flask. Of course, if you'd rather use this package directly, that will work too.

Usage

Initialization

from UnleashClient import UnleashClient

client = UnleashClient(
    url="https://unleash.herokuapp.com",
    app_name="my-python-app",
    custom_headers={'Authorization': '<API token>'})

client.initialize_client()

For more information about configuring UnleashClient, check out the project reference docs!

Checking if a feature is enabled

A check of a simple toggle:

client.is_enabled("my_toggle")

To supply application context, use the second positional argument:

app_context = {"userId": "test@email.com"}
client.is_enabled("user_id_toggle", app_context)

Fallback function and default values

You can specify a fallback function for cases where the client doesn't recognize the toggle by using the fallback_function keyword argument:

def custom_fallback(feature_name: str, context: dict) -> bool:
    return True

client.is_enabled("my_toggle", fallback_function=custom_fallback)

You can also use the fallback_function argument to replace the obsolete default_value keyword argument by using a lambda that ignores its inputs. Whatever the lambda returns will be used as the default value.

client.is_enabled("my_toggle", fallback_function=lambda feature_name, context: True)

The fallback function must accept the feature name and context as positional arguments in that order.

The client will evaluate the fallback function only if an exception occurs when calling the is_enabled() method. This happens when the client can't find the feature flag. The client may also throw other, general exceptions.

For more information about usage, see the Usage documentation.

Getting a variant

Checking for a variant:

context = {'userId': '2'}  # Context must have userId, sessionId, or remoteAddr.  If none are present, distribution will be random.

variant = client.get_variant("variant_toggle", context)

print(variant)
> {
>    "name": "variant1",
>    "payload": {
>        "type": "string",
>        "value": "val1"
>        },
>    "enabled": True
> }

For more information about variants, see the Variant documentation.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

UnleashClient-5.10.0.tar.gz (63.1 kB view details)

Uploaded Source

Built Distribution

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

UnleashClient-5.10.0-py3-none-any.whl (75.1 kB view details)

Uploaded Python 3

File details

Details for the file UnleashClient-5.10.0.tar.gz.

File metadata

  • Download URL: UnleashClient-5.10.0.tar.gz
  • Upload date:
  • Size: 63.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.13

File hashes

Hashes for UnleashClient-5.10.0.tar.gz
Algorithm Hash digest
SHA256 413eeeacfd040c4145000ada4544c50cabe7be22d2239af02e79b5f31728d72f
MD5 54d4c07a5f428f01f15fdb36f34a86f4
BLAKE2b-256 6f724b02bc14bcd1b1a050f7b75d989e63eb049fe834c94a843e3e139fe52d34

See more details on using hashes here.

File details

Details for the file UnleashClient-5.10.0-py3-none-any.whl.

File metadata

  • Download URL: UnleashClient-5.10.0-py3-none-any.whl
  • Upload date:
  • Size: 75.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.13

File hashes

Hashes for UnleashClient-5.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7779301d6e0dad464e9cd7c3d39207869a791d39f0072882395aed9a5653615b
MD5 a1d8c02abe5cc38d955aed5bfe43fd1b
BLAKE2b-256 e45fb9e999319ee55148859f19378c04a39329766e7e1f954726df6f45ab0538

See more details on using hashes here.

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