Using slowapi in bigger application
See original GitHub issueIn test.py from app is run:
import uvicorn
from app.main import app
if __name__ == "__main__":
uvicorn.run("test:app", host="0.0.0.0", port=8000, reload=True)
In main.py
from app.api.api_v1.api import router as api_router
from fastapi import FastAPI
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded
def get_application() -> FastAPI:
application = FastAPI(title=PROJECT_NAME, debug=DEBUG, version=VERSION)
application.add_event_handler(
"startup", create_start_app_handler(application))
application.add_event_handler(
"shutdown", create_stop_app_handler(application))
application.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
application.include_router(api_router, prefix=API_PREFIX)
return application
app = get_application()
In endpoint user.py, how to use @limiter.limit(“5/minute”) ?
from starlette.requests import Request
router = APIRouter()
@router.post('/show',
tags=["show"],
name="show:show")
async def homepage(request: Request):
return PlainTextResponse("test")
***code****
In this use case how to use slowapi in endpoint. I need to limit to 5 requests per minute per user only and block that user for a minute Sleep and retry after a minute
In api.py
from app.api.api_v1.endpoints.show import router as ShowRouter
router = APIRouter()
router.include_router(ShowRouter, tags=["show"], prefix="/show")
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top Results From Across the Web
SlowApi Documentation
Single and multiple limit decorator on endpoint functions to apply limits; redis, memcached and memory backends to track your limits (memory as a...
Read more >FastAPI and SlowAPI limit request under all “path/*”
Apply a shared limit only to the endpoints you wish, using shared_limit . As per the documentation: shared_limit: Decorator to be applied to ......
Read more >Where/how to store results of a slow API call that I only want to ...
My app uses an API that is very slow. I want to load the results of this API ... Or use Memcached or...
Read more >How to Deal With Slow APIs - DZone
A better approach, in this case, is to not deal with the response coming out of a slow API but to simply stop...
Read more >How to handle UI updates dependent on slow API responses
The result set we're dealing with is rather large, and our target machine not too powerful. So, iterating through an array of large...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Your code is not importing the
limiter
object but a function, that’s why it’s not working. You don’t need that extra limiter function, you can just define the object in the limiter file and import it elsewhere.Hi @himalacharya is your question about how to import
limiter
in your endpoint file? Or about limiting per user? In that latter case, you need to define what “user” means. Is it an IP address? their userID? something else?