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.10.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b02204594d6ee7322a484ce88e3cfc818b5fffe114f0f35a021f1c7bda94549d |
|
MD5 | 9debabc7134b1d60ad710f52f8dc6ca3 |
|
BLAKE2b-256 | cf09b96806daf74d8d03f39de1aa8ada7b5d7114cf34c4e9611c167f49a5807b |
Close
Hashes for fastapi_decorators-0.0.10-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca849910a9ce89a95cd54a012f7f447475791f57af15b37e7a80e59575ce59c6 |
|
MD5 | 5305b6224a5944aa542ed02a6313b6cf |
|
BLAKE2b-256 | 79b5de78a67fb96269354948e8b2b11d08b2dbef50c27c0b5174546852df78db |