FastAPI Injectable is a lightweight tool that allows developers to utilize FastAPI's powerful dependency injection system beyond route handlers. Whether you're building CLI tools, background tasks, or scheduled jobs, this package offers a seamless solution to reuse dependencies, making development simpler and more efficient.
fastapi-injectable is a lightweight and powerful Python package designed to enhance your development experience by enabling the use of FastAPI's dependency injection system beyond just route handlers. Whether you're working on CLI tools, background tasks, or any non-HTTP context, this package provides a seamless solution for reusing FastAPI dependencies with ease.
The fastapi-injectable library addresses a common challenge that developers face when attempting to incorporate FastAPI dependencies in various contexts beyond the web framework. With this package, you can easily manage dependencies and facilitate reusable injections across different functions, making your code cleaner and more maintainable.
from typing import Annotated
from fastapi import Depends
from fastapi_injectable import injectable
class Database:
def query(self) -> str:
return "data"
def get_db() -> Database:
return Database()
@injectable
def process_data(db: Annotated[Database, Depends(get_db)]) -> str:
return db.query()
result = process_data()
print(result) # Output: 'data'
from typing import Annotated
from fastapi import Depends
from fastapi_injectable import injectable
class Service:
async def process(self) -> None:
print("Processing")
def get_service() -> Service:
return Service()
@injectable
async def async_task(service: Annotated[Service, Depends(get_service)]) -> None:
await service.process()
await async_task() # Output: Processing
from collections.abc import Generator
from fastapi_injectable import injectable
from fastapi_injectable.util import cleanup_all_exit_stacks
class Database:
def query(self) -> str:
return "data"
def cleanup(self) -> None:
print("cleaning")
def get_db() -> Generator[Database, None, None]:
db = Database()
yield db
db.cleanup() # Called on cleanup
@injectable
def process_data(db: Annotated[Database, Depends(get_db)]) -> str:
return db.query()
result = process_data()
await cleanup_all_exit_stacks() # Cleanup all resources
Dependency Caching: Opt for caching of resolved dependencies to enhance performance and maintain instance consistency.
Graceful Shutdown: Integrated utilities ensure proper cleanup during application termination through signal handling.
This package is commonly utilized for:
To see fastapi-injectable in action, check out the following example:
from typing import Annotated
from fastapi import Depends
from fastapi_injectable import injectable
class Database:
def query(self) -> str:
return "data"
def get_db() -> Database:
return Database()
@injectable
def process_data(db: Annotated[Database, Depends(get_db)]) -> str:
return db.query()
result = process_data()
print(result) # Output: 'data'
With a straightforward API and numerous application scenarios, fastapi-injectable is your go-to library for bringing the benefits of FastAPI's dependency injection system to the rest of your Python applications.
No comments yet.
Sign in to be the first to comment.