Skip to main content
Version: Next

HttpClientBase

Shared configuration and utilities for HTTP clients.

Provides common functionality for both sync and async HTTP clients including: header construction, parameter parsing, request body preparation, URL building, timeout calculation, and error classification.

Subclasses should call super().__init__() to initialize shared configuration. The helpers are then used by the inherited call, and stay available to a client that replaces it.

Hierarchy

Index

Methods

__init__

  • __init__(*, token, timeout_short, timeout_medium, timeout_long, timeout_max, max_retries, min_delay_between_retries, statistics, headers, http_compressor): None
  • Initialize the HTTP client base.


    Parameters

    • optionalkeyword-onlytoken: str | None = None

      Apify API token for authentication.

    • optionalkeyword-onlytimeout_short: timedelta = DEFAULT_TIMEOUT_SHORT

      Default timeout for short-duration API operations (simple CRUD operations, ...).

    • optionalkeyword-onlytimeout_medium: timedelta = DEFAULT_TIMEOUT_MEDIUM

      Default timeout for medium-duration API operations (batch operations, listing, ...).

    • optionalkeyword-onlytimeout_long: timedelta = DEFAULT_TIMEOUT_LONG

      Default timeout for long-duration API operations (long-polling, streaming, ...).

    • optionalkeyword-onlytimeout_max: timedelta = DEFAULT_TIMEOUT_MAX

      Maximum timeout cap for any single request attempt, including tier and per-call timeouts.

    • optionalkeyword-onlymax_retries: int = DEFAULT_MAX_RETRIES

      Maximum number of retries for failed requests.

    • optionalkeyword-onlymin_delay_between_retries: timedelta = DEFAULT_MIN_DELAY_BETWEEN_RETRIES

      Minimum delay between retries.

    • optionalkeyword-onlystatistics: ClientStatistics | None = None

      Statistics tracker for API calls. Created automatically if not provided.

    • optionalkeyword-onlyheaders: dict[str, str] | None = None

      Additional HTTP headers to include in all requests.

    • optionalkeyword-onlyhttp_compressor: HttpCompressor | None = None

      Compressor used to compress request bodies. Defaults to GzipHttpCompressor.

    Returns None

is_retryable_transport_error

  • is_retryable_transport_error(exc): bool
  • Return whether an underlying HTTP-library exception is retryable.

    The default classifies nothing as retryable, so a transport that doesn't override it gives up on the first connection failure. Every transport adapter should map its own transient error types here.


    Parameters

    • exc: Exception

    Returns bool

is_timeout_error

  • is_timeout_error(exc): bool
  • Return whether an exception represents a transport timeout.

    Recognizes Python's own TimeoutError. Transport adapters extend it with the timeout types their HTTP library defines.


    Parameters

    • exc: Exception

    Returns bool

set_default_authorization

  • set_default_authorization(token): None
  • Set the Authorization header from the token, unless an authorization header is already configured.


    Parameters

    • token: str

      The Apify API token to set as the Bearer authorization.

    Returns None