pyiv_common

Ubiquitous-library integrations for pyiv.

pyiv-common is the #include <stdlib.h> extra for pyiv: PyYAML and requests. Core pyiv stays stdlib-only (including urllib HTTP clients).

What Problem Does This Solve?

Libraries such as PyYAML and requests are everywhere in Python, but they are not the standard library. Putting them in core would break pyiv’s zero-runtime-dependency guarantee. A dedicated package per library would proliferate wheels. pyiv-common is that middle tier.

Real-World Use Cases: - YAML config files via YAMLSerDe - HTTP with the requests stack via RequestsHttpClient - Injecting those types through a pyiv Config

Usage:

>>> from pyiv_common.serde import YAMLSerDe
>>> YAMLSerDe().handler_type
'yaml'
>>> from pyiv_common.network import RequestsHttpClient
>>> client = RequestsHttpClient()
>>> client.request("GET", "ftp://example.com/")
Traceback (most recent call last):
    ...
ValueError: Only http:// and https:// URLs are allowed, got: ftp://example.com/
pyiv_common.RequestsClient

alias of RequestsHttpClient

class pyiv_common.RequestsHttpClient(base_url=None, headers=None, timeout=None, https_only=False, session=None)[source]

Bases: HttpClient

HTTP(S) client using the requests library.

Why this exists: Teams that already standardize on requests can bind this implementation to HttpClient without changing call sites that expect json=, raise_for_status, or streaming lines.

Example

>>> from pyiv import Config, get_injector
>>> from pyiv.http import HttpClient
>>> from pyiv_common.network import RequestsHttpClient
>>> class MyConfig(Config):
...     def configure(self):
...         self.register(HttpClient, RequestsHttpClient)
>>> injector = get_injector(MyConfig)
>>> isinstance(injector.inject(HttpClient), RequestsHttpClient)
True
__init__(base_url=None, headers=None, timeout=None, https_only=False, session=None) → None[source]
close() → None[source]

Release transport resources (no-op by default).

request(method: str, url: str, *, headers: Mapping[str, str] | None = None, params: Mapping[str, Any] | None = None, data: str | bytes | None = None, json: Any = None, timeout: float | None = None) → HttpResponse[source]

Send an HTTP request and return a buffered response.

stream(method: str, url: str, *, headers: Mapping[str, str] | None = None, params: Mapping[str, Any] | None = None, data: str | bytes | None = None, json: Any = None, timeout: float | None = None) → Iterator[StreamResponse][source]

Open a streaming response (context manager).

class pyiv_common.YAMLSerDe[source]

Bases: SerDe

YAML encoding SerDe using PyYAML.

Install with pip install pyiv-common. Register on the ENCODING chain like any other SerDe.

Example

>>> from pyiv import ChainType, Config, get_injector
>>> from pyiv_common.serde import YAMLSerDe
>>> class MyConfig(Config):
...     def configure(self):
...         self.register_chain_handler(ChainType.ENCODING, "yaml", YAMLSerDe)
>>> injector = get_injector(MyConfig)
>>> serde = injector.inject_chain_handler(ChainType.ENCODING, "yaml")
>>> serde.deserialize(serde.serialize({"a": 1}))
{'a': 1}
deserialize(data: str | bytes, target_type: Type[T] | None = None) → T[source]

Deserialize YAML string/bytes back to a Python object.

Parameters:
  • data – The YAML string or bytes

  • target_type – Optional type hint for the expected result type

Returns:

Deserialized Python object

property handler_type: str

Return the handler type identifier (“yaml”).

serialize(obj: Any) → str[source]

Serialize using YAML encoding.

Parameters:

obj – The Python object to serialize

Returns:

YAML string representation

Modules

serde

YAML SerDe for pyiv, backed by PyYAML.

network

requests-backed HttpClient for pyiv.