A request rate limiter for fastapi
Project description
fastapi-limiter
Introduction
FastAPI-Limiter is a rate limiting tool for fastapi routes.
Requirements
Install
Just install from pypi
> pip install fastapi-limiter
Quick Start
FastAPI-Limiter is simple to use, which just provide a dependency RateLimiter, the following example allow 2 times request per 5 seconds in route /.
import aioredis
import uvicorn
from fastapi import Depends, FastAPI
from fastapi_limiter import FastAPILimiter
from fastapi_limiter.depends import RateLimiter
app = FastAPI()
@app.on_event("startup")
async def startup():
redis = await aioredis.create_redis_pool("redis://localhost")
FastAPILimiter.init(redis)
@app.get("/", dependencies=[Depends(RateLimiter(times=2, seconds=5))])
async def index():
return {"msg": "Hello World"}
if __name__ == "__main__":
uvicorn.run("main:app", debug=True, reload=True)
Usage
There are some config in FastAPILimiter.init.
redis
The redis instance of aioredis.
prefix
Prefix of redis key.
identifier
Identifier of route limit, default is ip, you can override it such as userid and so on.
async def default_identifier(request: Request):
forwarded = request.headers.get("X-Forwarded-For")
if forwarded:
return forwarded.split(",")[0]
return request.client.host
callback
Callback when access is forbidden, default is raise HTTPException with 403 status code.
async def default_callback(request: Request):
raise HTTPException(HTTP_403_FORBIDDEN, "The request is frequent")
License
This project is licensed under the Apache-2.0 License.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi-limiter-0.1.0.tar.gz.
File metadata
- Download URL: fastapi-limiter-0.1.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8b5bcdfa7c86c36a2305563bb9da0e7127f1df711443a6eb35285a9c7e952f6c
|
|
| MD5 |
d74c8f306f6ff87379a9e1c51a86e68b
|
|
| BLAKE2b-256 |
b4b04e28c8313ea9014cf99b149570794cb7de09d01201dec7412d78e514ac99
|
File details
Details for the file fastapi_limiter-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_limiter-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bcdade8142a03ecfaab6982f9999bc76f970cfcab861c4d6715b01859750f4d7
|
|
| MD5 |
de4b44728da514895f3c1309f42db6ed
|
|
| BLAKE2b-256 |
ecda88086e5d3dddb027fcfcd88beaac163163d7983d9c1d291d349aef7113b2
|