Skip to main content

Lightweight Python SDK for FlagVault, enabling seamless feature flag integration and real-time flag status checks for Python applications.

Project description

FlagVault Python SDK

A lightweight Python SDK that allows developers to integrate FlagVault's feature flag service into their Python applications. Feature flags let you enable/disable features remotely without deploying new code.

Table of Contents

Installation

pip install flagvault-sdk
# or
poetry add flagvault-sdk

Quick Start

from flagvault_sdk import FlagVaultSDK, FlagVaultAuthenticationError, FlagVaultNetworkError

# Initialize the SDK with your API credentials
sdk = FlagVaultSDK(
    api_key="your-api-key",
    api_secret="your-api-secret",
    # Optional: custom base URL and timeout
    # base_url="https://custom-api.flagvault.com",
    # timeout=10
)

# Check if a feature flag is enabled
def check_feature():
    try:
        is_enabled = sdk.is_enabled("my-feature-flag")
        
        if is_enabled:
            # Feature is enabled, run feature code
            print("Feature is enabled!")
        else:
            # Feature is disabled, run fallback code
            print("Feature is disabled.")
    except FlagVaultAuthenticationError:
        print("Invalid API credentials")
    except FlagVaultNetworkError:
        print("Network connection failed")
    except Exception as error:
        print(f"Unexpected error: {error}")

check_feature()

Project Overview

What It Is

FlagVault Python SDK provides a simple, reliable way to integrate feature flags into your Python applications. Feature flags (also known as feature toggles) allow you to:

  • Enable/disable features without deploying new code
  • Perform A/B testing and gradual rollouts
  • Create kill switches for problematic features
  • Manage environment-specific features

Core Functionality

The SDK centers around one main class and method:

# Initialize once
sdk = FlagVaultSDK(api_key="key", api_secret="secret")

# Use throughout your application
if sdk.is_enabled("new-checkout-flow"):
    show_new_checkout()
else:
    show_old_checkout()

How It Works

  1. Initialize: Create SDK instance with API credentials from your FlagVault dashboard
  2. Check Flag: Call is_enabled("flag-key") anywhere in your code
  3. HTTP Request: SDK makes secure GET request to FlagVault API
  4. Parse Response: Returns boolean from API response
  5. Handle Errors: Specific exceptions for different failure scenarios

Error Handling

The SDK provides specific exception types for different error scenarios:

from flagvault_sdk import (
    FlagVaultSDK,
    FlagVaultError,
    FlagVaultAuthenticationError,
    FlagVaultNetworkError,
    FlagVaultAPIError,
)

try:
    is_enabled = sdk.is_enabled("my-feature-flag")
except FlagVaultAuthenticationError:
    # Handle authentication errors (401, 403)
    print("Check your API credentials")
except FlagVaultNetworkError:
    # Handle network errors (timeouts, connection issues)
    print("Network connection problem")
except FlagVaultAPIError:
    # Handle API errors (500, malformed responses, etc.)
    print("API error occurred")
except ValueError:
    # Handle invalid input (empty flag_key, etc.)
    print("Invalid input provided")

Exception Types

  • FlagVaultAuthenticationError: Invalid API credentials (401/403 responses)
  • FlagVaultNetworkError: Connection timeouts, network failures
  • FlagVaultAPIError: Server errors, malformed responses
  • ValueError: Invalid input parameters (empty flag keys, etc.)
  • FlagVaultError: Base exception class for all SDK errors

Configuration

SDK Parameters

  • api_key (required): Your FlagVault API key
  • api_secret (required): Your FlagVault API secret
  • base_url (optional): Custom API endpoint. Defaults to https://api.flagvault.com
  • timeout (optional): Request timeout in seconds. Defaults to 10

Getting API Credentials

  1. Sign up at FlagVault
  2. Create a new project
  3. Go to Settings > API Credentials
  4. Generate new API credentials

API Reference

FlagVaultSDK(api_key, api_secret, base_url=None, timeout=10)

Creates a new FlagVault SDK instance.

Parameters:

  • api_key (str): Your FlagVault API key
  • api_secret (str): Your FlagVault API secret
  • base_url (str, optional): Custom API endpoint
  • timeout (int, optional): Request timeout in seconds

