No project description provided
Project description
fastapi-wraps
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.
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-wraps-0.1.3.tar.gz
(3.6 kB
view hashes)
Built Distribution
Close
Hashes for fastapi_wraps-0.1.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 98a942debb735a091900d5fd3e47d3795ccbb55143fdeadcbc4368d476e49e24 |
|
MD5 | ebc18963f6416bc72d806fa10fffca68 |
|
BLAKE2b-256 | 121b6b4834f010424c6774dbd6e4527b76afedbbdc5b680432479632a2725b63 |