Skip to main content

Toolkit for building ASGI applications and libraries

Project description

Asgikit - ASGI Toolkit

Asgikit is a toolkit for building asgi applications and frameworks.

It is intended to be a minimal library and provide the building blocks for other libraries.

The examples directory contain usage examples of several use cases

Features:

  • Request
    • Headers
    • Cookies
    • Body (bytes, str, json, form, stream)
    • Form
  • Response
    • Plain text
    • Json
    • Streaming
    • File
  • Websockets

Requests and Responses

Asgikit Request, like other libraries, have methods to read items from the incoming request. However, unlike other libraries, there is no response object. Instead, you use the methods in Request to respond to the request like respond_json and respond_stream. There is a response property in the Request object where you can set response attributes like status, headers and cookies.

The main methods to interact with the Request are the following:

class Request:
    # Read the request body as a byte stream
    async def read_stream(self) -> AsyncIterable[bytes]: ...
    # Read the request body as bytes
    async def read_bytes(self) -> bytes: ...
    # Read the request body as str
    async def read_text(self, encoding: str = None) -> str: ...
    # Read the request body and parse it as json
    async def read_json(self) -> Any: ...
    # Read the request body and parse it as form
    async def read_form(self) -> dict[str, str | list[str]]: ...

    # Respond with bytes
    async def respond_bytes(self, content: bytes): ...
    # Respond with str
    async def respond_text(self, content: str): ...
    # Respond with the given content encoded as json
    async def respond_json(self, content: Any): ...
    # Respond with empty response and given status
    async def respond_status(self, status: HTTPStatus): ...
    # Respond with redirect
    async def respond_redirect(self, location: str, permanent: bool = False): ...
    # Respond with a post/redirect/get
    # https://en.wikipedia.org/wiki/Post/Redirect/Get
    async def respond_redirect_post_get(self, location: str): ...
    # Context manager that provides a function to write to the response
    async def response_writer(self): ...
    # Respond with file
    async def respond_file(self, path: str | os.PathLike): ...

Example request and response

from asgikit.requests import Request


async def main(scope, receive, send):
    assert scope["type"] == "http"

    request = Request(scope, receive, send)

    # request method
    method = request.method

    # request path
    path = request.path

    # request headers
    headers = request.headers

    # read body as json
    body = await request.read_json()

    data = {
        "lang": "Python",
        "async": True,
        "platform": "asgi",
        "method": method,
        "path": path,
        "headers": dict(headers.items()),
        "body": body,
    }

    # send json response
    await request.respond_json(data)

Example websocket

from asgikit.requests import Request
from asgikit.websockets import WebSocketDisconnect


async def app(scope, receive, send):
    assert scope["type"] == "websocket"

    request = Request(scope, receive, send)
    ws = await request.websocket_accept()

    while True:
        try:
            message = await ws.read()
            await ws.write(message)
        except WebSocketDisconnect:
            print("Client disconnect")
            break

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

asgikit-0.13.3.tar.gz (19.8 kB view details)

Uploaded Source

Built Distribution

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

asgikit-0.13.3-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file asgikit-0.13.3.tar.gz.

File metadata

  • Download URL: asgikit-0.13.3.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.26.1 CPython/3.14.0 Linux/6.14.0-35-generic

File hashes

Hashes for asgikit-0.13.3.tar.gz
Algorithm Hash digest
SHA256 9c14be383647736d32b72ce3824a5daaaf6ba90dc0b7fd395c0b668c45e548e2
MD5 64c9e0d199a842fec497ee09fe018c8f
BLAKE2b-256 f86b389a46665100c9461d96e2d4d1474d03a6ccfd1fb8ad0b5b1d8a0921c927

See more details on using hashes here.

File details

Details for the file asgikit-0.13.3-py3-none-any.whl.

File metadata

  • Download URL: asgikit-0.13.3-py3-none-any.whl
  • Upload date:
  • Size: 17.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.26.1 CPython/3.14.0 Linux/6.14.0-35-generic

File hashes

Hashes for asgikit-0.13.3-py3-none-any.whl
Algorithm Hash digest
SHA256 72d1a9a4bdf00662861a3a9af5641f8270c0d792bcce6e9c4941cf58773daf8a
MD5 e31eebcc673616fa81d9c76b006471e7
BLAKE2b-256 4cdd1bb57467852e5a909bc62345bfe88b89501a0137f83a48818f2e3056f83c

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