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, stream)
    • Form
  • Response
    • Plain text
    • Json
    • Streaming
    • File
  • Websockets

Request and Response

Asgikit Request and Response were designed to be have minimal interfaces, so they only provide very few methods to read from the request and write to the response. In particular, the Response works differently from most tools, in which you do not return a response, but you write data into it.

It is provided several functions to interact with the request and the response, for instance, to read form data from the request and write json to the response.

This strategy allows for simpler extensibility. For example, to parse json from the request using an alternative json parser, you just need to write a function that reads the request. Similarly, to write another data format into the response, you just write a function that writes to the response.

Custom JSON encoder and decoder

By default, asgikit uses json.dumps and json.loads for dealing with JSON. If you want to use other libraries like orjson, just define the environment variable ASGIKIT_JSON_ENCODER of the module compatible with json, or the full path to the functions that perform encoding and decoding, in that order:

ASGIKIT_JSON_ENCODER=orjson
# or
ASGIKIT_JSON_ENCODER=msgspc.json.encode,msgspc.json.encode

Example request and response

from asgikit.requests import Request, read_json
from asgikit.responses import respond_json


async def main(scope, receive, send):
    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_json = await read_json(request)
  
    data = {
        "lang": "Python",
        "async": True,
        "platform": "asgi",
        "method": method,
        "path": path,
        "headers": dict(headers.items()),
        "body": body_json,
    }
  
    # send json response
    await respond_json(request.response, data)

Example websocket

from asgikit.requests import Request
from asgikit.errors.websocket import WebSocketDisconnectError


async def app(scope, receive, send):
    request = Request(scope, receive, send)
    ws = request.websocket
    await ws.accept()

    while True:
        try:
            message = await ws.receive()
            await ws.send(message)
        except WebSocketDisconnectError:
            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.12.2.tar.gz (20.1 kB view details)

Uploaded Source

Built Distribution

asgikit-0.12.2-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: asgikit-0.12.2.tar.gz
  • Upload date:
  • Size: 20.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.22.3 CPython/3.13.2 Linux/6.8.0-1021-azure

File hashes

Hashes for asgikit-0.12.2.tar.gz
Algorithm Hash digest
SHA256 5078e19e2989a5a4f356ba47f16958d5c2a1c50e597e76bc9bbffa3d5c03aa58
MD5 49b997d4ca0d87ddc3b7240dd24894f3
BLAKE2b-256 bfe7deb51cd83454308d1c288b3877d5972ecd0f84f4eb1ececb12f6d06de59d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: asgikit-0.12.2-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.22.3 CPython/3.13.2 Linux/6.8.0-1021-azure

File hashes

Hashes for asgikit-0.12.2-py3-none-any.whl
Algorithm Hash digest
SHA256 38edaa329fccb3381d7d81a784c8867dd52319d1a3d4a7af89a4c4ab3055ee95
MD5 9f5b8cc09a82322770785d6b44c9d9da
BLAKE2b-256 927f6e585946daeadda43c7a74b0093fa5fa3dd22f18e674bd5ee44c769f35fe

See more details on using hashes here.

Supported by

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