qfa.api.app#
Application factory and composition root.
Module attributes
Factory that builds an |
Functions
|
Build an LLM client from the provided settings. |
|
Create and configure the FastAPI application. |
Register all exception handlers on the application. |
Classes
|
Pure ASGI middleware that assigns a unique request ID to every request. |
Pure ASGI middleware that logs every HTTP request. |
- class qfa.api.app.RequestIdMiddleware(app: Callable[[MutableMapping[str, Any], Callable[[], Awaitable[MutableMapping[str, Any]]], Callable[[MutableMapping[str, Any]], Awaitable[None]]], Awaitable[None]])[source]#
Bases:
objectPure ASGI middleware that assigns a unique request ID to every request.
Stores
request_idandstart_utconscope["state"]and adds anX-Request-IDheader to every response.- Parameters:
app (ASGIApp) – The wrapped ASGI application.
- class qfa.api.app.RequestLoggingMiddleware(app: Callable[[MutableMapping[str, Any], Callable[[], Awaitable[MutableMapping[str, Any]]], Callable[[MutableMapping[str, Any]], Awaitable[None]]], Awaitable[None]])[source]#
Bases:
objectPure ASGI middleware that logs every HTTP request.
Logs method, path, status code, duration, request ID, and tenant name (when available). Never logs API keys or request bodies.
- qfa.api.app.build_llm_client(settings: LLMSettings) LiteLLMClient[source]#
Build an LLM client from the provided settings.
- Parameters:
settings (LLMSettings) – The LLM configuration settings.
- Returns:
A configured LLM client instance.
- Return type:
- qfa.api.app.LLMFactory#
Factory that builds an
LLMPortfrom settings.The default is
build_llm_client(real LiteLLM client). Tests can pass their own factory tocreate_appto inject a fake without monkeypatching.alias of
Callable[[LLMSettings],LLMPort]
- qfa.api.app.register_exception_handlers(app: FastAPI) None[source]#
Register all exception handlers on the application.
- Parameters:
app (FastAPI) – The FastAPI application instance.
- qfa.api.app.create_app(*, llm_factory: Callable[[LLMSettings], LLMPort] | None = None) FastAPI[source]#
Create and configure the FastAPI application.
- Parameters:
llm_factory (LLMFactory | None) – Optional override for the LLM-port factory. Defaults to
build_llm_client(the real LiteLLM client). Tests pass a fake factory here to inject a stubbedLLMPortwithout monkeypatching — the lifespan still wraps it inTrackingLLMAdapterexactly as it would the real client.- Returns:
The fully configured application instance.
- Return type:
FastAPI