Tools to make ASGI Applications
Project description
asgi-tools – Tools to make ASGI Applications
Features:
Request – Parse ASGI scope, get url, headers, cookies, read a request’s data/json/form-data
Response – Send HTTP (html, json) responses
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
AppMiddleware – A combined (request, response, router, lifespan) middleware to quikly create ASGI apps
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 build a http response:
from asgi_tools import Request, Response
template = "... any template for the HTML content here ..."
async def app(scope, receive, send):
if scope['type'] != 'http':
return
# Parse the given scope
request = Request(scope, receive, send)
# Render the page
body = template.render(
# Get full URL
url=request.url,
charset=request.charset,
# Get headers
headers=request.headers,
# Get query params
query=request.query,
# Get cookies
cookies=request.cookies,
# Get a decoded request body (the methods: request.body, request.form, request.json also available)
text=await request.text(),
)
# Render a response as HTML (HTMLResponse, PlainTextResponse, JSONResponse, StreamResponse, RedirectResponse also available)
return await Response(body, content_type="text/html")(scope, receive, send)
Response/Request Middlewares
Automatically convert a scope into a asgi_tools.Request
from asgi_tools import RequestMiddleware
async def base_app(request, receive, send):
assert request.url
assert request.headers
# ...
app = RequestMiddleware(base_app)
Automatically parse an result from asgi apps and convert it into a asgi_tools.Response
from asgi_tools import ResponseMiddleware
async def base_app(request, receive, send):
return "Hello World!"
app = ResponseMiddleware(base_app)
Router Middleware
Route HTTP requests
from asgi_tools import RouterMiddleware, ResponseMiddleware
async def index_and_default(*args):
return "Hello from Index"
async def page1(*args):
return "Hello from Page1"
async def page2(*args):
return "Hello from Page2"
app = ResponseMiddleware(RouterMiddleware(index_and_default, routes={'/page1': page1, '/page2': page2}))
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
Built Distribution
File details
Details for the file asgi_tools-0.0.18-py3-none-any.whl
.
File metadata
- Download URL: asgi_tools-0.0.18-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb646e84ea97f2eff2dea5d6025f70f18b88fcb96dcf8e4f61096e4d39a1f6c7 |
|
MD5 | 623e575b82f171b69882f6ec14af605f |
|
BLAKE2b-256 | 2448cd8778d21fb6dda82473ad665ad0f3a08e4493b7ec5e1d7f6911a3f2df4b |