Decorate FastAPI endpoints with custom decorators.
Project description
FastAPI decorators
Small Python utility for wrapping your FastAPI endpoints in custom decorators.
Installation
pip install fastapi-decorators
Usage
Create a simple decorator that rejects unauthorized requests:
from fastapi_decorators import add_dependencies
from fastapi.security import OAuth2PasswordBearer
from fastapi import Depends
oauth2_scheme = OAuth2PasswordBearer(
tokenUrl="/api/v1/auth/login",
auto_error=False,
)
def authorize(*required_scopes: str):
def decorator(func):
def dependency(
token: Optional[str] = Depends(oauth2_password_scheme),
db_session: Session = Depends(get_db),
):
# Execute your auth logic here
...
return add_dependencies(Depends(dependency))(func)
return decorator
The decorator can be used like so:
@app.put("/users/{user_id}")
@authorize("users:write")
def update_user(*, user_id: int, user_update: UserUpdate):
...
Credits
Inspired by solutions suggested by @gocreating and @dmontagu.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for fastapi_decorators-0.0.9-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 390b02950ce67a72afc50c72db679d462df101e3109e31b1bee2399239631af8 |
|
MD5 | ff42510ef3fc003957759d14ef146fcc |
|
BLAKE2b-256 | 92766dd9c3c6cac3653ba5a6ec78907d689a0e6203f89c38bebd6bf3677ef2ff |