Skip to main content

Asyncly

PyPI version Python versions License Documentation

A tiny async HTTP client and a real aiohttp mock server for testing your integrations — built on aiohttp.

asyncly gives you two pieces that fit together:

  • BaseHttpClient — a thin, typed base class for HTTP clients with per-status response handlers, policy-driven retries, flexible timeouts, and first-class proxy support. InstrumentableHttpClient adds pluggable metrics (Prometheus / OpenTelemetry): requests, in-flight, network phases, and connection-pool stats.
  • srvmocker — spin up a real aiohttp test server (not a transport patch) to simulate upstreams and socket faults, assert what your client sent, and even route through a mock proxy.

📖 Read the full documentation →

Installation

pip install asyncly

Optional extras — msgspec, pydantic, orjson, prometheus, opentelemetry:

pip install "asyncly[pydantic]"

Quickstart

Define a client by subclassing BaseHttpClient and mapping status codes to handlers:

from http import HTTPStatus
from types import MappingProxyType

from aiohttp import ClientSession, hdrs
from pydantic import BaseModel

from asyncly import BaseHttpClient, DEFAULT_TIMEOUT, ResponseHandlersType
from asyncly.client.handlers.pydantic import parse_model
from asyncly.client.timeout import TimeoutType


class CatFact(BaseModel):
    fact: str
    length: int


class CatfactClient(BaseHttpClient):
    FACT_HANDLERS: ResponseHandlersType = MappingProxyType(
        {HTTPStatus.OK: parse_model(CatFact)}
    )

    async def fetch_fact(self, timeout: TimeoutType = DEFAULT_TIMEOUT) -> CatFact:
        return await self._make_req(
            method=hdrs.METH_GET,
            url=self._url / "fact",
            handlers=self.FACT_HANDLERS,
            timeout=timeout,
        )

Test it against a real mock server — no network, no monkeypatching:

from asyncly.srvmocker import JsonResponse, MockRoute, start_service


async def test_fetch_fact() -> None:
    routes = [MockRoute("GET", "/fact", "fact")]
    async with start_service(routes) as service:
        service.register("fact", JsonResponse({"fact": "Cats sleep a lot.", "length": 17}))

        async with ClientSession() as session:
            client = CatfactClient(url=service.url, session=session, client_name="catfact")
            fact = await client.fetch_fact()

        assert fact.fact == "Cats sleep a lot."
        service.assert_called("fact", times=1)

Prefer fixtures over boilerplate? asyncly ships a pytest plugin with mock_service and mock_proxy. See the Quickstart for more.

Why asyncly?

Unlike transport-patching mocks (aioresponses, respx), srvmocker runs a real aiohttp.TestServer inside your test loop — catching real sockets, timeouts, header auto-injection, and serialization quirks. See Testing strategies for the full comparison.

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asyncly-0.9.0.tar.gz (31.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

asyncly-0.9.0-py3-none-any.whl (47.9 kB view details)

Uploaded Python 3

File details

Details for the file asyncly-0.9.0.tar.gz.

File metadata

  • Download URL: asyncly-0.9.0.tar.gz
  • Upload date:
  • Size: 31.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.31 {"installer":{"name":"uv","version":"0.11.31","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for asyncly-0.9.0.tar.gz
Algorithm Hash digest
SHA256 4b917a58db46056367b44627ae6cf508f29023693071e67a4e0f64f1affe67d9
MD5 9e28d98761bd4f2ff9ddd8606697afa6
BLAKE2b-256 4e61481bba2a065754794ce382736d3416de8601113e9bd114745a4111c19504

See more details on using hashes here.

File details

Details for the file asyncly-0.9.0-py3-none-any.whl.

File metadata

  • Download URL: asyncly-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 47.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.31 {"installer":{"name":"uv","version":"0.11.31","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for asyncly-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 517b9aa6d4dc5cff6a23cad27f6bda3202eefad403e256ba13db3815e664b7b4
MD5 9176442936014e88daefdc51dded663d
BLAKE2b-256 9d76d2b63fd6ed13b2084544ecdeadd3eeb9ef009d05706b3d144769b2c342dd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page