asgikit


Nameasgikit JSON
Version 0.8.0 PyPI version JSON
download
home_pagehttps://github.com/livioribeiro/asgikit
SummaryToolkit for building ASGI applications and libraries
upload_time2024-04-17 20:43:28
maintainerNone
docs_urlNone
authorLivio Ribeiro
requires_python<4.0,>=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, 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.

## Example request and response

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

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


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/livioribeiro/asgikit",
    "name": "asgikit",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.11",
    "maintainer_email": null,
    "keywords": "asgi, toolkit, asyncio, web",
    "author": "Livio Ribeiro",
    "author_email": "livioribeiro@outlook.com",
    "download_url": "https://files.pythonhosted.org/packages/34/99/66cd20353a098fb01cde54a37d928b95500a7b2a22c3a48d68880e45bd53/asgikit-0.8.0.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, stream)\n  - Form\n- Response\n  - Plain text\n  - Json\n  - Streaming\n  - File\n- Websockets\n\n## Request and Response\n\nAsgikit `Request` and `Response` were designed to be have minimal interfaces,\nso they only provide very few methods to read from the request and write to the response.\nIn particular, the `Response` works differently from most tools, in which you do not\nreturn a response, but you write data into it.\n\nIt is provided several functions to interact with the request and the response, for instance,\nto read form data from the request and write json to the response.\n\nThis strategy allows for simpler extensibility. For example, to parse json from the request\nusing an alternative json parser, you just need to write a function that reads the request.\nSimilarly, to write another data format into the response, you just write a function that\nwrites to the response.\n\n## Example request and response\n\n```python\nfrom asgikit.requests import Request, read_json\nfrom asgikit.responses import respond_json\n\n\nasync def main(scope, receive, send):\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_json = await read_json(request)\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_json,\n  }\n\n  # send json response\n  await respond_json(request.response, data)\n```\n\n## Example websocket\n\n```python\nfrom asgikit.requests import Request\nfrom asgikit.errors.websocket import WebSocketDisconnectError\n\nasync def app(scope, receive, send):\n    request = Request(scope, receive, send)\n    ws = request.websocket\n    await ws.accept()\n\n    while True:\n        try:\n            message = await ws.receive()\n            await ws.send(message)\n        except WebSocketDisconnectError:\n            print(\"Client disconnect\")\n            break\n```\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Toolkit for building ASGI applications and libraries",
    "version": "0.8.0",
    "project_urls": {
        "Homepage": "https://github.com/livioribeiro/asgikit",
        "Repository": "https://github.com/livioribeiro/asgikit"
    },
    "split_keywords": [
        "asgi",
        " toolkit",
        " asyncio",
        " web"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "50f69775097007cbac32105a4459fd9d3e1b44898875d0dc703946b5f37fa16d",
                "md5": "e2eb7a767db763eed65d7fb8c4643435",
                "sha256": "8124033952b7495c6c76be375768a8931263bf24aedd82d208aab4319350ab7f"
            },
            "downloads": -1,
            "filename": "asgikit-0.8.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e2eb7a767db763eed65d7fb8c4643435",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.11",
            "size": 14383,
            "upload_time": "2024-04-17T20:43:26",
            "upload_time_iso_8601": "2024-04-17T20:43:26.844063Z",
            "url": "https://files.pythonhosted.org/packages/50/f6/9775097007cbac32105a4459fd9d3e1b44898875d0dc703946b5f37fa16d/asgikit-0.8.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "349966cd20353a098fb01cde54a37d928b95500a7b2a22c3a48d68880e45bd53",
                "md5": "6cbdd6211a82d350419e112179d5fed5",
                "sha256": "0a1982e48b045d084938de0f035c75b261e769c39c8ee50eb21bff4f5d948d74"
            },
            "downloads": -1,
            "filename": "asgikit-0.8.0.tar.gz",
            "has_sig": false,
            "md5_digest": "6cbdd6211a82d350419e112179d5fed5",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.11",
            "size": 11264,
            "upload_time": "2024-04-17T20:43:28",
            "upload_time_iso_8601": "2024-04-17T20:43:28.812445Z",
            "url": "https://files.pythonhosted.org/packages/34/99/66cd20353a098fb01cde54a37d928b95500a7b2a22c3a48d68880e45bd53/asgikit-0.8.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-17 20:43:28",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "livioribeiro",
    "github_project": "asgikit",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "asgikit"
}
        
Elapsed time: 0.25698s