Skip to main content

A simple library for working with middlewares in Python

Project description

Python-Middlewareable

A simple library for working with middlewares in Python.

Installation

pip install python-middlewareable

Usage

1. Define the payload structure

@dataclass
class Request(RequestBase): # inherit from RequestBase
    name: str

2. Create a middleware

class OneMiddleware(MiddlewareBase[Request]):
    async def handle(
        self, request: Request, next_call: MiddlewareNextCallBase[Request]
    ) -> None:
        request.name = request.name + " from OneMiddleware"

        print("OneMiddleware before")
        await next_call(request)
        print("OneMiddleware after")

3. Add the MiddlewareableBase trait to the class that will use the middlewares

class App(MiddlewareableBase[Request]):
    middlewares = [OneMiddleware] # add your middlewares here

4. Instantiate and use it

# middlewareable
app = App()

# process request
result = await app.process_middlewares(Request(name="Hello"))

# check the result
print(request)

# output:
# OneMiddleware before
# OneMiddleware after
# Hello from OneMiddleware

Traits

You can use the following traits to extend the functionality of your classes:

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

python_middlewareable-1.0.0.tar.gz (7.7 kB view hashes)

Uploaded Source

Built Distribution

python_middlewareable-1.0.0-py3-none-any.whl (7.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