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.
async method HttpRequest.content ¶
Summary¶
Return the contents of the request body as bytes.
Description¶
This function consumes the body. Calling it a second time will generate an error.
async method HttpRequest.json ¶
Summary¶
Return the contents of the request body as JSON.
Description¶
This function consumes the body. Calling it a second time will generate an error.
Parameters¶
decode: Callable[[Union[str, bytes]], Any] (optional)A function to
decode the body to json. Defaults to json.loads
.
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 method HttpResponse.from_bytes ¶
Summary¶
Create an HTTP response from bytes content.
Parameters¶
status: int (optional)An optional status. Defaults to 200
.
An optional content type. Defaults
to b'text/plain'
.
Optional headers.
Defaults to None
.
The size of each chunk to send or -1 to send as a single chunk. Defaults to -1.
class method HttpResponse.from_json ¶
Summary¶
Create an HTTP response from data converted to JSON.
Parameters¶
status: int (optional)An optional status code. Defaults to 200
.
An optional content type. Defaults
to b'application/json'
.
Optional headers.
Defaults to None
.
An optional function to
convert the data to a JSON string. Defaults to json.dumps
.
class method HttpResponse.from_text ¶
Summary¶
Create an HTTP response from a text string.
Parameters¶
status: int (optional)An optional status. Defaults to 200
.
An optional content type. Defaults
to b'text/plain'
.
Optional headers.
Defaults to None
.
The encoding to apply when transforming the text into bytes. Defaults to 'utf-8'.
chunk_size: int (optional)The size of each chunk to send or -1 to send as a single chunk. Defaults to -1.
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.