bareasgi
class Application(CoreApplication) ¶
Summary¶
Construct the application
Description¶
```python 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: HttpMiddlewares | NoneOptional middleware callbacks. Defaults to None.
Optional router to for http routes. Defaults to None.
Optional middleware callbacks. Defaults to None.
Optional router for web routes. Defaults to None.
Optional handlers to run at startup. Defaults to None.
Optional handlers to run at shutdown. Defaults to None.
Optional not found (404) response. Defaults to DEFAULT_NOT_FOUND_RESPONSE.
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.
Shared data from the application.
Private data for this request or chain of requests.
Matches made by the router.
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]A function to
decode the body to json. Defaults to json.loads.
class HttpResponse ¶
Summary¶
The HTTP response.
Parameters¶
status: intThe status code.
The headers if any. Mandatory headers will be added if missing. Defaults to None.
The body, if any. Defaults to None.
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 function to convert the
data to a JSON string. Defaults to json.dumps.
An
optional function to convert the data to JSON bytes. If
specified this will be preferred to the encode argument.
Defaults to None.
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'.
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.
The shared information from the Application.
The private information for this request or chain of requests.
Any path matches from the router.
The 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]An optional subprotocol sent by the client. Defaults to None.
Optional headers to send. Defaults to None.
async method WebSocket.close ¶
Summary¶
Close the WebSocket.
Parameters¶
code: int (optional)The reason code. Defaults to 1000.
The reason. Can be any string. Defaults to 1000.
async method WebSocket.receive ¶
Summary¶
Receive data from the WebSocket.
class LifespanRequest ¶
Summary¶
Initialise a class holding a lifespan request.
Parameters¶
scope: LifespanScopeThe ASGI lifespan scope.
The global info dictionary.