Skip to main content

Qx HTTP layer: FastAPI integration, container-aware Depends, Result-to-HTTP mapping, middleware

Project description

qx-http

FastAPI integration layer for the Qx framework — envelope responses, DI bridge, Result-to-HTTP error mapping, middleware, and health probes.

What lives here

  • qx.http.setup_qx_app — one-call FastAPI application factory. Mounts routers, installs middleware, registers exception handlers, and attaches the DI container.
  • qx.http.Inject — FastAPI Depends-compatible DI bridge. Resolves any registered type from the container for use in route function signatures.
  • qx.http.scope_depDepends factory that opens a per-request DI scope, shared across all dependencies in the same request.
  • qx.http.unwrap — extracts the value from a Result[T], raising an ErrorException (translated to HTTP by the exception handler) on failure.
  • qx.http.envelope_success / envelope_failure — build the standard {"success": true, "data": ..., "metadata": ...} response shape.
  • qx.http.ApiResponse / ApiError / ApiMetadata — Pydantic models for the response envelope.
  • qx.http.RequestContextMiddleware — populates RequestContext (request_id, correlation_id, trace_id, tenant_id) from incoming headers for every request.
  • qx.http.MetricsMiddleware — records request duration and status-code counters via Prometheus.
  • qx.http.make_probes_router/healthz (liveness) and /readyz (readiness) endpoints backed by HealthRegistry.
  • qx.http.install_exception_handlers — maps ErrorException subclasses to HTTP status codes (DomainError → 422, NotFoundError → 404, ConflictError → 409, etc.).

Usage

from fastapi import APIRouter, Depends
from qx.http import Inject, envelope_success, scope_dep, setup_qx_app, unwrap
from qx.cqrs import Mediator
from qx.di import Scope

router = APIRouter(prefix="/users")

@router.post("", status_code=201)
async def create_user(
    cmd: CreateUserCommand,
    mediator: Mediator = Inject(Mediator),  # noqa: B008
    scope: Scope = Depends(scope_dep),      # noqa: B008
) -> dict:
    result = await mediator.send(cmd, scope=scope)
    return envelope_success(unwrap(result))


def build_app() -> FastAPI:
    return setup_qx_app(
        settings=settings,
        container=container,
        routers=[router],
        metrics=metrics,
        health=health,
    )

Response envelope

All successful responses follow:

{
  "success": true,
  "data": { ... },
  "metadata": { "page": 1, "page_size": 20, "total": 57 }
}

All error responses follow:

{
  "success": false,
  "error": { "code": "user.not_found", "message": "User 123 not found", "details": {} }
}

Design rules

  • Inject(T) is syntactic sugar for Depends(lambda: container.resolve(T, scope=...)). The scope is shared within a request via FastAPI's dependency deduplication on scope_dep.
  • Do not use from __future__ import annotations in route files — FastAPI reads annotations at runtime for OpenAPI schema generation and body parsing.
  • Runtime imports (not under TYPE_CHECKING) are required for any type used as a FastAPI body parameter, Inject target, or Pydantic field.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qx_http-1.0.0.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qx_http-1.0.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file qx_http-1.0.0.tar.gz.

File metadata

  • Download URL: qx_http-1.0.0.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for qx_http-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8355ac0336ba4330981396f86f564036ec09ef97db89c319996462ed2cc31014
MD5 a0fbe2f5ce930daa8b7389584daaf1e7
BLAKE2b-256 88bde356087c55a451bd5fd601179f38c02faae1ce9166c9f3cb07fe97e79606

See more details on using hashes here.

File details

Details for the file qx_http-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: qx_http-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for qx_http-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b4773601611bda0f508c6e9b6269e6c9fedadff79f6ad2a674aaf68d8341751d
MD5 8537d2596fa217e1df52ea544e7cae0f
BLAKE2b-256 1793c0a89ad2ac2642a93f3849a4ce99e572523e3eb29355d190cd238989a17a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page