Harness Feature Flags Python SDK

1.7.5 · active · verified Wed Apr 15

The `harness-featureflags` library provides the Python server SDK for Harness Feature Flags. It allows developers to integrate feature flag management into their Python applications, enabling dynamic control over features without redeploying code. The library is actively maintained, with frequent patch and minor releases, and is currently at version 1.7.5.

Warnings

Install

Imports

Quickstart

This quickstart code demonstrates how to initialize the Harness Feature Flags Python SDK, define a target, and evaluate boolean and string feature flags. It continuously checks flag variations and prints their status until interrupted. Remember to replace `FF_API_KEY` with your actual Server SDK Key and update flag identifiers as needed. The SDK handles connection, caching, and updates automatically.

import os
import time
from harness_featureflags.client import CfClient
from harness_featureflags.util import Target

# Set your Harness Server SDK Key as an environment variable or replace directly
API_KEY = os.environ.get('FF_API_KEY', 'YOUR_HAARNESS_SDK_KEY')
# Set a target identifier for your user or application
TARGET_IDENTIFIER = os.environ.get('FF_TARGET_IDENTIFIER', 'test-user-1')

if not API_KEY or API_KEY == 'YOUR_HAARNESS_SDK_KEY':
    print("Error: FF_API_KEY not set. Please set the environment variable or replace the placeholder.")
    exit(1)

def main():
    client = CfClient(API_KEY)
    
    # Wait for the SDK to initialize
    client.wait_for_initialization()

    target = Target(
        identifier=TARGET_IDENTIFIER,
        name=TARGET_IDENTIFIER,
        attributes={
            "email": f"{TARGET_IDENTIFIER}@example.com",
            "country": "US"
        }
    )

    print(f"Client initialized. Target: {target.identifier}")

    try:
        while True:
            # Replace 'harnessappdemodarkmode' with your actual feature flag identifier
            is_dark_mode_enabled = client.bool_variation('harnessappdemodarkmode', target, False)
            print(f"Feature flag 'harnessappdemodarkmode' is {'enabled' if is_dark_mode_enabled else 'disabled'} for target {target.identifier}")

            string_flag_value = client.string_variation('another_string_flag', target, 'default_string')
            print(f"Feature flag 'another_string_flag' has value: {string_flag_value}")

            time.sleep(10)
    except KeyboardInterrupt:
        print("Stopping...")
    finally:
        client.close()
        print("SDK Closed.")

if __name__ == '__main__':
    main()

view raw JSON →