def apply_rate_limit(router, path: str, method: str, limit_str: str):\n for route in router.routes:\n if isinstance(route, APIRoute) and route.path == path and method.upper() in route.methods:\n route.endpoint = limiter.limit(limit_str)(route.endpoint)\n\n\nregister_router = fastapi_users.get_register_router(UserRead, UserCreate)\n\napply_rate_limit(register_router, \"/register\", \"POST\", \"5/minute\")\n\napp.include_router(\n register_router,\n prefix=\"/auth\",\n tags=[\"auth\"],\n)It's important to note that you must call apply_rate_limit before app.include_router.
I just tested it, and it works without any issues.
","upvoteCount":2,"url":"https://github.com/fastapi-users/fastapi-users/discussions/1505#discussioncomment-12911985"}}}-
|
Hi, how can I perform rate limiting on the user requests? @limiter.limit("5/minute")
async def myendpoint(request: Request)
pass |
Beta Was this translation helpful? Give feedback.
-
def apply_rate_limit(router, path: str, method: str, limit_str: str):
for route in router.routes:
if isinstance(route, APIRoute) and route.path == path and method.upper() in route.methods:
route.endpoint = limiter.limit(limit_str)(route.endpoint)
register_router = fastapi_users.get_register_router(UserRead, UserCreate)
apply_rate_limit(register_router, "/register", "POST", "5/minute")
app.include_router(
register_router,
prefix="/auth",
tags=["auth"],
)It's important to note that you must call I just tested it, and it works without any issues. |
Beta Was this translation helpful? Give feedback.
It's important to note that you must call
apply_rate_limitbeforeapp.include_router.I just tested it, and it works without any issues.