Raises:

  • ValueError: If api_key or api_secret is empty

is_enabled(flag_key: str) -> bool

Checks if a feature flag is enabled.

Parameters:

  • flag_key (str): The key/name of the feature flag

Returns:

  • bool: True if the flag is enabled, False otherwise

Raises:

  • ValueError: If flag_key is empty or None
  • FlagVaultAuthenticationError: If API credentials are invalid
  • FlagVaultNetworkError: If network request fails
  • FlagVaultAPIError: If API returns an error

Use Cases

1. A/B Testing

if sdk.is_enabled("new-ui-design"):
    render_new_design()
else:
    render_current_design()

2. Gradual Rollouts

if sdk.is_enabled("premium-feature"):
    show_premium_features()
else:
    show_basic_features()

3. Kill Switches

if sdk.is_enabled("external-api-integration"):
    call_external_api()
else:
    use_cached_data()  # Fallback if external service has issues

4. Environment-Specific Features

if sdk.is_enabled("debug-mode"):
    enable_verbose_logging()
    show_debug_info()

Project Structure

sdk-py/
├── flagvault_sdk/           # Main package
│   ├── __init__.py         # Package exports & version
│   └── flagvault_sdk.py    # Core SDK implementation
├── tests/                  # Test suite
│   ├── __init__.py
│   └── test_flagvault_sdk.py
├── examples/               # Usage examples
├── LICENSE                 # MIT license
├── README.md              # This file
├── setup.py               # Package configuration
├── pyproject.toml         # Build configuration
└── requirements-dev.txt   # Development dependencies

Key Features

  • 🚀 Simple: One method, clear API
  • 🛡️ Reliable: Comprehensive error handling with custom exceptions
  • 🔧 Compatible: Works with Python 3.6+
  • ✅ Well-Tested: 92% test coverage, handles edge cases
  • ⚡ Production-Ready: Configurable timeouts, proper error types
  • 📦 Lightweight: Only requires requests library

Testing Strategy

The SDK includes 14 comprehensive tests covering:

  • ✅ Initialization (valid/invalid credentials)
  • ✅ Successful flag checks (enabled/disabled responses)
  • ✅ Error scenarios (authentication, network, API errors)
  • ✅ Edge cases (invalid JSON, missing fields, special characters)
  • ✅ Timeout and connection handling

Requirements

  • Python 3.6 or later
  • requests >= 2.25.0

Development

Setting up for development

# Clone the repository
git clone https://github.com/flagvault/sdk-py.git
cd sdk-py

# Create a virtual environment (optional but recommended)
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install development dependencies
pip install -e ".[dev]"
pip install -r requirements-dev.txt

Running tests

pytest
# With coverage
pytest --cov=flagvault_sdk

Code Quality

# Linting
flake8 flagvault_sdk tests

# Code formatting
black flagvault_sdk tests

# Import sorting
isort flagvault_sdk tests

# Type checking
mypy flagvault_sdk

Building the package

python -m build

License

MIT

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure all tests pass
  6. Submit a pull request

Support


Made with ❤️ by the FlagVault team

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

flagvault_sdk-0.1.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

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

flagvault_sdk-0.1.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file flagvault_sdk-0.1.0.tar.gz.

File metadata

  • Download URL: flagvault_sdk-0.1.0.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for flagvault_sdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 149099c7ae18979e547da7506645a4874d07dcb5212afd13e68d84089f30e667
MD5 1fb8c1faed59cdf40a7023cd8b7c51ca
BLAKE2b-256 ca2cf48fb4b64cd636bed6ea4a435027638e6390112cfe86f4f151bc434332f5

See more details on using hashes here.

File details

Details for the file flagvault_sdk-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: flagvault_sdk-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for flagvault_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b263fbb3f479623a4b5deca22cf414fbf522d1ec4acbd794ec6d6f0ede1e6d62
MD5 9050403371a1fb189731d19755d6083a
BLAKE2b-256 16c126d778e58b7d88d143b662e6b319ebe2cf3648b1a08a76bae87f9558b950

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