AIOHTTP Connector for running ASGI applications
Project description
aiohttp-asgi
AIOHTTP Connector for running ASGI applications.
This library intends to increase the parity between AIOHTTP and HTTPX, specifically with HTTPX's AsyncClient
. It is primarily intended to be used in test suite scenarios, or other situations where one would want to interface with an ASGI application directly instead of through a web server.
Supports Python 3.8+ and AIOHTTP 3+.
Installation
$ pdm add aiohttp-asgi-connector
# or
$ python -m pip install --user aiohttp-asgi-connector
Usage
This library replaces the entire connection stack and pool underlying HTTP transport. AIOHTTP exposes custom connectors via the connector
argument supplied when creating a ClientSession
instance.
To use the ASGIApplicationConnector
:
import asyncio
from typing import Annotated # or from typing_extensions
from aiohttp_asgi_connector import ASGIApplicationConnector
from aiohttp import ClientSession
from fastapi import FastAPI, Body
app = FastAPI()
@app.post("/ping")
async def pong(message: Annotated[str, Body(embed=True)]):
return {"broadcast": f"Application says '{message}'!"}
async def main():
connector = ASGIApplicationConnector(app)
async with ClientSession(base_url="http://localhost", connector=connector) as session:
async with session.post("/ping", json={"message": "hello"}) as resp:
print(await resp.json())
# ==> {'broadcast': "Application says 'hello'!"}
asyncio.run(main())
This library does not handle ASGI lifespan events. If you want to run those events, use this library in conjunction with something like asgi-lifespan.
License
This software is licensed under the BSD 3-Clause License.
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file aiohttp_asgi_connector-1.0.0.tar.gz
.
File metadata
- Download URL: aiohttp_asgi_connector-1.0.0.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.16.1 CPython/3.12.4 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e0155baaa933c209196a176b38755ef05ce633f7bdeb4e4c4ebd79af1516798e |
|
MD5 | 53468d2974faa36271616c7702c67ea5 |
|
BLAKE2b-256 | 0a3dd0609fff0181a39e4102c6ec1e8df544f11e4fb2d3a01fd160d460b0ac0f |
File details
Details for the file aiohttp_asgi_connector-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: aiohttp_asgi_connector-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.16.1 CPython/3.12.4 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 222264c8151a64806d5333722345b2e71c62b9d38f071fae7c485d33b1b8c45c |
|
MD5 | ec252aace2e209528282d3553c772942 |
|
BLAKE2b-256 | e74aa278fe656bbd3db25ae20d49c45f769db18f8dfaea44a9d26ed594a8a024 |