Harness Feature Flags Python SDK
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
- gotcha Older versions of the SDK (prior to 1.6.4) had issues with rigidly pinned `tenacity` and `typing_extensions` dependencies, potentially causing conflicts with other libraries in your project. Ensure you are using version 1.6.4 or newer for improved dependency management.
- gotcha SDK initialization failures are commonly caused by an invalid Server SDK Key or network connectivity issues to the Harness Feature Flags configuration service (https://config.ff.harness.io/api/1.0). Verify your key and network access.
- gotcha By default, the SDK uses both streaming (for real-time updates) and polling (as a fallback). In case of connection issues, the SDK will gracefully degrade, either serving the most recently cached flag state or the default variation provided in the `variation` call if no cached value exists. Ensure your application logic accounts for these fallback scenarios to prevent unexpected behavior.
Install
-
pip install harness-featureflags
Imports
- CfClient
from harness_featureflags.client import CfClient
Quickstart
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()