Skip to content

API

pico_httpx.client

Declarative HTTP clients: @http_client classes with @get/@post/ @put/@delete/@patch stub methods.

The decorator generates the implementation: path placeholders bind to the method's parameters, a parameter named json becomes the request body, every other bound parameter becomes a query param (None values are dropped). Sync stubs use a shared httpx.Client; async stubs a shared httpx.AsyncClient. Both close on container shutdown.

http_client(_cls=None, *, base_url='', name='')

Turn a class of request stubs into an injectable HTTP client component.

base_url is either given literally or resolved from config at first request: http.clients.<name>.base_url (name defaults to the class name).

Source code in src/pico_httpx/client.py
def http_client(_cls=None, *, base_url: str = "", name: str = ""):
    """Turn a class of request stubs into an injectable HTTP client component.

    ``base_url`` is either given literally or resolved from config at
    first request: ``http.clients.<name>.base_url`` (``name`` defaults
    to the class name).
    """

    def dec(cls):
        setattr(cls, HTTP_CLIENT_META, {"base_url": base_url, "name": name or cls.__name__})
        if "__init__" not in cls.__dict__:
            cls.__init__ = _make_init()
        cls._pico_httpx_close = cleanup(_close_clients)
        return component(cls)

    return dec(_cls) if _cls is not None else dec

pico_httpx.config

Settings for pico-httpx (prefix http, zero-config).

HttpSettings dataclass

clients maps a client name to its settings, e.g. http.clients.users.base_url. A literal base_url on the decorator wins over config.

Source code in src/pico_httpx/config.py
@configured(target="self", prefix="http", mapping="tree")
@dataclass
class HttpSettings:
    """``clients`` maps a client name to its settings, e.g.
    ``http.clients.users.base_url``. A literal ``base_url`` on the
    decorator wins over config."""

    timeout_seconds: float = 10.0
    # dict[str, Any] (not bare dict): the config builder only walks
    # parameterized mapping annotations.
    clients: dict[str, Any] = field(default_factory=dict)