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.1.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.1.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: qx_http-1.1.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.1.0.tar.gz
Algorithm Hash digest
SHA256 7aae77b415c9e30dabc816ddb76043199f9f360ac313bc72af072974cf7476b9
MD5 569415436343bd88c4b2db1c8eccc6c7
BLAKE2b-256 e18a16ef006b7cafb8cce17e5b681d7fdd251c566a656376789367bf23164478

See more details on using hashes here.

File details

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

File metadata

  • Download URL: qx_http-1.1.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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b6b696c9cc8d3221acbe62514c61fe870e5ae591bf8c4c67f9c318fbbda7e19d
MD5 a6994b9a2464f60c6027b655588e6642
BLAKE2b-256 e9157d9b6bb7da4e4000001508df03ac9f951dc3c5be11aac1673fb55c7bdd32

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