garde-fou


Namegarde-fou JSON
Version 0.1.11 PyPI version JSON
download
home_pageNone
SummaryProtective wrappers around paid API clients with quotas & duplicate detection
upload_time2025-07-19 20:20:29
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords api rate-limiting quota protection wrapper
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # garde-fou (Python)

[![PyPI version](https://badge.fury.io/py/garde-fou.svg)](https://badge.fury.io/py/garde-fou)
[![Python Tests](https://github.com/rfievet/garde-fou/actions/workflows/python-test.yml/badge.svg)](https://github.com/rfievet/garde-fou/actions/workflows/python-test.yml)
[![Python versions](https://img.shields.io/pypi/pyversions/garde-fou.svg)](https://pypi.org/project/garde-fou/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Downloads](https://pepy.tech/badge/garde-fou)](https://pepy.tech/project/garde-fou)

**garde-fou** is a lightweight guard for protecting against accidental over-usage of paid API calls. It provides call counting and duplicate detection to help you avoid unexpected API bills.

## Features

- **Call counting** - Set maximum number of calls and get warnings or exceptions when exceeded
- **Duplicate detection** - Detect and handle repeated identical API calls
- **Flexible violation handling** - Choose to warn, raise exceptions, or use custom handlers
- **Configuration support** - Load settings from JSON/YAML files or set programmatically
- **Async support** - Works with both synchronous and asynchronous functions

## Installation

```bash
pip install garde-fou
```

## Quick Start

```python
from gardefou import GardeFou

# Protect any function with call limits
guard = GardeFou(max_calls=5, on_violation_max_calls="warn")

# Instead of: result = expensive_api_call("query")
# Use: result = guard(expensive_api_call, "query")
result = guard(your_api_function, "your", "arguments")
```

## Usage Examples

### Basic Call Limiting
```python
from gardefou import GardeFou, QuotaExceededError

# Create a guard with a 3-call limit
guard = GardeFou(max_calls=3, on_violation_max_calls="raise")

try:
    for i in range(5):
        result = guard(api_call, f"query {i}")
except QuotaExceededError:
    print("Call limit exceeded!")
```

### Duplicate Call Detection
```python
# Warn on duplicate calls
guard = GardeFou(on_violation_duplicate_call="warn")

guard(api_call, "hello")  # First call - OK
guard(api_call, "hello")  # Duplicate - Warning logged
guard(api_call, "world")  # Different call - OK
```

### Using Profiles
```python
from gardefou import Profile

# Create a profile with multiple rules
profile = Profile(
    max_calls=10,
    on_violation_max_calls="raise",
    on_violation_duplicate_call="warn"
)

guard = GardeFou(profile=profile)
```

### Configuration Files
```python
# Load from JSON/YAML file
profile = Profile(config="gardefou.config.json")
guard = GardeFou(profile=profile)

# Or pass config as dict
config = {"max_calls": 5, "on_violation_max_calls": "warn"}
profile = Profile(config=config)
```

## Configuration Options

- `max_calls`: Maximum number of calls allowed (-1 for unlimited)
- `on_violation_max_calls`: Handler when call limit exceeded ("warn", "raise", or callable)
- `on_violation_duplicate_call`: Handler for duplicate calls ("warn", "raise", or callable)
- `on_violation`: Default handler for all violations

## How It Works

garde-fou works by wrapping your function calls. Instead of calling your API function directly, you call it through the guard:

```python
# Before
result = openai.chat.completions.create(messages=[...])

# After  
guard = GardeFou(max_calls=10)
result = guard(openai.chat.completions.create, messages=[...])
```

The guard tracks calls and enforces your configured rules before executing the actual function.

## Contributing

This is part of the multi-language garde-fou toolkit. See the main repository for contributing guidelines.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "garde-fou",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "api, rate-limiting, quota, protection, wrapper",
    "author": null,
    "author_email": "Robin Fi\u00e9vet <robinfievet@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/60/f2/e3b772d358396720072be7691e7dfeb4dab45aae71c01559fc0b7ab6c707/garde_fou-0.1.11.tar.gz",
    "platform": null,
    "description": "# garde-fou (Python)\n\n[![PyPI version](https://badge.fury.io/py/garde-fou.svg)](https://badge.fury.io/py/garde-fou)\n[![Python Tests](https://github.com/rfievet/garde-fou/actions/workflows/python-test.yml/badge.svg)](https://github.com/rfievet/garde-fou/actions/workflows/python-test.yml)\n[![Python versions](https://img.shields.io/pypi/pyversions/garde-fou.svg)](https://pypi.org/project/garde-fou/)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![Downloads](https://pepy.tech/badge/garde-fou)](https://pepy.tech/project/garde-fou)\n\n**garde-fou** is a lightweight guard for protecting against accidental over-usage of paid API calls. It provides call counting and duplicate detection to help you avoid unexpected API bills.\n\n## Features\n\n- **Call counting** - Set maximum number of calls and get warnings or exceptions when exceeded\n- **Duplicate detection** - Detect and handle repeated identical API calls\n- **Flexible violation handling** - Choose to warn, raise exceptions, or use custom handlers\n- **Configuration support** - Load settings from JSON/YAML files or set programmatically\n- **Async support** - Works with both synchronous and asynchronous functions\n\n## Installation\n\n```bash\npip install garde-fou\n```\n\n## Quick Start\n\n```python\nfrom gardefou import GardeFou\n\n# Protect any function with call limits\nguard = GardeFou(max_calls=5, on_violation_max_calls=\"warn\")\n\n# Instead of: result = expensive_api_call(\"query\")\n# Use: result = guard(expensive_api_call, \"query\")\nresult = guard(your_api_function, \"your\", \"arguments\")\n```\n\n## Usage Examples\n\n### Basic Call Limiting\n```python\nfrom gardefou import GardeFou, QuotaExceededError\n\n# Create a guard with a 3-call limit\nguard = GardeFou(max_calls=3, on_violation_max_calls=\"raise\")\n\ntry:\n    for i in range(5):\n        result = guard(api_call, f\"query {i}\")\nexcept QuotaExceededError:\n    print(\"Call limit exceeded!\")\n```\n\n### Duplicate Call Detection\n```python\n# Warn on duplicate calls\nguard = GardeFou(on_violation_duplicate_call=\"warn\")\n\nguard(api_call, \"hello\")  # First call - OK\nguard(api_call, \"hello\")  # Duplicate - Warning logged\nguard(api_call, \"world\")  # Different call - OK\n```\n\n### Using Profiles\n```python\nfrom gardefou import Profile\n\n# Create a profile with multiple rules\nprofile = Profile(\n    max_calls=10,\n    on_violation_max_calls=\"raise\",\n    on_violation_duplicate_call=\"warn\"\n)\n\nguard = GardeFou(profile=profile)\n```\n\n### Configuration Files\n```python\n# Load from JSON/YAML file\nprofile = Profile(config=\"gardefou.config.json\")\nguard = GardeFou(profile=profile)\n\n# Or pass config as dict\nconfig = {\"max_calls\": 5, \"on_violation_max_calls\": \"warn\"}\nprofile = Profile(config=config)\n```\n\n## Configuration Options\n\n- `max_calls`: Maximum number of calls allowed (-1 for unlimited)\n- `on_violation_max_calls`: Handler when call limit exceeded (\"warn\", \"raise\", or callable)\n- `on_violation_duplicate_call`: Handler for duplicate calls (\"warn\", \"raise\", or callable)\n- `on_violation`: Default handler for all violations\n\n## How It Works\n\ngarde-fou works by wrapping your function calls. Instead of calling your API function directly, you call it through the guard:\n\n```python\n# Before\nresult = openai.chat.completions.create(messages=[...])\n\n# After  \nguard = GardeFou(max_calls=10)\nresult = guard(openai.chat.completions.create, messages=[...])\n```\n\nThe guard tracks calls and enforces your configured rules before executing the actual function.\n\n## Contributing\n\nThis is part of the multi-language garde-fou toolkit. See the main repository for contributing guidelines.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Protective wrappers around paid API clients with quotas & duplicate detection",
    "version": "0.1.11",
    "project_urls": {
        "Bug Reports": "https://github.com/rfievet/garde-fou/issues",
        "Changelog": "https://github.com/rfievet/garde-fou/blob/main/python/CHANGELOG.md",
        "Homepage": "https://github.com/rfievet/garde-fou",
        "Source": "https://github.com/rfievet/garde-fou"
    },
    "split_keywords": [
        "api",
        " rate-limiting",
        " quota",
        " protection",
        " wrapper"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "24dae743276d2eda0954492be2154392fb7546f83a3f7490fc5fe8dae085afd3",
                "md5": "b318456acc6006ac898d109f1261c1fe",
                "sha256": "69e91e20fbce5ff19dc047a3161055af789df0ca91b808f01201a3e82bd50b90"
            },
            "downloads": -1,
            "filename": "garde_fou-0.1.11-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b318456acc6006ac898d109f1261c1fe",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 6553,
            "upload_time": "2025-07-19T20:20:23",
            "upload_time_iso_8601": "2025-07-19T20:20:23.258449Z",
            "url": "https://files.pythonhosted.org/packages/24/da/e743276d2eda0954492be2154392fb7546f83a3f7490fc5fe8dae085afd3/garde_fou-0.1.11-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "60f2e3b772d358396720072be7691e7dfeb4dab45aae71c01559fc0b7ab6c707",
                "md5": "a7fc564ee56278f922f5ac14c5d81dfd",
                "sha256": "148c683ea12d0bf6e1ff705e806c946033a1a2ab4afb389d6e080ce8a4e139a7"
            },
            "downloads": -1,
            "filename": "garde_fou-0.1.11.tar.gz",
            "has_sig": false,
            "md5_digest": "a7fc564ee56278f922f5ac14c5d81dfd",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 9001,
            "upload_time": "2025-07-19T20:20:29",
            "upload_time_iso_8601": "2025-07-19T20:20:29.785632Z",
            "url": "https://files.pythonhosted.org/packages/60/f2/e3b772d358396720072be7691e7dfeb4dab45aae71c01559fc0b7ab6c707/garde_fou-0.1.11.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-19 20:20:29",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "rfievet",
    "github_project": "garde-fou",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "garde-fou"
}
        
Elapsed time: 1.23601s