No project description provided
Project description
Fastapi depends extension
Purpose
This library extends capabilities of standard Depends di in FastaAPI and add Resources that creating only once and than dependency always return created object at first time. This is convenient when we create some kind of connection in the database or want to use a long-time object for the entire application. The extension can also correctly handle resource closures on shutdown application in the familiar Depends style. With this extension you don't need to create global variables and other strange things in your code.
Quickstart
- Install extension
pip install fastapi-depends-extension
- Create FastApi application and setup di extenstion
from fastapi import FastAPI, Depends
from extdepends import setup_extend_di, on_di_shutdown
app = FastAPI(lifespan=on_di_shutdown)
setup_extend_di(app)
or you can extend your own lifespan
from extdepends import setup_extend_di, on_di_shutdown
async def app_lifespan(app):
print("your own code")
yield
await on_di_shutdown(app)
print("your close code")
app = FastApi(lifespan=app_lifespan)
- After that you can define your resource and use it in routes almost just like common Depends function, resource only does not support callable classes. All current methods for creating resources are described below. Notice that function resource_with_close will always return the same resource. Also it is not possible call this function call outside di FastAPI, because of serious modification under the hood.
@resource
async def resource_with_close():
print("open resource logic")
yield "resource"
print("close resource login on shutdown application")
app.get("/test")
async def test(dep=Depends(resource_with_close)):
return dep
also you can define others Depends resources or common depends function in resource and it will be work
@resource
def setting():
return {"setting": 12}
def common_depends():
return 12
@resource
async def resource_with_close(settings=Depends(setting), common=Depends(common_depends)):
print(f"open resource logic, with setting: {settings} and common depends {common}")
yield "resource"
print("close resource on shutdown resource")
All methods for creating different resources:
- Resource with closing supports asynchronous and synchronous generators:
@resource
async def async_resource_with_close():
print("open async resource")
yield "resource"
print("close resource on shutdown app")
@resource
def sync_resource_with_close():
print("open sync resource")
yield "resource"
print("close resource on shutdown app")
- Resource with closing has contextmanager support is such way:
from contextlib import asynccontextmanager, contextmanager
@resource
@asynccontextmanager
async def async_context_manager():
print("open async context manager depends")
yield "resource"
print("close async context manager")
@resource
@contextmanager
async def sync_context_manager():
print("open sync context manager depends")
yield "resource"
print("close sync context manager depends")
The resource will open a context manager and return the resource that was used throughout the lifecycle of the application. Upon shutdown, the context manager will be closed.
- Resource without close logic, it's just like singletone
@resource
async def async_resource():
await asyncio.sleep(2)
return "resource"
@resource
def sync_resource():
return "resource"
Limitations
- Callable classes has not support yet
- Does not support functions with *args and **kwargs in arguments
- Decorated functions also may not work correctly
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_depends_extension-0.2.1.tar.gz
.
File metadata
- Download URL: fastapi_depends_extension-0.2.1.tar.gz
- Upload date:
- Size: 3.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | acacaf46e41ae40b6c8e07e8e84ca6c8c8b94d7b3b79e9d201b03b20310e92d9 |
|
MD5 | 2c7127e16fc4a05e1f08882e6ee69ccc |
|
BLAKE2b-256 | 9287c59a75081e225ab633bb8f30ec688168417ce81e9cac68c4b02ba965c933 |
File details
Details for the file fastapi_depends_extension-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: fastapi_depends_extension-0.2.1-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52ee70859dd2dfae1b3561ab41c18342afe319f2ceb79e117d1abbfbae0678c8 |
|
MD5 | bdb2264a646bbcac8ba5ab217e804115 |
|
BLAKE2b-256 | b4cce4cb62f17d6a0767a5ec3fc67ca878eee8445cc367421f6f53a9b449223d |