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.

This connector transmits the request to the ASGI application exactly as it is serialized by AIOHTTP. If upload chunking or compression are enabled for your ClientSession requests, your ASGI application will need to be able to handle de-chunking and de-compressing; FastAPI / Starlette do not do this by default. Support to enable connector-side dechunking and decompressing may come as a future feature if a need is demonstrated for it (file an Issue).

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.1.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

aiohttp_asgi_connector-1.1.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aiohttp_asgi_connector-1.1.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.20.1 CPython/3.11.2 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for aiohttp_asgi_connector-1.1.0.tar.gz
Algorithm Hash digest
SHA256 8f62ad017db78446f82bef2128ef962845fcc3edaeb683b8da38143e8dbe0350
MD5 5d823a6bda8dff9b8adaa37c029fbc46
BLAKE2b-256 a2b4813c60bf1cb13e9f4153a2ce95fc1364259c4b9132fb79af0d8d8261665a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aiohttp_asgi_connector-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.20.1 CPython/3.11.2 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for aiohttp_asgi_connector-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8955fc332912305f5abcc5a79aff0ac35d1b7e31aeb75f45635120d7eb9bfec4
MD5 3687627e78ced5acaa56436f4fc3c00e
BLAKE2b-256 8c792455e1d7b74dc72392a0e2e8036c923ef62ccbeef6efa025c340d6a2b391

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 Pingdom Monitoring Sentry Error logging StatusPage Status page