Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

starlette_abstract-0.0.1.tar.gz (3.9 kB view hashes)

Uploaded Source

Built Distribution

starlette_abstract-0.0.1-py3-none-any.whl (4.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page