qfa.api.dependencies#

FastAPI dependency functions for authentication and service injection.

Functions

authenticate_request(request, credentials)

Validate a Bearer token from the Authorization header.

call_scope_for(operation)

Build a FastAPI dependency that enters call_scope for operation.

get_analyze_service(request)

Return the analyze service from app state.

get_auth_orchestrator(request)

Return the auth orchestrator from app state.

get_coding_service(request)

Return the coding service from app state.

get_sensitivity_service(request)

Return the sensitivity-detection service from app state.

get_summarize_service(request)

Return the summarisation service from app state.

get_usage_repo(request)

Return the usage repository from app state.

require_superuser(tenant)

FastAPI dependency that authenticates and checks superuser status.

qfa.api.dependencies.get_sensitivity_service(request: Request) SensitivityService[source]#

Return the sensitivity-detection service from app state.

One provider per use-case service (ADR-017): the detect-sensitive handler depends on this service alone, not on the type that reaches every use case.

Parameters:

request (Request) – The incoming HTTP request.

Returns:

The sensitivity-detection service instance.

Return type:

SensitivityService

qfa.api.dependencies.get_coding_service(request: Request) CodingService[source]#

Return the coding service from app state.

One provider per use-case service, per ADR-017: the assign-codes route annotates against CodingService alone, so its dependency surface is readable from the handler signature.

Parameters:

request (Request) – The incoming HTTP request.

Returns:

The coding service instance.

Return type:

CodingService

qfa.api.dependencies.get_analyze_service(request: Request) AnalyzeService[source]#

Return the analyze service from app state.

One provider per use-case service (ADR-017): the analyze route depends on this rather than on a shared service, so its type signature names exactly the service it uses.

Parameters:

request (Request) – The incoming HTTP request.

Returns:

The analyze service instance.

Return type:

AnalyzeService

qfa.api.dependencies.get_summarize_service(request: Request) SummarizeService[source]#

Return the summarisation service from app state.

One provider per use-case service, so each route handler annotates against the single service it actually uses (ADR-017).

Parameters:

request (Request) – The incoming HTTP request.

Returns:

The summarisation service instance.

Return type:

SummarizeService

qfa.api.dependencies.get_auth_orchestrator(request: Request) AuthOrchestrator[source]#

Return the auth orchestrator from app state.

Parameters:

request (Request) – The incoming HTTP request.

Returns:

The auth orchestrator service instance.

Return type:

AuthOrchestrator

qfa.api.dependencies.get_usage_repo(request: Request) UsageRepositoryPort[source]#

Return the usage repository from app state.

Parameters:

request (Request) – The incoming HTTP request.

Returns:

The usage repository instance.

Return type:

UsageRepositoryPort

async qfa.api.dependencies.authenticate_request(request: Request, credentials: HTTPAuthorizationCredentials = Security(dependency=<fastapi.security.http.HTTPBearer object>, use_cache=True, scope=None, scopes=None)) TenantApiKey[source]#

Validate a Bearer token from the Authorization header.

Parameters:
  • request (Request) – The incoming HTTP request.

  • credentials (HTTPAuthorizationCredentials) – The parsed Authorization header credentials.

Returns:

The authenticated tenant API key.

Return type:

TenantApiKey

Raises:

AuthenticationError – If the credentials are missing or invalid.

qfa.api.dependencies.call_scope_for(operation: Operation) Callable[[...], AsyncIterator[CallContext]][source]#

Build a FastAPI dependency that enters call_scope for operation.

The returned dependency reads the authenticated tenant from authenticate_request(), enters call_scope for the duration of the request, and yields the resulting CallContext. It is the driving adapter’s contribution to the cross-adapter correlation bridge: the route declares which operation it represents, and the dependency arranges for current_call_context to be set before the route body (and the application service beneath it) runs.

This is then used for anything that requires the call context, such at usage tracking in TrackingLLMAdapter.

Use inline at the route, e.g. Depends(call_scope_for(Operation.ANALYZE)). FastAPI evaluates the default value once at module-load time (when the route function is defined), so there’s no per-request cost to inlining.

Parameters:

operation (Operation) – The public use-case operation this dependency represents.

Returns:

A FastAPI dependency suitable for Depends(...).

Return type:

Callable[…, AsyncIterator[CallContext]]

qfa.api.dependencies.require_superuser(tenant: TenantApiKey = Depends(dependency=<function authenticate_request>, use_cache=True, scope=None)) TenantApiKey[source]#

FastAPI dependency that authenticates and checks superuser status.

Parameters:

tenant (TenantApiKey) – The authenticated tenant (injected by authenticate_request).

Returns:

The authenticated superuser tenant.

Return type:

TenantApiKey

Raises:

AuthorizationError – If the tenant is not a superuser.