justapi — /docs

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

Override dependencies for testing:

def get_db():
    return ProductionDB()

app = JustAPIApp(dependencies=[Depends(get_db)])

# In tests
def override_get_db():
    return TestDB()

app.dependency_overrides[get_db] = override_get_db

def test_handler():
    response = client.get("/items/")
    assert response.status == 200

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

JustAPI v2.0.9 — Open Source MIT License · Built with WebTUI · GitHub

NORMAL master justapi/docs
utf-8 Top 1:1