Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update fastapi snippet so it can work from copy paste #91

Open
swartchris8 opened this issue Apr 19, 2022 · 2 comments
Open

Update fastapi snippet so it can work from copy paste #91

swartchris8 opened this issue Apr 19, 2022 · 2 comments

Comments

@swartchris8
Copy link

Hello,
This project is awesome thanks for all the work could the docs for fastapi be updated to this:

from fastapi import FastAPI, Request, Response
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded

limiter = Limiter(key_func=get_remote_address)
app = FastAPI()
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)

# Note: the route decorator must be above the limit decorator, not below it
@app.get("/home")
@limiter.limit("5/minute")
async def homepage(request: Request):
    return "test"

@app.get("/mars")
@limiter.limit("5/minute")
async def homepage(request: Request, response: Response):
    return {"key": "value"}

The current snippet misses the Request and Response import and I can't find a text response in fastapi

The above edited snippet will work when copy pasted!

@laurentS
Copy link
Owner

Hi @swartchris8 thanks for providing this code sample (and the kind words)! Just to make sure I've got the right context, can you confirm which version of fastapi you're running this with?

@swartchris8
Copy link
Author

I am on fastapi==0.67.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants