Niansuh commited on
Commit
ccd9777
·
verified ·
1 Parent(s): 97b3e2f

Update api/rpmlimits.py

Browse files
Files changed (1) hide show
  1. api/rpmlimits.py +13 -11
api/rpmlimits.py CHANGED
@@ -1,15 +1,17 @@
1
- from slowapi import Limiter
2
  from slowapi.util import get_remote_address
3
- from slowapi.errors import RateLimitExceeded
4
- from fastapi import FastAPI, Request
5
- from starlette.responses import JSONResponse
6
 
7
- # Initialize the limiter
8
  limiter = Limiter(key_func=get_remote_address)
9
 
10
- # Exception handler for rate limit exceeded
11
- async def rate_limit_exceeded_handler(request: Request, exc: RateLimitExceeded):
12
- return JSONResponse(
13
- status_code=429,
14
- content={"error": {"message": "Rate limit exceeded. Please wait and try again.", "type": "rate_limit"}}
15
- )
 
 
 
 
 
 
1
  from slowapi.util import get_remote_address
2
+ from slowapi import Limiter
3
+ from fastapi import Request
 
4
 
5
+ # Initialize a limiter instance
6
  limiter = Limiter(key_func=get_remote_address)
7
 
8
+ def check_rate_limit(request: Request):
9
+ """
10
+ Function to check if a user has exceeded their rate limit.
11
+ This is managed by slowapi's limiter.
12
+ """
13
+ if not limiter.is_allowed(request):
14
+ raise HTTPException(
15
+ status_code=429,
16
+ detail="Rate limit exceeded. Please try again later."
17
+ )