Skip to main content

AIOHTTP Connector for running ASGI applications

Project description

aiohttp-asgi-connector

GitHub Workflow Status PyPI - Downloads GitHub

An AIOHTTP ClientSession connector for interacting with 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 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())

Exceptions raised within the ASGI application that are not handled by middleware are propagated.

Since no HTTP packets are actually sent, request chunking and compression have no effect when used.

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:

from asgi_lifespan import LifespanManager

async with LifespanManager(app) as manager:
    connector = ASGIApplicationConnector(manager.app)
    async with ClientSession(base_url="http://localhost", connector=connector) as session:
        ...

License

This software is licensed under the BSD 3-Clause License.

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

aiohttp_asgi_connector-1.0.3.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

aiohttp_asgi_connector-1.0.3-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp_asgi_connector-1.0.3.tar.gz.

File metadata

  • Download URL: aiohttp_asgi_connector-1.0.3.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.17.3 CPython/3.12.4 Windows/10

File hashes

Hashes for aiohttp_asgi_connector-1.0.3.tar.gz
Algorithm Hash digest
SHA256 313e6c4d4d887757d213aac28379e2fa0020bb187a72e5382bd79f2c0ee4802e
MD5 91290ff3d9abd42244ff175fc6d68e44
BLAKE2b-256 e1488eddcbfd7c9c5bab3497d2a48a9b2703e4aca83d938942d190d1359dd6e3

See more details on using hashes here.

File details

Details for the file aiohttp_asgi_connector-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for aiohttp_asgi_connector-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1ddeda8e67cdb6e24f7b2b27ed14efc4fdccecf7e77924050a33d6a0ba6eedd2
MD5 0d838a8976323b3d03a8fb1c88cccdc3
BLAKE2b-256 bf0b2719872adcca627f994c10bc6e0a85ec4398b780b29742fd0d44305b1091

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page