Skip to content

bareasgi

class Application

Summary

Construct the application

Description

from bareasgi import (
    Application,
    Scope,
    Info,
    RouteMatches,
    Content,
    WebSocket,
    text_reader,
    text_writer
)

async def http_request_callback(
    scope: Scope,
    info: Info,
    matches: RouteMatches,
    content: Content
) -> HttpResponse:
    text = await text_reader(content)
    return 200, [(b'content-type', b'text/plain')], text_writer('This is not a test'), None

import uvicorn

app = Application()
app.http_router.add({'GET', 'POST', 'PUT', 'DELETE'}, '/{path}', http_request_callback)

uvicorn.run(app, port=9009)
bareasgi.Application(
*,
middlewares: Optional[List[HttpMiddlewareCallback]],
http_router: Optional[HttpRouter],
web_socket_router: Optional[WebSocketRouter],
startup_handlers: Optional[List[LifespanHandler]],
shutdown_handlers: Optional[List[LifespanHandler]],
not_found_response: Optional[HttpResponse],
info: Optional[MutableMapping[str, Any]]
) -> None

Parameters

middlewares: Optional[List[HttpMiddlewareCallback]] (optional)

Optional middleware callbacks. Defaults to None.

http_router: Optional[HttpRouter] (optional)

Optional router to for http routes. Defaults to None.

web_socket_router: Optional[WebSocketRouter] (optional)

Optional router for web routes. Defaults to None.

startup_handlers: Optional[List[LifespanHandler]] (optional)

Optional handlers to run at startup. Defaults to None.

shutdown_handlers: Optional[List[LifespanHandler]] (optional)

Optional handlers to run at shutdown. Defaults to None.

not_found_response: Optional[HttpResponse] (optional)

Optional not found (404) response. Defaults to None.

info: Optional[MutableMapping[str, Any]] (optional)

Optional dictionary for user data. Defaults to None.

property Application.http_router

Summary

Router for http routes

http_router -> HttpRouter

property Application.info

Summary

A place to sto application specific data.

info -> MutableMapping[str, Any]

property Application.middlewares

Summary

The middlewares.

middlewares -> List[HttpMiddlewareCallback]

property Application.shutdown_handlers

Summary

Handlers run on shutdown

shutdown_handlers -> List[LifespanHandler]

property Application.startup_handlers

Summary

Handlers run at startup

startup_handlers -> List[LifespanHandler]

property Application.ws_router

Summary

Router for WebSocket routes

ws_router -> WebSocketRouter

method Application.on_http_request

Summary

A decorator to add an http route handler to the application

Application.on_http_request(
methods: AbstractSet[str],
path: str
) -> Callable[[HttpRequestCallback], HttpRequestCallback]

Parameters

methods: AbstractSet[str]

The http methods, e.g. {{'POST', 'PUT'}

path: str

The path

Returns

Callable[[HttpRequestCallback], HttpRequestCallback]: The decorated request.

method Application.on_shutdown

Summary

A decorator to add a startup handler to the application

Application.on_shutdown(
callback: LifespanHandler
) -> LifespanHandler

Parameters

callback: LifespanHandler

The shutdown handler.

Returns

LifespanHandler: The decorated handler.

method Application.on_startup

Summary

A decorator to add a startup handler to the application

Application.on_startup(
callback: LifespanHandler
) -> LifespanHandler

Parameters

callback: LifespanHandler

The startup handler.

Returns

LifespanHandler: The decorated handler.

method Application.on_ws_request

Summary

A decorator to add a websocket route handler to the application

Application.on_ws_request(
path: str
) -> Callable[[WebSocketRequestCallback], WebSocketRequestCallback]

Parameters

path: str

The path

Returns

Callable[[WebSocketRequestCallback], WebSocketRequestCallback]: The decorated handler