asgikit


Nameasgikit JSON
Version 0.13.1 PyPI version JSON
download
home_pageNone
SummaryToolkit for building ASGI applications and libraries
upload_time2025-07-09 20:00:10
maintainerNone
docs_urlNone
authorLivio Ribeiro
requires_python>=3.11
licenseMIT
keywords asgi toolkit asyncio web
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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](./examples) 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:

```python
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

```python
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

```python
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
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "asgikit",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": null,
    "keywords": "asgi, toolkit, asyncio, web",
    "author": "Livio Ribeiro",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/4f/82/95388556fd2f3d024654149e8db548699cace6e90e9535bb0f80919c76ec/asgikit-0.13.1.tar.gz",
    "platform": null,
    "description": "# Asgikit - ASGI Toolkit\n\nAsgikit is a toolkit for building asgi applications and frameworks.\n\nIt is intended to be a minimal library and provide the building blocks for other libraries.\n\nThe [examples directory](./examples) contain usage examples of several use cases\n\n## Features:\n\n- Request\n  - Headers\n  - Cookies\n  - Body (bytes, str, json, form, stream)\n  - Form\n- Response\n  - Plain text\n  - Json\n  - Streaming\n  - File\n- Websockets\n\n## Requests and Responses\n\nAsgikit `Request`, like other libraries, have methods to read items from the incoming\nrequest. However, unlike other libraries, there is no response object. Instead, you\nuse the methods in `Request` to respond to the request like `respond_json` and `respond_stream`.\nThere is a `response` property in the `Request` object where you can set response\nattributes like status, headers and cookies.\n\nThe main methods to interact with the `Request` are the following:\n\n```python\nclass Request:\n    # Read the request body as a byte stream\n    async def read_stream(self) -> AsyncIterable[bytes]: ...\n    # Read the request body as bytes\n    async def read_bytes(self) -> bytes: ...\n    # Read the request body as str\n    async def read_text(self, encoding: str = None) -> str: ...\n    # Read the request body and parse it as json\n    async def read_json(self) -> Any: ...\n    # Read the request body and parse it as form\n    async def read_form(self) -> dict[str, str | list[str]]: ...\n\n    # Respond with bytes\n    async def respond_bytes(self, content: bytes): ...\n    # Respond with str\n    async def respond_text(self, content: str): ...\n    # Respond with the given content encoded as json\n    async def respond_json(self, content: Any): ...\n    # Respond with empty response and given status\n    async def respond_status(self, status: HTTPStatus): ...\n    # Respond with redirect\n    async def respond_redirect(self, location: str, permanent: bool = False): ...\n    # Respond with a post/redirect/get\n    # https://en.wikipedia.org/wiki/Post/Redirect/Get\n    async def respond_redirect_post_get(self, location: str): ...\n    # Context manager that provides a function to write to the response\n    async def response_writer(self): ...\n    # Respond with file\n    async def respond_file(self, path: str | os.PathLike): ...\n```\n\n## Example request and response\n\n```python\nfrom asgikit.requests import Request\n\n\nasync def main(scope, receive, send):\n    assert scope[\"type\"] == \"http\"\n\n    request = Request(scope, receive, send)\n\n    # request method\n    method = request.method\n\n    # request path\n    path = request.path\n\n    # request headers\n    headers = request.headers\n\n    # read body as json\n    body = await request.read_json()\n\n    data = {\n        \"lang\": \"Python\",\n        \"async\": True,\n        \"platform\": \"asgi\",\n        \"method\": method,\n        \"path\": path,\n        \"headers\": dict(headers.items()),\n        \"body\": body,\n    }\n\n    # send json response\n    await request.respond_json(data)\n```\n\n## Example websocket\n\n```python\nfrom asgikit.requests import Request\nfrom asgikit.websockets import WebSocketDisconnect\n\n\nasync def app(scope, receive, send):\n    assert scope[\"type\"] == \"websocket\"\n\n    request = Request(scope, receive, send)\n    ws = await request.websocket_accept()\n\n    while True:\n        try:\n            message = await ws.read()\n            await ws.write(message)\n        except WebSocketDisconnect:\n            print(\"Client disconnect\")\n            break\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Toolkit for building ASGI applications and libraries",
    "version": "0.13.1",
    "project_urls": {
        "source": "https://codeberg.org/livioribeiro/asgikit"
    },
    "split_keywords": [
        "asgi",
        " toolkit",
        " asyncio",
        " web"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bfbca239dbaf82234309a1281af7f75cc1ca840426c9d978ec372564f41f4986",
                "md5": "1aeb5ee64ae51b854625b7f9d228122d",
                "sha256": "3263bb2382583be55be6cb903800c691b479fb33c71d2c39c08681f2adfab9a8"
            },
            "downloads": -1,
            "filename": "asgikit-0.13.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1aeb5ee64ae51b854625b7f9d228122d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 17021,
            "upload_time": "2025-07-09T20:00:09",
            "upload_time_iso_8601": "2025-07-09T20:00:09.350166Z",
            "url": "https://files.pythonhosted.org/packages/bf/bc/a239dbaf82234309a1281af7f75cc1ca840426c9d978ec372564f41f4986/asgikit-0.13.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4f8295388556fd2f3d024654149e8db548699cace6e90e9535bb0f80919c76ec",
                "md5": "3e251de518ee9842060bd28090712b99",
                "sha256": "e75a67d2600ed1f3c5f3909dd17729e7f84e12b0bfab4f721868076d9ee225db"
            },
            "downloads": -1,
            "filename": "asgikit-0.13.1.tar.gz",
            "has_sig": false,
            "md5_digest": "3e251de518ee9842060bd28090712b99",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 20204,
            "upload_time": "2025-07-09T20:00:10",
            "upload_time_iso_8601": "2025-07-09T20:00:10.923255Z",
            "url": "https://files.pythonhosted.org/packages/4f/82/95388556fd2f3d024654149e8db548699cace6e90e9535bb0f80919c76ec/asgikit-0.13.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-09 20:00:10",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": true,
    "codeberg_user": "livioribeiro",
    "codeberg_project": "asgikit",
    "lcname": "asgikit"
}
        
Elapsed time: 1.63788s