You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

fastapi-wraps

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastapi-wraps

0.1.4
pipPyPI
Maintainers
1

fastapi-wraps

Python 3.10 codecov license

functools.wraps for endpoints decorators in FastAPI.

It updates the signature of the wrapped function with parameters defined in the decorator's wrapper function. All parameters of the wrapper function should have defaults assigned.

It's advised to name the parameter with some prefix, for example __, to avoid any name conflicts in decorated functions.

To use the Request object in the decorator's wrapper function use __request: Request = Depends(get_request)

Installation

pip install fastapi-wraps

Example

def save_request(
    endpoint: Callable[P, Awaitable[RT]],
) -> Callable[P, Awaitable[RT]]:
    @fastapi_wraps(endpoint)
    async def wrapper(
        *args: Any,
        __request: Request = Depends(get_request),
        __db: Db = Depends(get_db),
        **kwargs: Any,
    ) -> RT:
        __db.save(__request)
        response = await endpoint(*args, **kwargs)
        return response

    return wrapper


app = FastAPI()


@app.get("/")
@save_request
async def hello() -> str:
    return "hello"

Why?

To use dependencies provided by FastAPI's DI framework all dependencies have to be declared in the signature of the endpoint. Hence, the decorator cannot simply use functools.wraps, as functools.wraps maintains the signature of the wrapped function. The fastapi_wraps decorator takes updates the resulting signature by merging parameters from the wrapper and the wrapped function.

Keywords

fastapi

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts