fastapi-redis-rate-limiter


Namefastapi-redis-rate-limiter JSON
Version 1.0.1 PyPI version JSON
download
home_pagehttps://github.com/iunary/fastapi-redis-rate-limiter
Summaryfastapi rate limiter middleware
upload_time2023-05-14 22:18:02
maintainer
docs_urlNone
authorYusuf
requires_python>=3.8
licenseMIT
keywords fastapi rate limiter redis middleware
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![Fastapi rate limiter](https://github.com/iunary/fastapi-redis-rate-limiter/actions/workflows/app.yml/badge.svg)](https://github.com/iunary/fastapi-redis-rate-limiter/actions/workflows/app.yml)
# Fastapi redis rate limiter middleware

Redis Rate Limiter Middleware is a Python module that provides rate limiting functionality for FastAPI applications using Redis as the storage backend. It allows you to limit the number of requests a client can make within a specified time window.

## Features

- Simple integration with FastAPI applications
- Customizable rate limit and time window
- Uses Redis as the storage backend for efficient rate limiting
- Easy to configure and use

## Installation

Install the Redis Rate Limiter Middleware module using `pip`:

```shell
pip install fastapi_redis_rate_limiter
```

## Usage

Here's an example of how to use the Redis Rate Limiter Middleware in a FastAPI application:

```python
from fastapi import FastAPI
from fastapi_redis_rate_limiter import RedisRateLimiterMiddleware, RedisClient

app = FastAPI()

# Initialize the Redis client
redis_client = RedisClient(host="localhost", port=6379, db=0)

# Apply the rate limiter middleware to the app
app.add_middleware(RedisRateLimiterMiddleware, redis_client=redis_client, limit=40, window=60)

@app.get("/limited")
async def limited():
    return {"message": "This is a protected endpoint."}


# Run the FastAPI application
if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="0.0.0.0", port=8000)
```

In this example, we create a FastAPI application and initialize a Redis client using `RedisClient`. Then, we add `RedisRateLimiterMiddleware` to the app middleware using `add_middleware` and by passing in Redis client, rate limit, and time window.

Make sure to adjust the Redis connection parameters (host, port, and db) according to your Redis server configuration.

## Configuration

The RedisRateLimiterMiddleware accepts the following parameters:

- `app` (FastAPI): The FastAPI application instance.
- `redis_client` (RedisClient): The Redis client instance for interacting with Redis.
- `limit` (int): The maximum number of requests allowed within the time window.
- `window` (int): The time window in seconds within which the requests are limited (default: 60 seconds).

Adjust the `limit` and `window` values according to your desired rate limiting requirements.

## Contributions

Contributions, issues, and feature requests are welcome! Feel free to open a new issue or submit a pull request on GitHub.

## License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/iunary/fastapi-redis-rate-limiter",
    "name": "fastapi-redis-rate-limiter",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "fastapi rate limiter,redis,middleware",
    "author": "Yusuf",
    "author_email": "contact@yusuf.im",
    "download_url": "https://files.pythonhosted.org/packages/2b/ed/7e57b7a097486b286242eb2810dcef5d0e276b9b16ba50595651fd072606/fastapi_redis_rate_limiter-1.0.1.tar.gz",
    "platform": null,
    "description": "[![Fastapi rate limiter](https://github.com/iunary/fastapi-redis-rate-limiter/actions/workflows/app.yml/badge.svg)](https://github.com/iunary/fastapi-redis-rate-limiter/actions/workflows/app.yml)\n# Fastapi redis rate limiter middleware\n\nRedis Rate Limiter Middleware is a Python module that provides rate limiting functionality for FastAPI applications using Redis as the storage backend. It allows you to limit the number of requests a client can make within a specified time window.\n\n## Features\n\n- Simple integration with FastAPI applications\n- Customizable rate limit and time window\n- Uses Redis as the storage backend for efficient rate limiting\n- Easy to configure and use\n\n## Installation\n\nInstall the Redis Rate Limiter Middleware module using `pip`:\n\n```shell\npip install fastapi_redis_rate_limiter\n```\n\n## Usage\n\nHere's an example of how to use the Redis Rate Limiter Middleware in a FastAPI application:\n\n```python\nfrom fastapi import FastAPI\nfrom fastapi_redis_rate_limiter import RedisRateLimiterMiddleware, RedisClient\n\napp = FastAPI()\n\n# Initialize the Redis client\nredis_client = RedisClient(host=\"localhost\", port=6379, db=0)\n\n# Apply the rate limiter middleware to the app\napp.add_middleware(RedisRateLimiterMiddleware, redis_client=redis_client, limit=40, window=60)\n\n@app.get(\"/limited\")\nasync def limited():\n    return {\"message\": \"This is a protected endpoint.\"}\n\n\n# Run the FastAPI application\nif __name__ == \"__main__\":\n    import uvicorn\n\n    uvicorn.run(app, host=\"0.0.0.0\", port=8000)\n```\n\nIn this example, we create a FastAPI application and initialize a Redis client using `RedisClient`. Then, we add `RedisRateLimiterMiddleware` to the app middleware using `add_middleware` and by passing in Redis client, rate limit, and time window.\n\nMake sure to adjust the Redis connection parameters (host, port, and db) according to your Redis server configuration.\n\n## Configuration\n\nThe RedisRateLimiterMiddleware accepts the following parameters:\n\n- `app` (FastAPI): The FastAPI application instance.\n- `redis_client` (RedisClient): The Redis client instance for interacting with Redis.\n- `limit` (int): The maximum number of requests allowed within the time window.\n- `window` (int): The time window in seconds within which the requests are limited (default: 60 seconds).\n\nAdjust the `limit` and `window` values according to your desired rate limiting requirements.\n\n## Contributions\n\nContributions, issues, and feature requests are welcome! Feel free to open a new issue or submit a pull request on GitHub.\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "fastapi rate limiter middleware",
    "version": "1.0.1",
    "project_urls": {
        "Homepage": "https://github.com/iunary/fastapi-redis-rate-limiter"
    },
    "split_keywords": [
        "fastapi rate limiter",
        "redis",
        "middleware"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c47a9cf873f212d534ab6acd7392c85fa25850389e4a7317bd02d0f9ac15051b",
                "md5": "0f1eed213dc3f70d37583a8e9a8a3b0a",
                "sha256": "6ab3db87eb046bdd420f5373576f4f0f6c5639a427fe323101a31e2de4e6623d"
            },
            "downloads": -1,
            "filename": "fastapi_redis_rate_limiter-1.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0f1eed213dc3f70d37583a8e9a8a3b0a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 5645,
            "upload_time": "2023-05-14T22:17:59",
            "upload_time_iso_8601": "2023-05-14T22:17:59.973657Z",
            "url": "https://files.pythonhosted.org/packages/c4/7a/9cf873f212d534ab6acd7392c85fa25850389e4a7317bd02d0f9ac15051b/fastapi_redis_rate_limiter-1.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2bed7e57b7a097486b286242eb2810dcef5d0e276b9b16ba50595651fd072606",
                "md5": "b384cbb97561239dd69ea533422ae351",
                "sha256": "92a751ac630253d949494dc436baa559a2eb2135f0be8ffaae42c39e1a0adb6e"
            },
            "downloads": -1,
            "filename": "fastapi_redis_rate_limiter-1.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "b384cbb97561239dd69ea533422ae351",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 4674,
            "upload_time": "2023-05-14T22:18:02",
            "upload_time_iso_8601": "2023-05-14T22:18:02.154904Z",
            "url": "https://files.pythonhosted.org/packages/2b/ed/7e57b7a097486b286242eb2810dcef5d0e276b9b16ba50595651fd072606/fastapi_redis_rate_limiter-1.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-05-14 22:18:02",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "iunary",
    "github_project": "fastapi-redis-rate-limiter",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "fastapi-redis-rate-limiter"
}
        
Elapsed time: 0.07141s