Skip to content

http

Async HTTP request client implementation built on httpx.

clients module-attribute

clients = set[httpx.AsyncClient]()

For bookkeeping of clients.

We close them on app shutdown.

Client

Client(base_url, headers=None)

A simple HTTP client class with retrying and exponential backoff.

This class uses the tenacity library to retry failed HTTP httpx with exponential backoff and jitter. It also uses a httpx.Session instance to manage HTTP connections and cookies.

headers: Headers that are applied to every request

delete async

delete(path, headers=None)

Make an HTTP DELETE request with retrying and exponential backoff.

This method is a convenience wrapper around the request method that sends an HTTP DELETE request.

Parameters:

Name Type Description Default
path str

The URL path (e.g. "/users/123")

required
headers dict[str, str] | None

HTTP headers to send with the request (optional)

None

Returns:

Type Description
httpx.Response

The httpx.Response object.

Raises:

Type Description
httpx.RequestException

If the request fails and cannot be retried.

get async

get(path, params=None, headers=None)

Make an HTTP GET request with retrying and exponential backoff.

This method is a convenience wrapper around the request method that sends an HTTP GET request.

Parameters:

Name Type Description Default
path str

The URL path (e.g. "/users/123")

required
params dict[str, Any] | None

Query parameters (optional)

None
headers dict[str, str] | None

HTTP headers to send with the request (optional)

None

Returns:

Type Description
httpx.Response

The httpx.Response object.

Raises:

Type Description
httpx.RequestException

If the request fails and cannot be retried.

post async

post(path, content=None, headers=None)

Make an HTTP POST request with retrying and exponential backoff.

This method is a convenience wrapper around the request method that sends an HTTP POST request.

Parameters:

Name Type Description Default
path str

The URL path (e.g. "/users/123")

required
content bytes | None

Data to send in the request body (optional)

None
headers dict[str, str] | None

HTTP headers to send with the request (optional)

None

Returns:

Type Description
httpx.Response

The httpx.Response object.

Raises:

Type Description
httpx.RequestException

If the request fails and cannot be retried.

put async

put(path, content=None, headers=None)

Make an HTTP PUT request with retrying and exponential backoff.

This method is a convenience wrapper around the request method that sends an HTTP PUT request.

Parameters:

Name Type Description Default
path str

The URL path (e.g. "/users/123")

required
content bytes | None

Data to send in the request body (optional)

None
headers dict[str, str] | None

HTTP headers to send with the request (optional)

None

Returns:

Type Description
httpx.Response

The httpx.Response object.

Raises:

Type Description
httpx.RequestException

If the request fails and cannot be retried.

request async

request(method, path, params=None, content=None, headers=None)

Make an HTTP request with retrying and exponential backoff.

This method uses the httpx library to make an HTTP request and the tenacity library to retry the request if it fails. It uses exponential backoff with jitter to wait between retries.

Parameters:

Name Type Description Default
method str

The HTTP method (e.g. "GET", "POST")

required
path str

The URL path (e.g. "/users/123")

required
params dict[str, Any] | None

Query parameters (optional)

None
content bytes | None

Data to send in the request body (optional)

None
headers dict[str, str] | None

HTTP headers to send with the request (optional)

None

Returns:

Type Description
httpx.Response

The httpx.Response object.

Raises:

Type Description
httpx.RequestException

If the request fails and cannot be retried.

ClientException

Bases: Exception

Base client exception.

on_shutdown async

on_shutdown()

Close any clients that have been created.