Simple HTTP client and server for your integrations based on aiohttp
Project description
Simple HTTP client and server for your integrations based on aiohttp.
Installation
Installation is possible in standard ways, such as PyPI or installation from a git repository directly.
Installing from PyPI:
pip install asyncly
Installing from github.com:
pip install git+https://github.com/andy-takker/asyncly
The package contains several extras and you can install additional dependencies if you specify them in this way.
For example, with msgspec:
pip install "asyncly[msgspec]"
Complete table of extras below:
example |
description |
|---|---|
pip install "asyncly[msgspec]" |
For using msgspec structs |
pip install "asyncly[orjson]" |
For fast parsing json by orjson |
pip install "asyncly[pydantic]" |
For using pydantic models |
pip install "asyncly[prometheus]" |
To collect Prometheus metrics |
pip install "asyncly[opentelemetry]" |
To collect OpenTelemetry metrics |
Quick start guide
HttpClient
Simple HTTP Client for https://catfact.ninja. See full example in examples/catfact_client.py
from asyncly import DEFAULT_TIMEOUT, BaseHttpClient, ResponseHandlersType
from asyncly.client.handlers.pydantic import parse_model
from asyncly.client.timeout import TimeoutType
class CatfactClient(BaseHttpClient):
RANDOM_CATFACT_HANDLERS: ResponseHandlersType = MappingProxyType(
{
HTTPStatus.OK: parse_model(CatfactSchema),
}
)
async def fetch_random_cat_fact(
self,
timeout: TimeoutType = DEFAULT_TIMEOUT,
) -> CatfactSchema:
return await self._make_req(
method=hdrs.METH_GET,
url=self._url / "fact",
handlers=self.RANDOM_CATFACT_HANDLERS,
timeout=timeout,
)
Test Async Server for client
Example
For the HTTP client, we create a server to which he will go and simulate real responses. You can dynamically change the responses from the server in a specific test.
Let’s prepare the fixtures:
@pytest.fixture
async def catafact_service() -> AsyncIterator[MockService]:
routes = [
MockRoute("GET", "/fact", "random_catfact"),
]
async with start_service(routes) as service:
service.register(
"random_catfact",
JsonResponse({"fact": "test", "length": 4}),
)
yield service
@pytest.fixture
def catfact_url(catafact_service: MockService) -> URL:
return catafact_service.url
@pytest.fixture
async def catfact_client(catfact_url: URL) -> AsyncIterator[CatfactClient]:
async with ClientSession() as session:
client = CatfactClient(
client_name="catfact",
session=session,
url=catfact_url,
)
yield client
Now we can use them in tests. See full example in examples/test_catfact_client.py
async def test_fetch_random_catfact(catfact_client: CatfactClient) -> None:
# use default registered handler
fact = await catfact_client.fetch_random_cat_fact()
assert fact == CatfactSchema(fact="test", length=4)
async def test_fetch_random_catfact_timeout(
catfact_client: CatfactClient,
catafact_service: MockService,
) -> None:
# change default registered handler to time error handler
catafact_service.register(
"random_catfact",
LatencyResponse(
wrapped=JsonResponse({"fact": "test", "length": 4}),
latency=1.5,
),
)
with pytest.raises(asyncio.TimeoutError):
await catfact_client.fetch_random_cat_fact(timeout=1)
Useful responses and serializers
JsonResponse: simple JSON response from any object. You can setup status code and serializer for it. Using JsonSerializer
MsgpackResponse: response in msgpack format with It’s like JSON. But fast and small. Using MsgpackSerializer.
SequenceResponse: useful response if you want return different responses on next request. Accepts BaseMockResponse’s input.
TimeoutResponse: response with latency. For slow testing
TomlResponse: return TOML format text response. Using TomlSerializer.
YamlResponse: return YAML format text response. Using YamlSerializer.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file asyncly-0.5.1.tar.gz.
File metadata
- Download URL: asyncly-0.5.1.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.8.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f5dadbbe1e37b8b8c13c84ecacced88a24ff25a616bd1d9cfce8becf3c13f559
|
|
| MD5 |
80e3c7ff773c0f6516ae89f5f0e7d716
|
|
| BLAKE2b-256 |
cbae19844febf3d07308238d8e452031c229943d9eb456fb4596dd84e95cd150
|
File details
Details for the file asyncly-0.5.1-py3-none-any.whl.
File metadata
- Download URL: asyncly-0.5.1-py3-none-any.whl
- Upload date:
- Size: 21.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.8.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
071495d11a15ed4ddc9bc4892db1066026dc42d8b8e0a3e63f224ce2ea4999db
|
|
| MD5 |
26ea79b53aba11e034509785c005a8ec
|
|
| BLAKE2b-256 |
acdad3874278d1a8f5c10c1e86e83a17694580601c216cc71b4e4505e5afdbfd
|