Skip to main content

xtr-http-message-contracts

The HTTP message contract, and nothing else — so two libraries can pass a request between them without agreeing on a class.

python 3.11+ core dependencies: 1 typed license MIT

Why?

An authorization server, a rate limiter and a firewall all read a request and fill in a response. None of them should care whose request object it is — and if each brings its own, the application ends up converting between three shapes of the same thing on every call.

So they need the seam, not an implementation. This package is that seam and nothing else: thirteen protocols and an enumeration, built from the standard library, with one dependency — typing-extensions, for @override on 3.11.

  • 🧩 ServerRequestInterface — what arrived: method, URI, headers, body, cookies, query, uploaded files, parsed body, plus the attributes a handler annotates it with.
  • 📤 ResponseInterface — a status code, a reason phrase, headers and a body.
  • 🌊 StreamInterface — the body itself, as a cursor over bytes.
  • 🔗 UriInterface — a URI taken apart into components and put back together as text.
  • 📎 UploadedFileInterface / UploadError — a file that came in, and how it went.
  • 🏭 Six factories — build any of the above without naming a class.
from xtr_http_message_contracts import ResponseInterface, ServerRequestInterface


async def whoami(
    request: ServerRequestInterface,
    response: ResponseInterface,
) -> ResponseInterface:
    _ = await response.get_body().write(request.get_header_line("user-agent").encode())
    return response.with_header("content-type", "text/plain")

Install

uv add xtr-http-message-contracts

Requires Python 3.11+.

Who installs what

Depends on
A library that handles requests xtr-http-message-contracts at runtime, xtr-http-message as a dev dependency — its tests build real messages.
An application xtr-http-message, which implements the contract, plus the integration that bridges these types to the web framework it runs on.

An implementation builds on these types rather than redefining them. Import them from xtr_http_message_contracts; that one set of types is what lets a container register an implementation under an interface, and a library that never imported the implementation receive it.

The interfaces

Interface What it is
MessageInterface What a request and a response share: protocol version, headers, body.
RequestInterface A message going out: a method and a URI on top.
ServerRequestInterface A request that arrived, with everything the server learned alongside it.
ResponseInterface A message coming back: a status code and a reason phrase on top.
StreamInterface A message body: read, write, seek, and the whole of it in one awaited call.
UriInterface A URI as a value, every component readable.
UploadedFileInterface One file that came in with a request, movable once.

Under xtr_http_message_contracts.factory, one factory each for a request, a server request, a response, a stream, a URI and an uploaded file — RequestFactoryInterface, ServerRequestFactoryInterface, ResponseFactoryInterface, StreamFactoryInterface, UriFactoryInterface, UploadedFileFactoryInterface. They are re-exported at the top level too, so either import works.

The rules that hold across all of them:

  • Messages are immutable. Every with_* method leaves the instance it was called on as it was and returns one carrying the change, so a message handed on cannot be altered behind the caller's back. The return type is Self, so a subtype keeps its own type through a chain.
  • Header names are matched without regard to case, and reported in the case they were first given in. A header the message does not carry reads as an empty sequence from get_header and an empty string from get_header_line — never as an error.
  • A component the URI has not got reads as an empty string, and get_port reads as None when there is no port worth reporting.
  • A stream is the one thing that is not immutable: reading, writing and seeking move its cursor in place. Its payload is bytes, since a body may be binary; await stream.get_contents() rewinds and reads the whole of it, and raises when the body cannot be read.
  • A body is awaited. read, write, get_contents, seek, rewind and close are async, so a body can come off the network as it is asked for; move_to on an uploaded file is too. The methods that only report what the stream already knows — get_size, tell, eof, is_seekable, is_readable, is_writable, get_metadata, detach — are not.
  • with_uri decides the Host header. By default it follows the new URI, and a Host header already there carries over only when the URI has no host; preserve_host=True keeps a non-empty one exactly as it is.

Upload errors

get_error() on an uploaded file, and error= when building one, carry one of these:

Member Value Means
OK 0 The file arrived whole.
INI_SIZE 1 Larger than the server configuration allows.
FORM_SIZE 2 Larger than the submitted form named.
PARTIAL 3 The upload stopped part way.
NO_FILE 4 The field was submitted with no file in it.
NO_TMP_DIR 6 Nowhere to keep the file while receiving it.
CANT_WRITE 7 It could not be written to where it was being kept.
EXTENSION 8 Something loaded on the server refused it.

UploadError is an IntEnum, so a member passes anywhere the int the contract calls for is expected. 5 is absent because that number was never assigned.

What is not here

Anything that is a message rather than describing one: the request, response, stream, URI and uploaded file classes, the status-code phrase table, URI parsing, and the factory that builds all six. That is xtr-http-message.

Anything that bridges a message to a web framework — building one from the call in flight, returning one from a route, registering either on a container — is not here either: it belongs with that framework's integration, which for a FastAPI application is xtr-http-kernel.

Errors

This package defines no exception of its own; a contract with no behaviour has nothing to raise. The interfaces name two builtins instead, so a caller catches the same thing whichever implementation is behind them:

Error Raised when
ValueError An argument cannot be what it is being used as — a malformed header name, an out-of-range port, a status code that is not one
RuntimeError An operation cannot be carried out now — a stream that will not seek, an uploaded file already moved

Development

Developed in the python-xtr monorepo, under packages/xtr-http-message-contracts; run the commands below from there. The python-xtr-http-message-contracts repository is a read-only copy, so send issues and pull requests to the monorepo.

uv sync
uv run ruff check . && uv run ruff format --check .
uv run basedpyright
uv run ty check
uv run pytest

License

MIT — see LICENSE.

Metadata

Release files for xtr-http-message-contracts 6.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xtr-http-message-contracts 6.7.0
File Size Uploaded Provenance Actions
xtr_http_message_contracts-6.7.0.tar.gz 19.7 kB With provenance Details for xtr_http_message_contracts-6.7.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for xtr-http-message-contracts 6.7.0
File Interpreter ABI Platform Actions
xtr_http_message_contracts-6.7.0-py3-none-any.whl Python 3 none any Details for xtr_http_message_contracts-6.7.0-py3-none-any.whl

Total release size: 46.8 kB

Release files / xtr_http_message_contracts-6.7.0.tar.gz

Download URL xtr_http_message_contracts-6.7.0.tar.gz
Size 19.7 kB
Tags Source
SHA-256 checksum
How to use checksums
36df55049c8d0a69f6890c375ab4de50302e33aedf7026750980ea6ac1519b08
BLAKE2b-256 checksum
How to use checksums
d4ccab69046c0d8c11e3c1edda86fdec28be4209290d87d7f3fa249e669010e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.

Transparency log

Release files / xtr_http_message_contracts-6.7.0-py3-none-any.whl

Download URL xtr_http_message_contracts-6.7.0-py3-none-any.whl
Size 27.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d3461abe3fee3c2bd72efb5171848a452d64df988057d3a23f1800e75246f538
BLAKE2b-256 checksum
How to use checksums
46a4be5dde3683f2a0b0dca068b1b80807b2423fac1d562e5cf136dbb81fea2e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.

Transparency log

Security for xtr-http-message-contracts 6.7.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for xtr_http_message_contracts-6.7.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page