HTTP Response


The following attributes of the HTTP response can be used:


The HTTP status code (e.g. 401)


The HTTP reason phrase (e.g. "Unauthorized")


The HTTP headers as an HTTPHeaderDict instance


The body of the response as bytes


The body of the response as an Iterator[bytes]; useful for streaming


The body of the response as a str


The body of the response JSON-decoded


The response object coming from the adapter (e.g. requests.Response)

Usage as a context manager

Using a response as a context manager has two main effects.

First, some allocated resources are cleaned when leaving the context manager.


This depends on the HTTP engine, but for example requests needs this in streaming mode to be able to release connections back to the pool.

Second, all exceptions raised within the context manager are caught an HTTPContextError exception is raised instead.

>>> from sdkite.http import HTTPAdapterSpec, HTTPContextError

>>> class RootClient(Client):
...     _http = HTTPAdapterSpec(url="")
...     def get_user(self, user_id):
...         with self._http.get(f"user/{user_id}") as response:
...             return (response.data_json["name"], response.data_json["age"])

>>> RootClient().get_user(1)
('Alice', 42)

>>> RootClient().get_user(2)
Traceback (most recent call last):
sdkite.http.exceptions.HTTPContextError: KeyError: 'age'

This is useful to have the HTTP context (request, response) in which the issue happened, for example when validating the data returned by the API endpoint.

>>> try:
...     RootClient().get_user(2)
... except HTTPContextError as ex:
...     print(f"Invalid API response in URL {ex.request.url}")
...     print(f"Got JSON {ex.response.data_json}")
...     print(f"Exception raised is {ex}")
Invalid API response in URL
Got JSON {'name': 'Bob'}
Exception raised is KeyError: 'age'


The original exception is available through the __cause__ attribute of the HTTPContextError instance.