Skip to content

Http session factory

Create preconfigured sync and async HTTP clients for Eve Auth Manager.

async_client_manager(user_agent: str = USER_AGENT) -> AsyncIterator[AsyncClient] async

Yield a configured async HTTP client and close it automatically on exit.

Parameters:

Name Type Description Default
user_agent str

User-Agent header value to send with requests.

USER_AGENT

Yields:

Type Description
AsyncIterator[AsyncClient]

Configured asynchronous HTTP client.

Source code in src/pfmsoft/eve_snippets/httpx2/http_session_factory.py
@asynccontextmanager
async def async_client_manager(
    user_agent: str = USER_AGENT,
) -> AsyncIterator[AsyncClient]:
    """Yield a configured async HTTP client and close it automatically on exit.

    Args:
        user_agent: User-Agent header value to send with requests.

    Yields:
        Configured asynchronous HTTP client.
    """
    client: AsyncClient | None = None
    try:
        client = await config_async_http_client(user_agent)
        yield client
    finally:
        if client is not None:
            await client.aclose()

client_manager(user_agent: str = USER_AGENT) -> Iterator[Client]

Yield a configured HTTP client and close it automatically on exit.

Parameters:

Name Type Description Default
user_agent str

User-Agent header value to send with requests.

USER_AGENT

Yields:

Type Description
Client

Configured synchronous HTTP client.

Source code in src/pfmsoft/eve_snippets/httpx2/http_session_factory.py
@contextmanager
def client_manager(user_agent: str = USER_AGENT) -> Iterator[Client]:
    """Yield a configured HTTP client and close it automatically on exit.

    Args:
        user_agent: User-Agent header value to send with requests.

    Yields:
        Configured synchronous HTTP client.
    """
    client: Client | None = None
    try:
        client = config_http_client(user_agent)
        yield client
    finally:
        if client is not None:
            client.close()

config_async_http_client(user_agent: str = USER_AGENT) -> AsyncClient async

Create an async HTTP client configured with the project User-Agent header.

Parameters:

Name Type Description Default
user_agent str

User-Agent header value to send with requests. Defaults to the project setting.

USER_AGENT

Returns:

Type Description
AsyncClient

Configured asynchronous HTTP client.

Note

The caller owns the returned client and must close it when finished.

Source code in src/pfmsoft/eve_snippets/httpx2/http_session_factory.py
async def config_async_http_client(user_agent: str = USER_AGENT) -> AsyncClient:
    """Create an async HTTP client configured with the project User-Agent header.

    Args:
        user_agent: User-Agent header value to send with requests. Defaults to
            the project setting.

    Returns:
        Configured asynchronous HTTP client.

    Note:
        The caller owns the returned client and must close it when finished.
    """
    return AsyncClient(headers={"User-Agent": user_agent})

config_http_client(user_agent: str = USER_AGENT) -> Client

Create an HTTP client configured with the project User-Agent header.

Parameters:

Name Type Description Default
user_agent str

User-Agent header value to send with requests. Defaults to the project setting.

USER_AGENT

Returns:

Type Description
Client

Configured synchronous HTTP client.

Note

The caller owns the returned client and must close it when finished.

Source code in src/pfmsoft/eve_snippets/httpx2/http_session_factory.py
def config_http_client(user_agent: str = USER_AGENT) -> Client:
    """Create an HTTP client configured with the project User-Agent header.

    Args:
        user_agent: User-Agent header value to send with requests. Defaults to
            the project setting.

    Returns:
        Configured synchronous HTTP client.

    Note:
        The caller owns the returned client and must close it when finished.
    """
    return Client(headers={"User-Agent": user_agent})