httk.serve.http.openapi.app¶
The deliberately small OpenAPI 3.1-to-Starlette adapter.
Attributes¶
Exceptions¶
Report an unsupported or internally inconsistent OpenAPI contract. |
|
Report adapter-generated request parsing or validation failure. |
Classes¶
Describe one supported string OpenAPI parameter. |
|
Describe one supported OpenAPI operation. |
|
Normalized values passed to an OpenAPI operation handler. |
|
A handler response constrained by the matched OpenAPI operation. |
Functions¶
|
Parse the supported OpenAPI 3.1 path subset. |
|
Create a Starlette app from a constrained OpenAPI 3.1 contract. |
Module Contents¶
- type httk.serve.http.openapi.app.RequestErrorHandler = Callable[['OpenAPIRequestError'], 'OpenAPIResponse'][source]¶
- type httk.serve.http.openapi.app.ExceptionHandler = Callable[[Exception, 'OpenAPIRequest'], 'OpenAPIResponse | Awaitable[OpenAPIResponse]'][source]¶
- type httk.serve.http.openapi.app.RequestScope = Callable[['OpenAPIRequest'], contextlib.AbstractAsyncContextManager['OperationContext']][source]¶
- exception httk.serve.http.openapi.app.OpenAPIContractError[source]¶
Bases:
ValueErrorReport an unsupported or internally inconsistent OpenAPI contract.
- exception httk.serve.http.openapi.app.OpenAPIRequestError(operation, detail, request)[source]¶
Bases:
ValueErrorReport adapter-generated request parsing or validation failure.
- Parameters:
operation (OpenAPIOperation) – The matching operation.
detail (str) – Human-readable request failure detail.
request (OpenAPIRequest) – Partial normalized request values.
- class httk.serve.http.openapi.app.OpenAPIParameter[source]¶
Describe one supported string OpenAPI parameter.
- Parameters:
name – Parameter name as declared by OpenAPI.
location –
path,query, orheader.required – Whether the parameter must be sent.
enum – Optional exact set of accepted string values.
- class httk.serve.http.openapi.app.OpenAPIOperation[source]¶
Describe one supported OpenAPI operation.
- Parameters:
method – Lowercase HTTP method.
path – OpenAPI path template.
operation_id – Unique operation identifier.
parameters – Supported path, query, and header parameter contracts.
request_schema – Required JSON request schema identifier, if any.
responses – Exact status, media type, and schema response contracts.
success_status – The single declared 2xx status, or
Nonewhen the operation declares zero or more than one.
- parameters: tuple[OpenAPIParameter, Ellipsis][source]¶
- property success_contracts: tuple[tuple[str | None, str | None], Ellipsis][source]¶
Return the media type and schema contracts declared for the success status.
- Returns:
The declared
(media type, schema id)pairs forsuccess_status, or an empty tuple when there is no single declared success status.- Return type:
- class httk.serve.http.openapi.app.OpenAPIRequest[source]¶
Normalized values passed to an OpenAPI operation handler.
- Parameters:
operation – Matched OpenAPI operation.
path_params – Normalized route parameters.
query – Query parameters, retaining Starlette’s last-value semantics.
headers – Lowercase HTTP header names and values.
body – Validated JSON body, or
Nonefor bodyless operations.
- operation: OpenAPIOperation[source]¶
- path_params: collections.abc.Mapping[str, str][source]¶
- query: collections.abc.Mapping[str, str][source]¶
- headers: collections.abc.Mapping[str, str][source]¶
- class httk.serve.http.openapi.app.OpenAPIResponse[source]¶
A handler response constrained by the matched OpenAPI operation.
- Parameters:
status – Exact declared HTTP status, or
Noneto use the matched operation’s declared success status.body – Optional JSON-compatible response body.
media_type – Exact declared media type; inferred only when unambiguous.
headers – Additional HTTP response headers.
after_response – Optional zero-argument coroutine callback run once after the response has been sent.
- headers: collections.abc.Mapping[str, str][source]¶
- after_response: httk.serve.http.apptypes.ResponseHook | None = None[source]¶
- httk.serve.http.openapi.app.parse_openapi_operations(document)[source]¶
Parse the supported OpenAPI 3.1 path subset.
Local references may be used for path items, operations, parameters, request bodies, and responses. Bodies use external JSON Schema references. Supported parameters are path, query, and header parameters with a simple schema.
- Parameters:
document (collections.abc.Mapping[str, Any]) – Caller-owned OpenAPI document mapping.
- Returns:
Operations in document order.
- Raises:
OpenAPIContractError – If the document uses an unsupported construct.
- Return type:
tuple[OpenAPIOperation, Ellipsis]
- httk.serve.http.openapi.app.create_openapi_app(contract, operations, *, implementation=None, schemas=None, request_error_handler, exception_handlers=None, request_scope=None, scope_names=(), lifespan=None, debug=False, path_converters=None)[source]¶
Create a Starlette app from a constrained OpenAPI 3.1 contract.
contractaccepts either anOpenAPIContract, which already bundles its offline schema registry, or a plain OpenAPI document mapping paired with a separateschemasregistry.Each
operationsentry is a bare handler callable or anOperationBinding. The framework binds the operation’s declared path, query, and header parameters, the validated request body, and any whole-request injection to the handler’s parameters by name; seeoperation().- Parameters:
contract (OpenAPIContract | Mapping[str, Any]) – Parsed contract, or a caller-owned OpenAPI path document.
operations (Mapping[str, Callable[..., Any] | OperationBinding]) – Operation-id-to-handler mapping.
implementation (object | None) – Object whose methods resolve class-defined function entries;
Noneuses each entry callable directly.schemas (httk.serve.http.openapi.schemas.OpenAPISchemaRegistry | None) – Offline JSON Schema registry for external body references. Required and used when
contractis a plain mapping; must not be supplied whencontractis anOpenAPIContract.request_error_handler (RequestErrorHandler) – Converts request parsing or schema errors to a response.
exception_handlers (collections.abc.Mapping[type[Exception], ExceptionHandler] | None) – Exact protocol exception classes converted to responses.
request_scope (RequestScope | None) – Optional per-request async context manager entered around each handler call. It populates the
OperationContextextras before the handler runs and may set response metadata after it returns; that metadata is folded into the response on normal completion only, never onto an error or adapted-exception response.scope_names (collections.abc.Sequence[str]) – Names of the request-scope values the scope may supply, against which each operation’s declared extras are validated.
lifespan (collections.abc.Callable[[httk.serve.http.apptypes.ServeApp], Any] | None) – Optional Starlette lifespan callable.
debug (bool) – Whether Starlette debug responses are enabled.
path_converters (collections.abc.Mapping[str, str] | None) – OpenAPI path parameter to Starlette converter mapping.
- Returns:
Mountable Starlette application.
- Raises:
OpenAPIContractError – If the operations or the contract are incomplete or unsupported, if a handler cannot satisfy an operation’s declared inputs by name, or if
contractandschemasdisagree about which schema registry to use.- Return type: