Metadata-Version: 2.4
Name: foff
Version: 0.2.0
Summary: Python SDK for FOFF Feature Config service
Author-email: Tabarakul Islam Hazarika <foff@twospoon.ai>
License: MIT License
        
        Copyright (c) 2026 Tabarakul Islam Hazarika
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://foff.twospoon.ai
Project-URL: Repository, https://github.com/twospoon/foff-feature-config-python-sdk
Project-URL: Issues, https://github.com/twospoon/foff-feature-config-python-sdk/issues
Project-URL: Changelog, https://github.com/twospoon/foff-feature-config-python-sdk/blob/main/CHANGELOG.md
Keywords: feature-flags,feature-config,configuration,foff
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: httpx>=0.27
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: pytest-asyncio>=0.23; extra == "dev"
Dynamic: license-file

# FOFF Python SDK

This document covers how to integrate with the [FOFF Feature Config Service](https://foff.twospoon.ai) using the Python SDK.

## Installation

Please head over to [foff.twospoon.ai](https://foff.twospoon.ai) and create your first feature config. Then integrate it in your codebase using the following steps.

## Dependency Installation

Install the dependency using the following command:

```bash
pip install foff
```

Requires **Python 3.10** or later.

## Quick Start

### Sync Client

Proceed to create the client:

```python
from foff import Client, Config

# STEP 1: Create appropriate config
config = Config(
    api_key="your-api-key",
    base_url="https://foff.twospoon.ai/live",
    scope="name-of-your-scope",
    polling_interval=30,  # refresh configs every 30 seconds (0 to disable)
)

# STEP 2: Create the client with the config
with Client(config) as client:
    # STEP 3: Retrieve configs for your created features for given hierarchies
    value = client.get_feature_config("my-feature", ["org-1", "team-a", "user-123"])
    print(value)
```

### Async Client

```python
import asyncio
from foff import AsyncClient, Config

config = Config(
    api_key="your-api-key",
    base_url="https://foff.twospoon.ai/live",
    scope="name-of-your-scope",
    polling_interval=30,
)

async def main():
    async with AsyncClient(config) as client:
        value = client.get_feature_config("my-feature", ["org-1", "team-a", "user-123"])
        print(value)

asyncio.run(main())
```

## Configuration

The SDK is configured via the `foff.Config` dataclass:

| Field              | Type  | Required | Default | Description                                                                 |
|--------------------|-------|----------|---------|-----------------------------------------------------------------------------|
| `api_key`          | `str` | Yes      |         | Your FOFF API key.                                                          |
| `base_url`         | `str` | Yes      |         | Base URL of the FOFF API.                                                   |
| `scope`            | `str` | Yes      |         | The scope to fetch configs for, such as `production` or `staging`.          |
| `polling_interval` | `int` | No       | `30`    | How often, in seconds, to poll for config updates. Use `0` to disable polling. |

### Validation

Creating a `Client` or `AsyncClient` validates the config and raises `ValueError` if `api_key`, `base_url`, or `scope` is empty.

## Creating a Client

### Sync Client

```python
with Client(config) as client:
    value = client.get_feature_config("dark-mode", ["org-1", "team-a", "user-123"])
```

### Async Client

```python
async with AsyncClient(config) as client:
    value = client.get_feature_config("dark-mode", ["org-1", "team-a", "user-123"])
```

## Fetching Feature Configs

### `get_feature_config(feature_name: str, ordered_hierarchy: list[str]) -> Any`

Returns the config value for a feature, resolved against a hierarchy.
The provided hierarchy must have the same number of levels as the scope's configured hierarchy.

```python
value = client.get_feature_config("dark-mode", ["org-1", "team-a", "user-123"])
```

Hierarchy resolution works from most specific to least specific:

1. The SDK looks up the most specific combination first.
2. For the example above, it checks keys in this order:
   - `org-1 + team-a + user-123`
   - `org-1 + team-a`
   - `org-1`
3. If none match, it falls back to the `"default"` value of the config for a feature.
4. If the feature does not exist at all, it returns `None`.

This lets you define config overrides at any level of your hierarchy, such as organisation -> team -> user or environment -> region -> service, and the SDK resolves the most specific value automatically.

## Polling

When `polling_interval > 0`, the client refreshes configs in the background.

- Polling errors are silently ignored, and the SDK continues serving the last successfully fetched config.
- Calling `close()` on `Client`, or `await close()` on `AsyncClient`, stops background polling and releases resources.

### Recommended Intervals

| Use Case            | Interval |
|---------------------|----------|
| Near-real-time      | 10s      |
| Standard            | 30-60s   |
| Low-traffic / batch | 300-600s |

## Client Lifecycle

### Sync

```python
# Context manager (recommended)
with Client(config) as client:
    ...

# Manual
client = Client(config)
try:
    ...
finally:
    client.close()
```

### Async

```python
# Context manager (recommended)
async with AsyncClient(config) as client:
    ...

# Manual
client = AsyncClient(config)
await client.start()
try:
    ...
finally:
    await client.close()
```

## Contributing

Issues and pull requests are tracked on [GitHub](https://github.com/twospoon/foff-feature-config-python-sdk).

## License

See [LICENSE](LICENSE).
