Skip to main content

Is a toolkit to build ASGI applications faster

Project description

asgi-tools – Is a toolkit to build ASGI applications faster

Features:

  • Supports Asyncio and Trio libraries

  • Request – Parse ASGI scope, get url, headers, cookies, read a request’s data/json/form-data

  • Response – Send HTTP (text, html, json, stream, file, http errors) responses

  • ResponseWebsocket – Work with websockets

  • RequestMiddleware – Parse a scope and insert the parsed request into the scope

  • ResponseMiddleware – Parse responses and convert them into ASGI messages

  • RouterMiddleware – Route HTTP requests

  • LifespanMiddleware – Process a lifespan cycle

  • StaticFilesMiddleware – Serve static files from URL prefixes

  • asgi_tools.tests.TestClient – A test client with websockets support to test asgi applications

  • App – A simple foundation for ASGI apps

Tests Status PYPI Version

Requirements

  • python >= 3.7

Installation

asgi-tools should be installed using pip:

pip install asgi-tools

Usage

asgi_tools.Request, asgi_tools.Response

Parse HTTP Request data from a scope and return it as JSON response:

import json
from asgi_tools import Request, Response

async def app(scope, receive, send):
    if scope['type'] != 'http':
        return

    # Parse scope
    request = Request(scope, receive, send)
    request_data = {

        # Get full URL
        "url": str(request.url),

        "charset": request.charset,

        # Get headers
        "headers": {**request.headers},

        # Get query params
        "query": dict(request.query),

        # Get cookies
        "cookies": dict(request.cookies),

    }

    # Create a response (ResponseHTML, ResponseText, ResponseJSON, ResponseStream, ResponseFile, ResponseRedirect also available)
    response = Response(json.dumps(request_data), content_type="application/json")

    # Send ASGI messages
    return await response(scope, receive, send)

Response/Request Middlewares

Automatically convert a scope into a asgi_tools.Request

from asgi_tools import RequestMiddleware, ResponseHTML

async def app(request, receive, send):
    # We will get a parsed request here
    data = await request.json()
    response = ResponseHTML(data['name'])
    return await response(request, receive, send)

app = RequestMiddleware(app)

Automatically parse an result from asgi apps and convert it into a asgi_tools.Response

from asgi_tools import ResponseMiddleware

async def app(request, receive, send):
    return "Hello World!"

app = ResponseMiddleware(app)

Router Middleware

Route HTTP requests

from http_router import Router
from asgi_tools import RouterMiddleware, RequestMiddleware, ResponseMiddleware

router = Router()

@router.route('/page1')
async def page1(request, receive, send):
    return 'page1'

@router.route('/page2')
async def page2(request, receive, send):
    return 'page2'

# TODO

Bug tracker

If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/asgi-tools/issues

Contributing

Development of the project happens at: https://github.com/klen/asgi-tools

License

Licensed under a MIT license.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

asgi_tools-0.16.3-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file asgi_tools-0.16.3-py3-none-any.whl.

File metadata

  • Download URL: asgi_tools-0.16.3-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for asgi_tools-0.16.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3b96868da93cd820c4dd302e8c1a48e0dc7a1d52e9ab75db6a470e17417a5478
MD5 48c2703b25fcb57e12d7e330953e2712
BLAKE2b-256 e8c3f8f80140804e792a7d3c0ee0fca21e4098ca2f6f4c0ce65e2f004b4e70ab

See more details on using hashes here.

Supported by

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