emailnator.asyncio.builders package

Subpackages

Submodules

emailnator.asyncio.builders.builders module

Emailnator HTTP client singleton module.

This module provides a thread-safe singleton wrapper around httpx.Client that handles automatic fetching, decoding, and refreshing of XSRF tokens from cookies, as well as preparation of standard HTTP headers.

Typical Usage Example:

from emailnator.asyncio.builders.builders import AsyncEmailnatorClient

# Use as a context manager with emailnator_client as client:

headers = client.get_headers() token = client.get_xsrf_token() http_client = client.get_client()

# Or use methods directly emailnator_client.refresh_token() emailnator_client.close()

class emailnator.asyncio.builders.builders.AsyncEmailnatorClient(*args: Any, **kwargs: Any)

Bases: object

A singleton wrapper around httpx.AsyncClient that manages XSRF tokens and standard headers.

This class ensures that only one instance exists and provides thread-safe methods to interact with the HTTP client. It automatically handles fetching, decoding, and refreshing the XSRF token from cookies, as well as preparing default headers.

The following functionality is provided:
  • Access to the HTTP client via get_client().

  • Fetching or automatically ensuring the XSRF token and headers.

  • Forcibly refreshing the XSRF token with refresh_token().

  • Closing the client and clearing internal state with close().

  • Context manager support via with statements.

_client

The HTTP client used for making requests.

Type:

httpx.AsyncClient

_xsrf_token

The XSRF token for CSRF protection, initially None.

Type:

Optional[str]

_headers

A dictionary storing HTTNoneinternal_lock (threading.Lock): A reentrant lock for thread-safe operations.

Type:

dict[str, str]

_initialized

Flag indicating whether the instance has been initialized.

Type:

bool

async close()

Closes the HTTP client and resets internal state.

This method should be called when the application is shutting down to properly close the httpx.AsyncClient and clear session-related data.

Return type:

None

It performs the following actions:
  • Closes the HTTP client (_client) safely.

  • Resets the XSRF token (_xsrf_token) to None.

  • Clears the HTTP headers (_headers).

async get_client()

Returns the underlying HTTP client for making requests.

Returns:

The httpx.AsyncClient instance used by this singleton.

Return type:

httpx.Client

async get_headers()
Return type:

dict[str, str]

async get_xsrf_token()
Return type:

str

async refresh_token()
Return type:

None

Module contents

class emailnator.asyncio.builders.AsyncEmailnatorClient(*args: Any, **kwargs: Any)

Bases: object

A singleton wrapper around httpx.AsyncClient that manages XSRF tokens and standard headers.

This class ensures that only one instance exists and provides thread-safe methods to interact with the HTTP client. It automatically handles fetching, decoding, and refreshing the XSRF token from cookies, as well as preparing default headers.

The following functionality is provided:
  • Access to the HTTP client via get_client().

  • Fetching or automatically ensuring the XSRF token and headers.

  • Forcibly refreshing the XSRF token with refresh_token().

  • Closing the client and clearing internal state with close().

  • Context manager support via with statements.

_client

The HTTP client used for making requests.

Type:

httpx.AsyncClient

_xsrf_token

The XSRF token for CSRF protection, initially None.

Type:

Optional[str]

_headers

A dictionary storing HTTNoneinternal_lock (threading.Lock): A reentrant lock for thread-safe operations.

Type:

dict[str, str]

_initialized

Flag indicating whether the instance has been initialized.

Type:

bool

async close()

Closes the HTTP client and resets internal state.

This method should be called when the application is shutting down to properly close the httpx.AsyncClient and clear session-related data.

Return type:

None

It performs the following actions:
  • Closes the HTTP client (_client) safely.

  • Resets the XSRF token (_xsrf_token) to None.

  • Clears the HTTP headers (_headers).

async get_client()

Returns the underlying HTTP client for making requests.

Returns:

The httpx.AsyncClient instance used by this singleton.

Return type:

httpx.Client

async get_headers()
Return type:

dict[str, str]

async get_xsrf_token()
Return type:

str

async refresh_token()
Return type:

None