loom.rest.fastapi¶
FastAPI-specific REST layer for Loom.
Provides the FastAPI binding for compiled REST interfaces:
- MsgspecJSONResponse — zero-copy JSON response.
- bind_interfaces() — binds compiled routes to FastAPI.
- create_fastapi_app() — composition root.
- class loom.rest.fastapi.MsgspecJSONResponse(*args, **kwargs)[source]¶
Bases:
ResponseFastAPI
Responsesubclass that encodes content withmsgspec.json.Drop-in replacement for
fastapi.responses.JSONResponsewith two advantages:Native
msgspec.Structserialisation — nodictconversion needed.Single encoding pass — content is written directly to bytes with no intermediate JSON string or Pydantic round-trip.
- Parameters:
content – Any object supported by
msgspec.json.encode.status_code – HTTP status code. Defaults to
200.headers – Additional response headers.
media_type – Defaults to
"application/json".background – Optional Starlette background task.
args (Any)
kwargs (Any)
- Return type:
Any
Example:
return MsgspecJSONResponse(content=my_struct, status_code=201)
- loom.rest.fastapi.bind_interfaces(app, compiled_routes, factory, executor)[source]¶
Register compiled routes on a FastAPI application.
For each
CompiledRoute, creates a dynamic async handler and registers it viaapp.add_api_route. Path parameters are inferred from the route path template and exposed in the handler signature so FastAPI validates and documents them correctly.Nested JSON Schema
$defsproduced by msgspec/pydantic are collected into a shared component registry and returned. The caller is responsible for injecting these intocomponents.schemasof the OpenAPI document.- Parameters:
app (fastapi.FastAPI) – FastAPI application instance to register routes on.
compiled_routes (Sequence[CompiledRoute]) – Ordered list of fully resolved routes produced by
RestInterfaceCompiler.factory (UseCaseFactory) – Use-case factory for constructing instances per request.
executor (RuntimeExecutor) – Runtime executor that drives the use-case pipeline.
- Returns:
Mapping of schema name → JSON Schema fragment for all collected
$defsthat should appear undercomponents.schemas.- Return type:
Example:
compiler = RestInterfaceCompiler(use_case_compiler) routes = compiler.compile(UserRestInterface) component_schemas = bind_interfaces(app, routes, factory, executor)
- loom.rest.fastapi.create_app(*config_paths, code_path=None, metrics_registry=None)[source]¶
Create a FastAPI application from one or more YAML config files.
Config files are merged left-to-right — later files override earlier ones. Each file may also declare a top-level
includeslist to pull in additional base files before its own values (seeload_config()).TraceIdMiddlewareis mounted automatically whentrace.enabledistrue(the default). SQLAlchemy SQL echo inheritstrace.enabledunlessdatabase.echois set explicitly. Prometheus middleware is mounted whenmetrics.enabledistrue.- Parameters:
*config_paths (str) – One or more paths to YAML configuration files.
code_path (str | None) – Optional override for
app.code_path. Resolved relative to the first config file when not absolute.metrics_registry (CollectorRegistry | None) – Optional Prometheus
CollectorRegistryused forPrometheusMiddlewareand the scrape endpoint. Defaults to the global registry. Pass a freshCollectorRegistry()in tests to avoidValueError: Duplicated timeserieswhen multiple apps withmetrics.enabled: trueare created in the same process.
- Returns:
Configured
fastapi.FastAPIapplication, ready to serve.- Return type:
FastAPI
Example — single config:
app = create_app("config/app.yaml")
Example — base + environment override:
app = create_app("config/base.yaml", "config/production.yaml")
Example — single file using inline includes:
# config/app.yaml # includes: # - base.yaml # - secrets.yaml app = create_app("config/app.yaml")
- loom.rest.fastapi.create_fastapi_app(result, interfaces, *, middleware=(), defaults=None, **fastapi_kwargs)[source]¶
Create a FastAPI application from a bootstrap result and REST interfaces.
Compiles all
RestInterfacedeclarations viaRestInterfaceCompiler, binds each compiled route to theFastAPIinstance, and returns the ready application.Compilation is fail-fast: any structural error (missing use-case plan, duplicate route, missing prefix) raises
InterfaceCompilationErrorbefore the app starts accepting requests.- Parameters:
result (BootstrapResult) – Fully initialised
BootstrapResultfrombootstrap_app().interfaces (Sequence[type[RestInterface[Any]]]) –
RestInterfacesubclasses declaring which endpoints to expose. Compiled in declaration order.ASGI middleware classes to register on the application. Added in declaration order (first = outermost wrapper). Accepts any class compatible with
FastAPI.add_middleware. Example:from loom.rest.middleware import TraceIdMiddleware from loom.prometheus import PrometheusMiddleware app = create_fastapi_app( result, interfaces=[...], middleware=[TraceIdMiddleware, PrometheusMiddleware], )
defaults (RestApiDefaults | None) – Global REST API defaults (pagination mode, profile policy). Falls back to
RestApiDefaultswhen not provided.**fastapi_kwargs (Any) – Additional keyword arguments forwarded to the
FastAPIconstructor (e.g.title,version,docs_url).
- Returns:
Configured
fastapi.FastAPIinstance ready to serve requests.- Raises:
InterfaceCompilationError – If any interface fails structural validation.
- Return type:
fastapi.FastAPI
Example:
app = create_fastapi_app( result, interfaces=[UserRestInterface, OrderRestInterface], defaults=RestApiDefaults(pagination_mode=PaginationMode.CURSOR), title="My API", version="2.0.0", )