An abstract middleware for Starlette
Project description
Abstract Middleware for Starlette
AbstractHTTPMiddleware
is replacing the original Starlette's BaseHTTPMiddleware
.
In the original BaseHTTPMiddleware
a RuntimeError
with message No response returned
is raised when two or more middlewares inheriting from the BaseHTTPMiddleware
were used in the middleware stack following each other.
The new AbstractHTTPMiddleware
is simply handling a situation when client closes connection early, during the original request is processing in endpoint. Normally an anyio.EndOfStream
would be raised, causing a RuntimeError
exception with message No response returned
. This situation is fully logged and handled by returning a new response with status_code=499
and content='Client closed connection'
.
Instalation
pip install starlette_abstract
Simple Example
from starlette_abstract.middleware import AbstractHTTPMiddleware, RequestResponseEndpoint
from starlette.requests import Request
from starlette.responses import Response
class MyCustomMiddleware(AbstractHTTPMiddleware):
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
# do something with request
response = await call_next(request)
# do something with response
return response
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 Distribution
Built Distribution
Hashes for starlette_abstract-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00e36c09625b0f78a59f1cc3608ac4490150c5fbd76a504cd8db5e54a2f49e45 |
|
MD5 | 677c58a5cbc2d178c6938cca6bb3a674 |
|
BLAKE2b-256 | 996b8686bd98c209a07455b23b9d2d5b2cb8cbaa0f6966cc56683f3f8f0089f6 |