cheesechaser.utils.session

This module provides utilities for making HTTP requests with enhanced functionality.

It includes classes and functions for:

  • Creating custom HTTP adapters with default timeouts

  • Generating random user agents

  • Creating and configuring requests sessions with retries and timeouts

  • Making HTTP requests with automatic retries and error handling

The module supports both the requests and httpx libraries for making HTTP requests.

TimeoutHTTPAdapter

class cheesechaser.utils.session.TimeoutHTTPAdapter(*args, **kwargs)[source]

Custom HTTP adapter that sets a default timeout for requests.

Inherits from HTTPAdapter.

Usage: - Create an instance of TimeoutHTTPAdapter and pass it to a requests.Session object’s mount method.

Example:
>>> session = requests.Session()
>>> adapter = TimeoutHTTPAdapter(timeout=10)
>>> session.mount('http://', adapter)
>>> session.mount('https://', adapter)
Parameters:

timeout (int) – The default timeout value in seconds. (default: 10)

get_requests_session

cheesechaser.utils.session.get_requests_session(max_retries: int = 5, timeout: int = 10, headers: Dict[str, str] | None = None, session: Client | None = None, use_httpx: bool = False) Client | Session[source]

Creates and configures a requests or httpx session with retries, timeouts, and custom headers.

This function can create a new session or modify an existing one. It supports both the requests and httpx libraries.

Parameters:
  • max_retries (int) – Maximum number of retries for failed requests. (default: 5)

  • timeout (int) – Timeout value in seconds for requests. (default: DEFAULT_TIMEOUT)

  • headers (Optional[Dict[str, str]]) – Additional headers to add to the session. (default: None)

  • session (Optional[httpx.Client]) – An existing session to modify. If None, a new session is created. (default: None)

  • use_httpx (bool) – Whether to use httpx instead of requests. (default: False)

Returns:

A configured requests.Session or httpx.Client object.

Return type:

Union[httpx.Client, requests.Session]

srequest

cheesechaser.utils.session.srequest(session: Client, method, url, *, max_retries: int = 5, backoff_factor: float = 1.0, raise_for_status: bool = True, **kwargs) Response[source]

Sends an HTTP request with automatic retries and error handling.

This function uses exponential backoff for retries and can raise exceptions for HTTP errors.

Parameters:
  • session (httpx.Client) – The httpx.Client session to use for the request.

  • method (str) – The HTTP method to use (e.g., ‘GET’, ‘POST’).

  • url (str) – The URL to send the request to.

  • max_retries (int) – Maximum number of retries for failed requests. (default: 5)

  • backoff_factor (float) – Factor to calculate the exponential backoff time between retries. (default: 1.0)

  • raise_for_status (bool) – Whether to raise an exception for HTTP errors. (default: True)

  • kwargs – Additional keyword arguments to pass to the request method.

Returns:

The response object from the successful request.

Return type:

httpx.Response

Raises:

Various exceptions related to HTTP errors and request failures.