# FastAPI Rate Limit Controller
## Table of Contents
- [Description](#description)
- [Installation](#installation)
- [Usage](#usage)
- [Requirements](#requirements)
- [Additional Features](#additional-features)
- [Contributing](#contributing)
- [License](#license)
- [Acknowledgments](#acknowledgments)
## Description
This FastAPI library enables developers to control and limit the number of requests made to API endpoints, providing an effective solution to prevent server overload and enhance security. By using Redis and local memory storage, the library keeps a record of requests per endpoint, tracking when the last request was made and the number of requests within a specific time interval. If an endpoint reaches its allowed request limit within the defined interval, the library will temporarily block further requests to that endpoint, helping to prevent API abuse. This beta version currently supports Redis and local memory as storage options, with plans to expand to more types of storage in the future.
## Installation
```bash
pip install fastapi-request-limit
```
## Usage
Explain how to use your library with simple examples. Include code blocks and descriptions for each example to guide the user. For instance:
```python
from fastapi_requests_limit.configuration import Limiter
limiter = Limiter(host="localhost", port="6379", storage_engine='redis')
```
This example requires installing Redis and having a set host. If you don't want to use Redis but rather local memory, The configuration would be as follows:
```python
from fastapi_requests_limit.configuration import Limiter
limiter = Limiter(storage_engine='memory')
```
Then you must limit the endpoint you want.
```python
from fastapi_requests_limit.limiter_rest import LimiterDecorator as limiter_decorator
@app.get("/")
@limiter_decorator(time=5, count_target=3)
async def read_users(request: Request):
return [{"username": "Rick"}, {"username": "Morty"}]
```
## Configurations
### Local memory
```python
limiter = Limiter(storage_engine='memory')
```
### Redis server
```python
limiter = Limiter(host="<host>", port="<port>", storage_engine='redis')
```
### Decorator
```python
@limiter_decorator(time=5, count_target=3)
```
## Example
```python
from fastapi import FastAPI, Request
from fastapi_requests_limit.configuration import Limiter
from fastapi_requests_limit.limiter_rest import LimiterDecorator as limiter_decorator
app = FastAPI()
limiter = Limiter(host="localhost", port="6379", storage_engine='redis')
@app.get("/")
@limiter_decorator(time=5, count_target=3)
async def read_users(request: Request):
return [{"username": "Rick"}, {"username": "Morty"}]
```
## Project Status
If you're interested in contributing to this project, we welcome your contributions! Please read our CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
## License
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.txt) file for details.
Raw data
{
"_id": null,
"home_page": "https://github.com/oscarPyth/fastapi-requests-limit",
"name": "fastapi-requests-limit",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "fastapi ratelimiting api security",
"author": "Oscar Arias",
"author_email": "ariasp26@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/c7/91/6676fc22150992318d5379845ffa1b8de638fa7169059a5fddbb40e3ea8a/fastapi-requests-limit-0.1.5.tar.gz",
"platform": null,
"description": "# FastAPI Rate Limit Controller\r\n\r\n\r\n## Table of Contents\r\n\r\n- [Description](#description)\r\n- [Installation](#installation)\r\n- [Usage](#usage)\r\n- [Requirements](#requirements)\r\n- [Additional Features](#additional-features)\r\n- [Contributing](#contributing)\r\n- [License](#license)\r\n- [Acknowledgments](#acknowledgments)\r\n## Description\r\n\r\nThis FastAPI library enables developers to control and limit the number of requests made to API endpoints, providing an effective solution to prevent server overload and enhance security. By using Redis and local memory storage, the library keeps a record of requests per endpoint, tracking when the last request was made and the number of requests within a specific time interval. If an endpoint reaches its allowed request limit within the defined interval, the library will temporarily block further requests to that endpoint, helping to prevent API abuse. This beta version currently supports Redis and local memory as storage options, with plans to expand to more types of storage in the future.\r\n\r\n## Installation\r\n\r\n```bash\r\npip install fastapi-request-limit\r\n```\r\n\r\n\r\n## Usage\r\n\r\nExplain how to use your library with simple examples. Include code blocks and descriptions for each example to guide the user. For instance:\r\n\r\n```python\r\n\r\nfrom fastapi_requests_limit.configuration import Limiter\r\n\r\nlimiter = Limiter(host=\"localhost\", port=\"6379\", storage_engine='redis')\r\n```\r\nThis example requires installing Redis and having a set host. If you don't want to use Redis but rather local memory, The configuration would be as follows:\r\n\r\n```python\r\n\r\nfrom fastapi_requests_limit.configuration import Limiter\r\n\r\nlimiter = Limiter(storage_engine='memory')\r\n```\r\nThen you must limit the endpoint you want.\r\n```python\r\nfrom fastapi_requests_limit.limiter_rest import LimiterDecorator as limiter_decorator\r\n\r\n\r\n@app.get(\"/\")\r\n@limiter_decorator(time=5, count_target=3)\r\nasync def read_users(request: Request):\r\n return [{\"username\": \"Rick\"}, {\"username\": \"Morty\"}]\r\n\r\n```\r\n\r\n\r\n\r\n## Configurations\r\n\r\n### Local memory\r\n```python\r\nlimiter = Limiter(storage_engine='memory')\r\n```\r\n\r\n### Redis server\r\n```python\r\nlimiter = Limiter(host=\"<host>\", port=\"<port>\", storage_engine='redis')\r\n```\r\n\r\n### Decorator\r\n```python\r\n@limiter_decorator(time=5, count_target=3)\r\n\r\n```\r\n## Example\r\n\r\n```python\r\n\r\nfrom fastapi import FastAPI, Request\r\nfrom fastapi_requests_limit.configuration import Limiter\r\nfrom fastapi_requests_limit.limiter_rest import LimiterDecorator as limiter_decorator\r\n\r\napp = FastAPI()\r\nlimiter = Limiter(host=\"localhost\", port=\"6379\", storage_engine='redis')\r\n\r\n\r\n@app.get(\"/\")\r\n@limiter_decorator(time=5, count_target=3)\r\nasync def read_users(request: Request):\r\n return [{\"username\": \"Rick\"}, {\"username\": \"Morty\"}]\r\n\r\n\r\n```\r\n## Project Status\r\n\r\nIf you're interested in contributing to this project, we welcome your contributions! Please read our CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.\r\n\r\n## License\r\n\r\nThis project is licensed under the MIT License - see the [LICENSE.md](LICENSE.txt) file for details.\r\n\r\n\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Control and limit request rates to FastAPI applications with Redis and local memory support.",
"version": "0.1.5",
"project_urls": {
"Bug Reports": "https://github.com/oscarPyth/fastapi-requests-limit/issues",
"Homepage": "https://github.com/oscarPyth/fastapi-requests-limit",
"Source": "https://github.com/oscarPyth/fastapi-requests-limit"
},
"split_keywords": [
"fastapi",
"ratelimiting",
"api",
"security"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c7916676fc22150992318d5379845ffa1b8de638fa7169059a5fddbb40e3ea8a",
"md5": "538ac40812f9967220110ccccd983cda",
"sha256": "9e3940b157b1c963052133dd2cb6323914c34438785c4a9cabc94a9ce1a81620"
},
"downloads": -1,
"filename": "fastapi-requests-limit-0.1.5.tar.gz",
"has_sig": false,
"md5_digest": "538ac40812f9967220110ccccd983cda",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 7856,
"upload_time": "2024-04-05T20:20:09",
"upload_time_iso_8601": "2024-04-05T20:20:09.633983Z",
"url": "https://files.pythonhosted.org/packages/c7/91/6676fc22150992318d5379845ffa1b8de638fa7169059a5fddbb40e3ea8a/fastapi-requests-limit-0.1.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-05 20:20:09",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "oscarPyth",
"github_project": "fastapi-requests-limit",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "fastapi",
"specs": [
[
"==",
"0.103.2"
]
]
}
],
"lcname": "fastapi-requests-limit"
}