Python

Table of Contents

Installation

Install the provider and the OpenFeature Python SDK:

pip install openfeature-sdk hyphen-openfeature-provider

Setup and Initialization

To begin using the Hyphen Provider, follow these steps:

  1. Import the required modules

  2. Configure the provider with your publicKey, application name and environment.
    You can specify the environment in one of two formats:

    • Alternate ID (e.g., "production", "staging") — the environment in which your application is running.
    • Project Environment ID (e.g., pevr_abc123) — useful for internal references.
  3. Register the provider with OpenFeature and create an OpenFeature client for your application.

from openfeature import api
from openfeature_provider_hyphen import (
    HyphenProvider,
    HyphenProviderOptions,
    HyphenEvaluationContext
)

# Initialize provider options
options = HyphenProviderOptions(
    application="your-app-name",
    environment="production"
)

# Create and set the provider
provider = HyphenProvider(
    public_key="your-public-key",
    options=options
)
api.set_provider(provider)

# Create a client
client = api.get_client()
  1. Use HyphenEvaluationContext to configure the required context for feature targeting evaluations, incorporating user or application context.
from openfeature_provider_hyphen import HyphenUser

# Create user details
user = HyphenUser(
    id="user-123",
    email="[email protected]",
    name="John Doe",
    custom_attributes={
        "role": "admin",
        "subscription": "premium"
    }
)

# Create evaluation context
context = HyphenEvaluationContext(
    targeting_key="user-123",
    attributes={
        "user": user,
        "ip_address": "203.0.113.42",
        "custom_attributes": {
            "device": "mobile",
            "platform": "ios"
        }
    }
)

Usage

Evaluation Context Example

Use the client to evaluate different types of feature flags in your application.

try:
    # Boolean flag evaluation
    is_enabled = client.get_boolean_value(
        flag_key="your-flag-key",
        default_value=False,
        evaluation_context=context
    )
    print(f"Feature enabled: {is_enabled}")

    # String flag evaluation
    theme = client.get_string_value(
        flag_key="app-theme",
        default_value="light",
        evaluation_context=context
    )

    # Integer flag evaluation
    max_items = client.get_integer_value(
        flag_key="max-items",
        default_value=10,
        evaluation_context=context
    )

    # Object flag evaluation
    config = client.get_object_value(
        flag_key="feature-config",
        default_value={
            "enabled": True,
            "timeout": 30
        },
        evaluation_context=context
    )
except Exception as e:
    print(f"Error evaluating flag: {e}")

Configuration

Options

Option Type Required Description
application str Yes The application id or alternate ID.
environment str Yes The environment identifier for the Hyphen project (project environment ID or alternateId).
horizon_urls List[str] No Custom Hyphen server URLs.
enable_toggle_usage bool No Enable/disable telemetry (default: true).
cache_ttl_seconds int No Cache TTL in seconds (default: 300).
generate_cache_key_fn Callable No Custom cache key generation function.

Context

Field Type Required Description
targeting_key str Yes The key used for caching the evaluation response.
attributes Dict No Dictionary containing context attributes.
attributes.user HyphenUser No User information for targeting.
attributes.ip_address str No The IP address of the user.
attributes.custom_attributes Dict[str, Any] No Additional contextual information.

HyphenUser Fields

Field Type Required Description
id str Yes The unique identifier of the user
email str No The email address of the user
name str No The name of the user
custom_attributes Dict[str, Any] No Custom attributes specific to the user