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
HTTP(S) client using the requests library. |
- class pyiv_common.network.RequestsClient[source]
Bases:
NetworkClientHTTP(S) client using the requests library.
handler_typeis"http"so it can replace urllibHTTPClienton the NETWORK_CLIENT chain. Bothhttp://andhttps://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
- 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:
- Returns:
status: HTTP status code
headers: Response headers dictionary
body: Response body (bytes)
url: Final URL after redirects
- Return type:
Dictionary containing
- Raises:
ValueError – If URL is not http:// or https://
requests.RequestException – If the request fails