bareasgi
class Application(CoreApplication) ¶
Summary¶
Construct the application
Description¶
from bareasgi import (
Application,
Scope,
HttpRequest,
HttpResponse,
text_reader,
text_writer
)
async def http_request_callback(request: HttpRequest) -> HttpResponse:
text = await text_reader(request.body)
return HttpResponse(
200,
[(b'content-type', b'text/plain')],
text_writer('This is not a test')
)
import uvicorn
app = Application()
app.http_router.add({'GET', 'POST', 'PUT', 'DELETE'}, '/{path}', http_request_callback)
uvicorn.run(app, port=9009)
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 DEFAULT_NOT_FOUND_RESPONSE.
info: Optional[Dict[str, Any]] (optional)Optional dictionary for user data. Defaults to None.
method Application.on_http_request ¶
Summary¶
A decorator to add an http route handler to the application
method Application.on_shutdown ¶
Summary¶
A decorator to add a startup handler to the application
method Application.on_startup ¶
Summary¶
A decorator to add a startup handler to the application
class HttpRequest ¶
Summary¶
An HTTP request.
Parameters¶
scope: HTTPScopeThe ASGI http scope.
info: Dict[str, Any]Shared data from the application.
context: Dict[str, Any]Private data for this request or chain of requests.
matches: Mapping[str, Any]Matches made by the router.
body: AsyncIterable[bytes]The body.
class HttpResponse ¶
Summary¶
The HTTP response.
Parameters¶
status: intThe status code.
headers: Optional[List[Tuple[bytes, bytes]]] (optional)The headers if any. Mandatory headers will be added if missing. Defaults to None.
body: Optional[AsyncIterable[bytes]] (optional)The body, if any. Defaults to None.
pushes: Optional[Iterable[PushResponse]] (optional)Server pushes, if any. Defaults to None.
class WebSocketRequest ¶
Summary¶
A WebSocket request.
Parameters¶
scope: WebSocketScopeThe ASGI WebSocket scope.
info: Dict[str, Any]The shared information from the Application.
context: Dict[str, Any]The private information for this request or chain of requests.
matches: Mapping[str, Any]Any path matches from the router.
web_socket: WebSocketThe WebSocket.
class WebSocket ¶
Summary¶
The interface for a server side WebSocket.
async method WebSocket.accept ¶
Summary¶
Accept the socket.
Description¶
This must be done before any other action is taken.
Parameters¶
subprotocol: Optional[str] (optional)An optional subprotocol sent by the client. Defaults to None.
headers: Optional[List[Tuple[bytes, bytes]]] (optional)Optional headers to send. Defaults to None.
async method WebSocket.close ¶
Summary¶
Close the WebSocket.