No project description provided
Project description
fastapi-wraps
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.
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
Built Distribution
File details
Details for the file fastapi-wraps-0.1.4.tar.gz
.
File metadata
- Download URL: fastapi-wraps-0.1.4.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.0 CPython/3.10.2 Linux/5.15.0-1019-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73f4cc07caab3d3cadbed79a97c9101d41d13d8056fc87e34d58fd4d10083944 |
|
MD5 | b3d6b217c345c79a9dedec3407b69b13 |
|
BLAKE2b-256 | 7f22b3c3ce00bc77a4f373284910a1c3e60855c6f15acc3d40baa8417bfc354f |
File details
Details for the file fastapi_wraps-0.1.4-py3-none-any.whl
.
File metadata
- Download URL: fastapi_wraps-0.1.4-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.0 CPython/3.10.2 Linux/5.15.0-1019-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3da9e07e2a294af1b55801b963747cfc08ec88a5d1004eb85623d83a8e044bae |
|
MD5 | cc124727a2a6201b8f16221cd1cb0259 |
|
BLAKE2b-256 | 2df6bdd42585e455b5a456b3a00a7b3e3b72f8f51df70d6c1d5ad2bb166a3075 |