Metadata-Version: 2.1
Name: starlette_abstract
Version: 0.0.1
Summary: An abstract middleware for Starlette
Author-email: Sebastian Balna <sebastianbalna@gmail.com>
License: Copyright (c) 2022 Sebastian Balna
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/zaxis23148/starlette_abstract
Project-URL: Bug Tracker, https://github.com/zaxis23148/starlette_abstract/issues
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.7
Description-Content-Type: text/markdown
License-File: LICENSE

# 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
```bash
pip install starlette_abstract
```

### Simple Example
```python
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
```
