ASLabs Dependencies: FastAPI
FastAPI Adapter for the ASLabs Dependencies package.
How to use it:
In your main.py, add the Dependencies additional handler:
from fastapi import FastAPI
from aslabs.dependencies.fastapi import Dependencies
app = FastAPI()
deps = Dependencies(app) # Add line
In your various routers, replace APIRouter with DependencyRouter and use it to register your dependencies:
from aslabs.dependencies.fastapi import DependencyRouter, D
router = DependencyRouter()
router.register_dependencies(lambda deps: deps.add_direct(MyDependencyClass)) # Register your dependencies here
Once registered, use the D parameter function to request dependencies. Notably, you must specify the type as the parameter for D (cannot be infered).
@router.get("/")
def index(dep: MyDependencyClass = D(MyDependencyClass)):
return {"Hello": dep.get_world()}
Once setup, back in your main.py, register the router on deps, not on app. The same parameters can be used as app.include_router
deps.include_router(router)
You are now done.
Adding global dependencies
You can add dependencies directly to the deps object, by calling register_with:
deps.register_with(lambda deps: deps.add_direct(GlobalDependency))
Metadata
Release files for aslabs-dependencies-fastapi 0.0.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| aslabs_dependencies_fastapi-0.0.9.tar.gz | 3.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| aslabs_dependencies_fastapi-0.0.9-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.1 kB
Release files / aslabs_dependencies_fastapi-0.0.9.tar.gz
| Download URL | aslabs_dependencies_fastapi-0.0.9.tar.gz |
|---|---|
| Size | 3.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
88ddbdf9d53934db4d58f92ae9860446b91910d8885379d9eb2c8539757a3119
|
|
BLAKE2b-256 checksum How to use checksums |
dce7ab29e117c7c8395a053f14d770181e1b3ddae47d95a8448dae53c17280f4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.2 CPython/3.9.1 Windows/10
|
Release files / aslabs_dependencies_fastapi-0.0.9-py3-none-any.whl
| Download URL | aslabs_dependencies_fastapi-0.0.9-py3-none-any.whl |
|---|---|
| Size | 3.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5b23ce146e1ed4ae404406178cf3b3277de0aff5e0b3043609da04f33b836282
|
|
BLAKE2b-256 checksum How to use checksums |
4ab3fc5303c25dd0931eb54248fbd7703c41bad82e647cd2989d803d38a5241d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.2 CPython/3.9.1 Windows/10
|