Skip to main content

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:

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):
    ...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_decorators-0.0.2.tar.gz (3.5 kB view hashes)

Uploaded Source

Built Distribution

fastapi_decorators-0.0.2-py3-none-any.whl (3.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page