sinch.core.ports.http_transport module

class sinch.core.ports.http_transport.HTTPTransport(sinch)[source]

Bases: ABC

Base class for HTTP transports.

Subclasses implement send_request() to perform the raw HTTP call. The public request() method adds cross-cutting concerns on top: request preparation, authentication, and automatic token refresh on 401.

Deprecated since version 2.1: Overriding send() (the old send(endpoint) hook) is still honored but deprecated; implement send_request() instead. The send override path will be removed in 3.0.

MAX_RETRIES = 3
RETRYABLE_STATUS_CODES = frozenset({429})
BACKOFF_BASE_SECONDS = 1.0
BACKOFF_GROWTH = 4
RETRY_AFTER_JITTER = 0.25
send_request(
request_data: HttpRequest,
) HTTPResponse[source]

Performs a single HTTP round-trip for an already-prepared, authenticated request.

Parameters:

request_data (HttpRequest) – The prepared request to send.

Returns:

The HTTP response.

Return type:

HTTPResponse

send(
endpoint: HTTPEndpoint,
) HTTPResponse[source]

Prepares, authenticates and performs a single round-trip for an endpoint.

Deprecated since version 2.1: This hook is deprecated. Implement send_request() instead; the send override path will be removed in 3.0.

Parameters:

endpoint (HTTPEndpoint) – The endpoint to call.

Returns:

The HTTP response.

Return type:

HTTPResponse

request(
endpoint: HTTPEndpoint,
) HTTPResponse[source]

Sends a request, renewing the token and retrying once on an expired-token 401.

Parameters:

endpoint (HTTPEndpoint) – The endpoint to call.

Returns:

The handled HTTP response.

Return type:

HTTPResponse

authenticate(
endpoint: HTTPEndpoint,
request_data: HttpRequest,
) HttpRequest[source]

Stamps the credentials required by the endpoint’s auth scheme onto the request.

Parameters:
  • endpoint (HTTPEndpoint) – The endpoint being called, whose HTTP_AUTHENTICATION selects the scheme.

  • request_data (HttpRequest) – The request to authenticate, mutated in place.

Returns:

The same request, with auth applied.

Return type:

HttpRequest

Raises:

ValidationException – If the credentials required by the scheme are missing.

prepare_request(
endpoint: HTTPEndpoint,
) HttpRequest[source]

Builds the HttpRequest for an endpoint.

Parameters:

endpoint (HTTPEndpoint) – The endpoint to build the request for.

Returns:

The prepared request.

Return type:

HttpRequest

static deserialize_json_response(response: Response) dict[source]

Parses the JSON body of a response.

Parameters:

response (Response) – The raw HTTP response.

Returns:

The parsed body.

Return type:

dict

Raises:

SinchException – If the body is present but not valid JSON.