The HTTP request engine¶
HttpRequestEngine sends HTTP requests through an httpx.Client and adds what a bare client leaves to you: retries with exponential backoff, per-request timeouts and redirect limits, one credential refresh on 401 Unauthorized, a typed error for every failure, and removal of URL credentials from error messages. The HTTP storage backend uses it for every request, and you can use it directly for any HTTP API.
The engine and its policy, response and error types are exported from mountainash_transport. The engine needs the [http] extra; the policies, responses and errors are standard library only. This page assumes you know what an HTTPConnection provides (HTTPConnection). The examples run against a local http.server started in the same process; its setup is not shown, and base_url is its address.
Making requests¶
Construct the engine around a client you own: HttpRequestEngine(client, policy=None, auth_strategy=None, *, location_check=None). The engine never closes the client. It offers two calls:
request(method, url, *, headers=None, content=None, stream=None, params=None, policy=None)sends a request and returns a fully bufferedHttpResponse. Astream=body is sent in chunks as it is read, never read into memory first: a seekable stream is sent from its current position with aContent-Length, a non-seekable one with chunked transfer encoding.stream(method, url, *, headers=None, content=None, params=None, policy=None)is a context manager that yields anHttpStreamResponsewithout loading the body. Its body must bebytes.
url may be relative when the client has a base_url. Every call can pass its own RequestPolicy through policy=, which replaces the engine's default for that call only. Caller headers override auth headers with the same name.
import httpx
from mountainash_transport import HttpRequestEngine
with httpx.Client(base_url=base_url) as client:
engine = HttpRequestEngine(client)
response = engine.request("GET", "/reports/summary.json")
print(response.status_code, response.headers["content-type"], response.json())
with engine.stream("GET", "/reports/sales.csv") as streamed:
for chunk in streamed.iter_bytes(chunk_size=16):
print(chunk)
Output:
Policies¶
A RequestPolicy is a frozen dataclass composed of three sub-policies, plus one flag. Derive variations with with_retry(), with_timeout() and with_redirect(), which return new instances.
| Field | Type | Defaults |
|---|---|---|
retry |
RetryPolicy |
see Retries |
timeout |
TimeoutPolicy |
connect=10.0, read=30.0, write=30.0, pool=10.0 seconds |
redirect |
RedirectPolicy |
follow_redirects=True, max_redirects=10 |
auth_refresh_on_401 |
bool |
True |
The engine applies the timeout policy to each request, overriding the client's own timeout. Redirects are followed by the engine one hop at a time, under the call's limit; going over it raises HttpRedirectError, and so does a 3xx response when following is turned off.
Checking every URL¶
location_check is a callable that receives each URL the engine is about to send, including redirect targets, and raises to refuse it. The facade passes its location policy check here when a policy is active (Policy on the facade). Any callable works:
from urllib.parse import urlsplit
import httpx
from mountainash_transport import HttpRequestEngine
def only_loopback_ip(url: str) -> None:
if urlsplit(url).hostname != "127.0.0.1":
raise PermissionError(f"refusing {url}")
with httpx.Client(base_url=base_url) as client:
engine = HttpRequestEngine(client, location_check=only_loopback_ip)
print(engine.request("GET", "/reports/sales.csv").status_code)
try:
engine.request("GET", "/moved") # 302 to http://localhost:<port>/...
except PermissionError as exc:
print(exc)
Output (the port differs on each run):
The first hop to 127.0.0.1 passed; the redirect to localhost was refused before it was sent. The exception raised by your check propagates unchanged.
Responses and errors¶
HttpResponse is a frozen dataclass with status_code, headers (a plain dict), content (bytes), url (the final URL, after redirects) and method. text decodes the body as UTF-8; json() parses it and raises HttpDecodeError if it cannot. HttpStreamResponse exposes the same metadata and reads the body through iter_bytes(chunk_size=65536), iter_text() or read(); leaving the with block closes it.
Only 2xx responses are returned. Anything else raises a subclass of HttpTransportError:
| Exception | Cause |
|---|---|
HttpNotFoundError, HttpAuthenticationError, HttpForbiddenError, HttpConflictError, HttpRateLimitError |
404, 401, 403, 409, 429 |
HttpClientError |
any other 4xx (base of the 4xx classes above) |
HttpBadGatewayError, HttpServiceUnavailableError, HttpGatewayTimeoutError |
502, 503, 504 |
HttpServerError |
any other 5xx (base of the 5xx classes above) |
HttpConnectionError |
DNS, TCP, TLS or proxy failure |
HttpTimeoutError |
connect, read, write or pool timeout |
HttpRedirectError |
unfollowed 3xx, or too many redirects |
HttpProtocolError |
malformed HTTP or content decoding |
HttpRequestError |
invalid URL or unsupported scheme |
HttpDecodeError |
json() could not parse the body |
Status errors share the base HttpResponseError, which carries status_code, headers, url and method. It does not carry the response body, which may hold tokens or personal data; read the body from the response yourself when you need it. HttpRateLimitError adds retry_after in seconds when the server sent one.
These classes do not subclass StorageError. When you read through StorageFacade, the HTTP backend translates them into the storage errors described in Errors; when you use the engine directly, catch the HTTP classes.
Retries¶
RetryPolicy decides which failures are tried again. max_attempts counts the first attempt, so the default of 3 means up to two retries. The same class configures SFTP retries; for storage, set it through the profile's RETRY_* fields (Retries). retry_on_status, retry_unsafe_methods and max_retry_after apply to HTTP only.
| Field | Default | Meaning |
|---|---|---|
max_attempts |
3 |
Total attempts, including the first. |
backoff_base, backoff_max |
1.0, 60.0 |
Wait before retry n is backoff_base * 2**(n-1), capped at backoff_max. |
backoff_jitter |
True |
Replace the wait with a random value between 0 and that wait. |
retry_on_status |
(429, 500, 502, 503, 504) |
Statuses that are retried. |
retry_on_transport |
True |
Retry HttpConnectionError and HttpTimeoutError. |
retry_unsafe_methods |
False |
Also retry methods outside IDEMPOTENT_METHODS, such as POST and PATCH. |
max_retry_after |
120.0 |
Longest Retry-After wait honoured, in seconds. |
Statuses other than 429 and transport errors are retried only for methods in IDEMPOTENT_METHODS (GET, HEAD, OPTIONS, TRACE, PUT, DELETE). A 429 is retried for any method; the engine waits for the server's Retry-After value (seconds or an HTTP date), capped at max_retry_after, or uses the backoff when there is none. A 401 followed by a successful credential refresh is not counted as an attempt (see Authentication strategies).
Every re-send follows one rule about the request body. No body, a bytes body or a seekable stream can be sent again: retries for 429, other retryable statuses and transport errors, the 401 refresh, and redirects or authentication challenges all re-send it from the position it started at. A non-seekable stream (a pipe, a socket, a transform's output) is sent once. None of those re-sends happen, and a redirect or challenge that would need one raises HttpRequestError. To make a non-seekable body retryable, pass materialize(stream), which copies it into a seekable buffer.
The example turns backoff off so that it runs instantly. The server answers /flaky with 503 on the first request, /limited with 429 and Retry-After: 0 on the first request, and every POST with 503.
import httpx
from mountainash_transport import (
HttpServiceUnavailableError, HttpRequestEngine, RequestPolicy, RetryPolicy,
)
policy = RequestPolicy(retry=RetryPolicy(max_attempts=3, backoff_base=0, backoff_jitter=False))
with httpx.Client(base_url=base_url) as client:
engine = HttpRequestEngine(client, policy)
print(engine.request("GET", "/flaky").text) # 503, then 200
print(engine.request("GET", "/limited").text) # 429 with Retry-After: 0, then 200
try:
engine.request("POST", "/flaky", content=b"{}")
except HttpServiceUnavailableError as exc:
print(type(exc).__name__, exc.status_code)
print(dict(hits))
Output (hits is the test server's request counter):
ok
ok
HttpServiceUnavailableError 503
{('GET', '/flaky'): 2, ('GET', '/limited'): 2, ('POST', '/flaky'): 1}
Both GETs succeeded on their second attempt; the POST was sent once and failed without a retry.
Waits use time.sleep in the calling thread, so large backoff_max or max_retry_after values block the caller for that long. stream() retries only until it yields the response; once you start reading the body, failures propagate without replay.
Authentication strategies¶
Static credentials, such as a fixed bearer token or basic auth, are rendered into the httpx.Client when the connection is built and need nothing from the engine. The engine's auth_strategy is for credentials that expire. A refreshable strategy implements the RefreshableAuthStrategy protocol (src/mountainash_transport/_core/http/auth_protocol.py) with three methods:
get_headers()returns the current auth headers. The engine calls it before each request.refresh()obtains new credentials and returnsTrueon success. The engine calls it once, on the first401, and resends the request with fresh headers.apply(kwargs)returns a new dict of client keyword arguments. The engine does not call it; it exists for client construction.
Implementations must be thread-safe. If the refresh fails or the retried request also gets 401, the engine raises HttpAuthenticationError. Set auth_refresh_on_401=False in the policy to skip the refresh.
The example implements a strategy whose first token the server has already expired:
import threading
import httpx
from mountainash_transport import HttpRequestEngine
class RotatingToken:
"""A RefreshableAuthStrategy: apply(), get_headers() and refresh()."""
def __init__(self, tokens: list[str]) -> None:
self._tokens = iter(tokens)
self._current = next(self._tokens)
self._lock = threading.Lock()
self.refreshes = 0
def apply(self, kwargs: dict) -> dict:
return dict(kwargs)
def get_headers(self) -> dict[str, str]:
with self._lock:
return {"Authorization": f"Bearer {self._current}"}
def refresh(self) -> bool:
with self._lock:
self._current = next(self._tokens)
self.refreshes += 1
return True
auth = RotatingToken(["token-1", "token-2"])
with httpx.Client(base_url=base_url) as client:
engine = HttpRequestEngine(client, auth_strategy=auth)
print(engine.request("GET", "/secure").text, "after", auth.refreshes, "refresh")
Output:
For OAuth 2.0 you do not write a strategy. mountainash-auth-client owns token acquisition, storage and refresh (see credentials come from auth-client); transport adapts its token manager to this protocol as OAuth2RefreshableAuthStrategy, built by create_auth_strategy(auth_profile, oauth_provider=..., token_store=...) in mountainash_transport.connections. StorageFacade.from_path does this for you when you pass an OAuth2 auth profile together with oauth_provider= and token_store=. The token store stays yours to open and close.
What HTTP errors show¶
URLs sometimes carry credentials as userinfo (https://user:password@host/path) or in the query string (?sig=..., ?token=...). HTTP errors show only the display form of the URL: scheme://host:port/path, with userinfo, query and fragment never shown. HttpResponseError.url holds that display form, and so does every HttpTransportError message. HttpResponseError.headers keeps only an allowlist: accept-ranges, content-encoding, content-length, content-location, content-range, content-type, date, etag, last-modified, location, retry-after and www-authenticate, with location and content-location resolved against the request URL (a relative reference becomes absolute) and then shown in display form; without a request URL a relative reference shows only its path. www-authenticate keeps only its auth-scheme names, in order and comma-separated (Bearer realm="api", error_uri="https://u:p@h/x?sig=s" becomes Bearer), with all parameters dropped. The other allowlisted headers keep their value as sent. Every other header, including Set-Cookie and Authorization, is dropped. Error objects do not carry response bodies, and native httpx exceptions are never chained: an error has no __cause__, and __context__ is cleared. The successful values HttpResponse.url, HttpStreamResponse.url and the path of an HTTP stat StorageEntry have userinfo removed but keep query and fragment, so a presigned URL stays usable. Importing the engine also attaches logging.Filters to the httpx logger and to httpcore's httpcore.connection, httpcore.http11, httpcore.http2, httpcore.proxy and httpcore.socks loggers, whatever level the application sets. The httpx filter rewrites the request URL in its HTTP Request: record to the display form (method and status stay). The httpcore filter reduces records that carry request or response data (header lists, request objects) to their event name; connection records such as connect_tcp.started host='h' port=443 stay.
import socket
import httpx
from mountainash_transport import (
HttpConnectionError, HttpNotFoundError, HttpRequestEngine, RequestPolicy, RetryPolicy,
)
port = base_url.rsplit(":", 1)[1]
with socket.socket() as probe: # a local port nobody listens on
probe.bind(("127.0.0.1", 0))
closed_port = probe.getsockname()[1]
no_retry = RequestPolicy(retry=RetryPolicy(max_attempts=1))
with httpx.Client() as client:
engine = HttpRequestEngine(client, no_retry)
try:
engine.request("GET", f"http://analyst:s3cret@127.0.0.1:{port}/reports/old.csv?sig=abc123")
except HttpNotFoundError as exc:
print(exc)
print(exc.url)
for label, userinfo in (("with userinfo", "analyst:s3cret@"), ("no userinfo", "")):
try:
engine.request("GET", f"http://{userinfo}127.0.0.1:{closed_port}/x")
except HttpConnectionError as exc:
print(f"{label}: cause shown = {exc.__cause__ is not None}")
Output (the port differs on each run):
HTTP 404 GET http://127.0.0.1:34933/reports/old.csv
http://127.0.0.1:34933/reports/old.csv
with userinfo: cause shown = False
no userinfo: cause shown = False
Traceback locals and path segments are not hidden
Path segments are shown, so keep credentials in auth profiles rather than in URLs or path templates. The local variables of the frames in a traceback, such as the URL you passed in, are outside the guarantee, so configure error reporters that capture frame locals accordingly.
Location-policy errors use the same display form (What errors show of a location). The helpers are in src/mountainash_transport/_core/safe_display.py; the HTTP tests are in tests/_core/http/test_http_safe_display.py.