pyiv_common.network

requests-backed NetworkClient for pyiv.

This module provides RequestsClient, a pyiv.network.NetworkClient that uses the requests library. urllib HTTP/HTTPS clients stay in core.

What Problem Does This Solve?

Many apps already depend on requests (sessions, adapters, timeouts). Core pyiv cannot import it. This extra implements the same request(method, url, headers, data, timeout) shape as core clients.

Real-World Use Cases: - Inject an HTTP client that uses the requests stack - Swap urllib HTTPClient for RequestsClient in Config

Example

>>> from pyiv_common.network import RequestsClient
>>> client = RequestsClient()
>>> client.handler_type
'http'
>>> client.request("GET", "ftp://example.com")
Traceback (most recent call last):
    ...
ValueError: RequestsClient only supports http:// and https:// URLs, got: ftp://example.com

Classes

RequestsClient()

HTTP(S) client using the requests library.

class pyiv_common.network.RequestsClient[source]

Bases: NetworkClient

HTTP(S) client using the requests library.

handler_type is "http" so it can replace urllib HTTPClient on the NETWORK_CLIENT chain. Both http:// and https:// URLs are accepted.

Example

>>> from pyiv import ChainType, Config, get_injector
>>> from pyiv_common.network import RequestsClient
>>> class MyConfig(Config):
...     def configure(self):
...         self.register_chain_handler(
...             ChainType.NETWORK_CLIENT, "http", RequestsClient
...         )
>>> injector = get_injector(MyConfig)
>>> client = injector.inject_chain_handler(ChainType.NETWORK_CLIENT, "http")
>>> isinstance(client, RequestsClient)
True
property handler_type: str

Return the handler type identifier (“http”).

request(method: str, url: str, headers: Dict[str, str] | None = None, data: str | bytes | None = None, timeout: float | None = None) → Dict[str, Any][source]

Make an HTTP request with requests.

Parameters:
  • method – HTTP method (GET, POST, PUT, DELETE, etc.)

  • url – The URL to request (must be http:// or https://)

  • headers – Optional dictionary of HTTP headers

  • data – Optional request body (string or bytes)

  • timeout – Optional timeout in seconds

Returns:

  • status: HTTP status code

  • headers: Response headers dictionary

  • body: Response body (bytes)

  • url: Final URL after redirects

Return type:

Dictionary containing

Raises: