fastapi-throttle


Namefastapi-throttle JSON
Version 0.1.4 PyPI version JSON
download
home_pagehttps://github.com/AliYmn/fastapi-throttle
SummaryA rate limiter for FastAPI without using Redis.
upload_time2024-09-04 10:05:25
maintainerNone
docs_urlNone
authorAli Yaman
requires_python<3.13,>=3.8
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Fastapi Throttle

[![pypi](https://img.shields.io/pypi/v/fastapi-throttle.svg?style=flat)](https://pypi.python.org/pypi/fastapi-throttle)
[![ci](https://github.com/AliYmn/fastapi-throttle/workflows/CI/badge.svg)](https://github.com/AliYmn/fastapi-throttle/actions?query=workflow:CI)

`fastapi-throttle` is a simple in-memory rate limiter for FastAPI applications. This package allows you to control the number of requests a client can make to your API within a specified time window without relying on external dependencies like Redis. It is ideal for lightweight applications where simplicity and speed are paramount.

## Features
- **Without Redis** : You don’t need to install or configure Redis.
- **In-Memory Rate Limiting**: No external dependencies required. Keeps everything in memory for fast and simple rate limiting.
- **Flexible Configuration**: Easily configure rate limits per route or globally.
- **Python Version Support**: Compatible with Python 3.8 up to 3.12.

## Installation

To install the package, use pip:

```bash
pip install fastapi-throttle
```

## Usage
Here’s how you can use fastapi-throttle in your FastAPI application:

### Basic Example
```python
from fastapi import FastAPI, Depends
from fastapi_throttle import RateLimiter

app = FastAPI()

# Apply rate limiting globally
@app.get("/", dependencies=[Depends(RateLimiter(times=2, seconds=5))])
async def root():
    return {"message": "Hello, World!"}
```

## Route-Specific Rate Limiting
You can apply different rate limits to different routes as needed:

```python
from fastapi import FastAPI, Depends
from fastapi_throttle import RateLimiter

app = FastAPI()

# Apply different rate limits to different routes
@app.get("/route1", dependencies=[Depends(RateLimiter(times=3, seconds=10))])
async def route1():
    return {"message": "This is route 1"}

@app.get("/route2", dependencies=[Depends(RateLimiter(times=5, seconds=15))])
async def route2():
    return {"message": "This is route 2"}
```

## Configuration
- times: The maximum number of requests allowed per client within the specified period.
- seconds: The time window in seconds within which the requests are counted.

## Example with Custom Configuration
Here is an example where you use custom rate limiting per endpoint:

```python
from fastapi import FastAPI, Depends
from fastapi_throttle import RateLimiter

app = FastAPI()

@app.get("/custom", dependencies=[Depends(RateLimiter(times=10, seconds=60))])
async def custom():
    return {"message": "This is a custom route with its own rate limit."}
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/AliYmn/fastapi-throttle",
    "name": "fastapi-throttle",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<3.13,>=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "Ali Yaman",
    "author_email": "aliymn.db@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/2b/a2/8380bb2683899370b61674ce7d27f3c357cb9761d7d52f8857ebc9f26c5d/fastapi_throttle-0.1.4.tar.gz",
    "platform": null,
    "description": "# Fastapi Throttle\n\n[![pypi](https://img.shields.io/pypi/v/fastapi-throttle.svg?style=flat)](https://pypi.python.org/pypi/fastapi-throttle)\n[![ci](https://github.com/AliYmn/fastapi-throttle/workflows/CI/badge.svg)](https://github.com/AliYmn/fastapi-throttle/actions?query=workflow:CI)\n\n`fastapi-throttle` is a simple in-memory rate limiter for FastAPI applications. This package allows you to control the number of requests a client can make to your API within a specified time window without relying on external dependencies like Redis. It is ideal for lightweight applications where simplicity and speed are paramount.\n\n## Features\n- **Without Redis** : You don\u2019t need to install or configure Redis.\n- **In-Memory Rate Limiting**: No external dependencies required. Keeps everything in memory for fast and simple rate limiting.\n- **Flexible Configuration**: Easily configure rate limits per route or globally.\n- **Python Version Support**: Compatible with Python 3.8 up to 3.12.\n\n## Installation\n\nTo install the package, use pip:\n\n```bash\npip install fastapi-throttle\n```\n\n## Usage\nHere\u2019s how you can use fastapi-throttle in your FastAPI application:\n\n### Basic Example\n```python\nfrom fastapi import FastAPI, Depends\nfrom fastapi_throttle import RateLimiter\n\napp = FastAPI()\n\n# Apply rate limiting globally\n@app.get(\"/\", dependencies=[Depends(RateLimiter(times=2, seconds=5))])\nasync def root():\n    return {\"message\": \"Hello, World!\"}\n```\n\n## Route-Specific Rate Limiting\nYou can apply different rate limits to different routes as needed:\n\n```python\nfrom fastapi import FastAPI, Depends\nfrom fastapi_throttle import RateLimiter\n\napp = FastAPI()\n\n# Apply different rate limits to different routes\n@app.get(\"/route1\", dependencies=[Depends(RateLimiter(times=3, seconds=10))])\nasync def route1():\n    return {\"message\": \"This is route 1\"}\n\n@app.get(\"/route2\", dependencies=[Depends(RateLimiter(times=5, seconds=15))])\nasync def route2():\n    return {\"message\": \"This is route 2\"}\n```\n\n## Configuration\n- times: The maximum number of requests allowed per client within the specified period.\n- seconds: The time window in seconds within which the requests are counted.\n\n## Example with Custom Configuration\nHere is an example where you use custom rate limiting per endpoint:\n\n```python\nfrom fastapi import FastAPI, Depends\nfrom fastapi_throttle import RateLimiter\n\napp = FastAPI()\n\n@app.get(\"/custom\", dependencies=[Depends(RateLimiter(times=10, seconds=60))])\nasync def custom():\n    return {\"message\": \"This is a custom route with its own rate limit.\"}\n```\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A rate limiter for FastAPI without using Redis.",
    "version": "0.1.4",
    "project_urls": {
        "Homepage": "https://github.com/AliYmn/fastapi-throttle"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a0e81586614e3299b25040c2639e040a8e5181c2e0f07a60750f8920f38c1630",
                "md5": "27df62ca5779c204ccd1ba91489b0f80",
                "sha256": "932b69eb0bc453d5d56576d0af7e9236b05249ec5dccbbea7feaa9aa40a8e204"
            },
            "downloads": -1,
            "filename": "fastapi_throttle-0.1.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "27df62ca5779c204ccd1ba91489b0f80",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<3.13,>=3.8",
            "size": 5193,
            "upload_time": "2024-09-04T10:05:24",
            "upload_time_iso_8601": "2024-09-04T10:05:24.332146Z",
            "url": "https://files.pythonhosted.org/packages/a0/e8/1586614e3299b25040c2639e040a8e5181c2e0f07a60750f8920f38c1630/fastapi_throttle-0.1.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2ba28380bb2683899370b61674ce7d27f3c357cb9761d7d52f8857ebc9f26c5d",
                "md5": "533d395188a4081741c381c4a731a6d7",
                "sha256": "96c51a87c805e8114cc0d54c3d25157ddab1cf6ed579a2dcce4ff3f56f7e1937"
            },
            "downloads": -1,
            "filename": "fastapi_throttle-0.1.4.tar.gz",
            "has_sig": false,
            "md5_digest": "533d395188a4081741c381c4a731a6d7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<3.13,>=3.8",
            "size": 4369,
            "upload_time": "2024-09-04T10:05:25",
            "upload_time_iso_8601": "2024-09-04T10:05:25.663953Z",
            "url": "https://files.pythonhosted.org/packages/2b/a2/8380bb2683899370b61674ce7d27f3c357cb9761d7d52f8857ebc9f26c5d/fastapi_throttle-0.1.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-04 10:05:25",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "AliYmn",
    "github_project": "fastapi-throttle",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "fastapi-throttle"
}
        
Elapsed time: 4.43631s