Skip to main content

Python SDK for Rollgate feature flags

Project description

Rollgate Python SDK

CI PyPI version License: MIT

Official Python SDK for Rollgate - Feature flags made simple.

Requirements

  • Python 3.9+
  • httpx >= 0.25.0
  • httpx-sse >= 0.4.0

Installation

pip install rollgate

Quick Start

import asyncio
from rollgate import RollgateClient, RollgateConfig, UserContext

async def main():
    # Initialize client
    config = RollgateConfig(api_key="your-api-key")
    client = RollgateClient(config)

    # Initialize and fetch flags
    await client.init()

    # Check if feature is enabled
    if client.is_enabled("new-feature"):
        print("New feature is enabled!")

    # With user targeting
    await client.identify(UserContext(
        id="user-123",
        email="user@example.com",
        attributes={"plan": "pro", "country": "IT"}
    ))

    if client.is_enabled("premium-feature"):
        print("Premium feature is enabled for this user!")

    # Cleanup
    await client.close()

asyncio.run(main())

Context Manager

async with RollgateClient(RollgateConfig(api_key="your-api-key")) as client:
    if client.is_enabled("my-feature"):
        # Feature is enabled
        pass

Configuration

from rollgate import (
    RollgateConfig,
    RetryConfig,
    CircuitBreakerConfig,
    CacheConfig,
)

config = RollgateConfig(
    api_key="your-api-key",
    base_url="https://api.rollgate.io",  # Custom API URL
    refresh_interval_ms=30000,  # Polling interval (30s default)
    enable_streaming=False,  # Use SSE for real-time updates
    timeout_ms=5000,  # Request timeout

    # Retry configuration
    retry=RetryConfig(
        max_retries=3,
        base_delay_ms=100,
        max_delay_ms=10000,
        jitter_factor=0.1,
    ),

    # Circuit breaker configuration
    circuit_breaker=CircuitBreakerConfig(
        failure_threshold=5,
        recovery_timeout_ms=30000,
        monitoring_window_ms=60000,
        success_threshold=3,
    ),

    # Cache configuration
    cache=CacheConfig(
        ttl_ms=300000,  # 5 minutes
        stale_ttl_ms=3600000,  # 1 hour
        persist_path="/tmp/rollgate-cache.json",  # Optional persistence
    ),
)

Events

client = RollgateClient(config)

# Register event callbacks
client.on("ready", lambda: print("Client ready"))
client.on("flags_updated", lambda flags: print(f"Flags updated: {flags}"))
client.on("flag_changed", lambda key, new, old: print(f"{key}: {old} -> {new}"))
client.on("error", lambda err: print(f"Error: {err}"))
client.on("circuit_open", lambda *args: print("Circuit breaker opened"))
client.on("circuit_closed", lambda: print("Circuit breaker closed"))

await client.init()

Event Tracking

Track conversion events for A/B testing experiments:

from rollgate import TrackEventOptions

# Track a conversion event
client.track(TrackEventOptions(
    flag_key="checkout-redesign",
    event_name="purchase",
    user_id="user-123",
))

# Track with all options
client.track(TrackEventOptions(
    flag_key="checkout-redesign",
    event_name="purchase",
    user_id="user-123",
    variation_id="variant-b",
    value=29.99,
    metadata={"currency": "EUR", "item_count": 3},
))

# Manually flush pending events
await client.flush_events()

Events are buffered in memory and flushed automatically every 30 seconds or when the buffer reaches 100 events. A final flush is attempted when the client is closed.

TrackEventOptions

Field Type Required Description
flag_key str Yes The flag key for the experiment
event_name str Yes Name of the conversion event
user_id str Yes The user who triggered the event
variation_id Optional[str] No The variation the user saw
value Optional[float] No Numeric value (e.g. revenue)
metadata Optional[Dict] No Additional event metadata

Features

Polling (Default)

By default, the SDK polls for flag updates every 30 seconds.

config = RollgateConfig(
    api_key="your-api-key",
    refresh_interval_ms=30000,  # Poll every 30s
)

SSE Streaming

Enable Server-Sent Events for real-time flag updates:

config = RollgateConfig(
    api_key="your-api-key",
    enable_streaming=True,
)

Circuit Breaker

The SDK includes a circuit breaker to prevent cascading failures:

# Check circuit state
state = client.circuit_state  # CircuitState.CLOSED, OPEN, or HALF_OPEN

# Get statistics
stats = client.get_circuit_stats()

# Force reset
client.reset_circuit()

Caching

Flags are cached locally with stale-while-revalidate support:

# Get cache statistics
stats = client.get_cache_stats()
hit_rate = client.get_cache_hit_rate()

# Clear cache
client.clear_cache()

Error Handling

from rollgate import (
    RollgateError,
    AuthenticationError,
    NetworkError,
    RateLimitError,
)

try:
    await client.init()
except AuthenticationError as e:
    print(f"Invalid API key: {e}")
except NetworkError as e:
    print(f"Network error: {e}")
except RateLimitError as e:
    print(f"Rate limited, retry after: {e.retry_after}s")
except RollgateError as e:
    print(f"Rollgate error: {e}")

API Reference

RollgateClient

Method Description
init(user?) Initialize client and fetch flags
is_enabled(flag_key, default?) Check if flag is enabled
is_enabled_detail(flag_key, default?) Check flag with evaluation reason
get_all_flags() Get all flags as dictionary
identify(user) Set user context and refresh flags
reset() Clear user context
refresh() Force refresh flags
track(options) Track a conversion event
flush_events() Flush pending conversion events
close() Cleanup resources

Evaluation Reasons

Get detailed information about why a flag evaluated to a particular value:

detail = client.is_enabled_detail("my-flag", False)
print(detail.value)        # bool
print(detail.reason.kind)  # "OFF", "TARGET_MATCH", "RULE_MATCH", "FALLTHROUGH", "ERROR", "UNKNOWN"

Reason kinds:

Kind Description
OFF Flag is disabled
TARGET_MATCH User is in the flag's target list
RULE_MATCH User matched a targeting rule
FALLTHROUGH Default rollout (no rules matched)
ERROR Error during evaluation
UNKNOWN Flag not found

UserContext

Field Type Description
id str User identifier (required)
email str? User email
attributes dict? Custom attributes for targeting

Documentation

Full documentation: docs.rollgate.io

About Rollgate

Rollgate is a feature management platform that helps teams release features safely with gradual rollouts, user targeting, and instant kill switches.

License

MIT

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

rollgate-1.1.0.tar.gz (40.7 kB view details)

Uploaded Source

Built Distribution

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

rollgate-1.1.0-py3-none-any.whl (32.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: rollgate-1.1.0.tar.gz
  • Upload date:
  • Size: 40.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for rollgate-1.1.0.tar.gz
Algorithm Hash digest
SHA256 cd332f4995307ebd88a26b49eb32018f02dd6d6c6860572fdd64720903699d14
MD5 cc5b1459a4c4a1e3fac89565fc7425b4
BLAKE2b-256 4681810f4fe7107a72ada2022674cf20aec8979350317b849496d84f5c7280be

See more details on using hashes here.

File details

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

File metadata

  • Download URL: rollgate-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 32.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for rollgate-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ba1416e4fbd82436ac0e509e61248ccc2967ec5c06880bf9d1946a60aa794ff5
MD5 9d24afaee7efb85ffb55b5261dc24a55
BLAKE2b-256 7b83198fec8e7325b9ff95ce06d55f680497ff14fdbe5d8abcacbae6b9cfef23

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