Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/byunjuneseok/fastapi-frl
⚡️⏲ Rate limiter library for FastAPI. (Work in progress..)
https://github.com/byunjuneseok/fastapi-frl
aioredis fastapi python3 rate-limiter rate-limiting redis
Last synced: 3 months ago
JSON representation
⚡️⏲ Rate limiter library for FastAPI. (Work in progress..)
- Host: GitHub
- URL: https://github.com/byunjuneseok/fastapi-frl
- Owner: byunjuneseok
- License: apache-2.0
- Created: 2022-02-10T02:40:51.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-03-30T13:49:36.000Z (almost 3 years ago)
- Last Synced: 2024-08-09T15:01:48.445Z (6 months ago)
- Topics: aioredis, fastapi, python3, rate-limiter, rate-limiting, redis
- Language: Python
- Homepage: https://byunjuneseok.github.io/fastapi-frl/
- Size: 561 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fastapi-frl
[![pypi](https://img.shields.io/pypi/v/fastapi-frl.svg)](https://pypi.python.org/pypi/fastapi-frl)
[![versions](https://img.shields.io/pypi/pyversions/fastapi-frl.svg)](https://pypi.org/project/fastapi-frl/)Rate limiter library for FastAPI.
![diagram](./static/diagram.png)
## Prerequisite
- Redis server## Installation
```shell
pip install fastapi-frl
```
## Getting Started```python3
from fastapi import APIRouter, Depends, FastAPIfrom fastapi_frl import LimiterBackend, Limiter
from fastapi_frl.algorithms import SimpleAlgorithm, SlidingWindowCounter
from fastapi_frl.key import KeyByIP, KeyByPathapp = FastAPI()
# You only need to initialize backend of limiter once.
limiter_backend = LimiterBackend('redis://localhost')# Define the limiter.
limiter = Limiter(
name='hello_world_limiter',
algorithm=SimpleAlgorithm(threshold=10),
key_generator=KeyByIP()
)# Apply it to a single api endpoint.
@app.get('/', status_code=200, dependencies=[Depends(limiter)])
async def hello_world():
return {'message': 'Hello World'}# You can define multiple limiter with different rules.
api_book_limiter = Limiter(
name='api_book_limiter',
algorithm=SlidingWindowCounter(10, 10),
key_generator=KeyByPath(),
)# Also, you can apply it to the api router.
api_book_router = APIRouter(prefix='/books', tags=['Book'], dependencies=[Depends(api_book_limiter)])...
app.include_router(api_book_router)
```
## Documentation
The API documentation is in progress.