laurentS / slowapi

A rate limiter for Starlette and FastAPI
https://pypi.org/project/slowapi/
MIT License
1.21k stars 77 forks source link

Make slowapi available as a dependency for FastAPI? #23

Open laurentS opened 3 years ago

laurentS commented 3 years ago

As suggested in this comment it should be possible to make the functionality available as a dependency for FastAPI. It looks like this would not be usable in Starlette, so probably only makes sense if the extra code required is minimal.

twcurrie commented 3 years ago

This appears to be the implementation approach for this other rate-limiting library for FastAPI.

tinducvo commented 3 years ago

Awesome work. I think also this pattern fits best with fastAPI and reduces boiler-plate.

maitham commented 3 years ago

You should just be able to create your own dependency like this

@limiter.limit("5/minute")
def rate_limit(request: Request):
    return None

@router.get(
    "/{user_key}", response_model=Mapping[str, List]]
)
def get_user(
    user_key: UUID,
    rate_limit = Depends(deps.rate_limit)
) -> Any: 
    pass