Advanced Dependencies
Parameterized Dependencies
Section titled “Parameterized Dependencies”Create dependencies that accept arguments:
from justapi import JustAPIApp, Depends
def get_query_filter(field: str, value: str): def dependency(): return {field: value} return dependency
app = JustAPIApp()
@app.get("/items/")def list_items(fresh: str = Depends(get_query_filter("status", "active"))): return {"filter": fresh}Dependency Overrides
Section titled “Dependency Overrides”Override dependencies for testing:
def get_db(): return ProductionDB()
app = JustAPIApp(dependencies=[Depends(get_db)])
# In testsdef override_get_db(): return TestDB()
app.dependency_overrides[get_db] = override_get_db
def test_handler(): response = client.get("/items/") assert response.status == 200Async Generator Dependencies
Section titled “Async Generator Dependencies”async def get_async_db(): db = await create_async_connection() try: yield db finally: await db.close()
@app.get("/items/")async def list_items(db = Depends(get_async_db)): items = await db.fetch_all("SELECT * FROM items") return {"items": items}See Also
Section titled “See Also”- Dependencies with yield — cleanup patterns
- Testing with Overrides — testing dependencies
- Classes as Dependencies — class-based deps