Skip to main content

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.

Metadata

Release files for python-middlewareable 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-middlewareable 1.0.0
File Size Uploaded
python_middlewareable-1.0.0.tar.gz 7.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-middlewareable 1.0.0
File Interpreter ABI Platform
python_middlewareable-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.7 kB

Release files / python_middlewareable-1.0.0.tar.gz

Download URL python_middlewareable-1.0.0.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
58709091fdb9c8f27624f2e09dc6317d9603b9c513710c30623ca74227260b4d
BLAKE2b-256 checksum
How to use checksums
bbc2f37d47f16c009ed5e702ec8dff2baa391758fe36a4e4ba9f917e3f57c050
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Release files / python_middlewareable-1.0.0-py3-none-any.whl

Download URL python_middlewareable-1.0.0-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
44e860b4acef6ba95064776b83cd97e09f5f426bd18de74b0ea64af9f1940fec
BLAKE2b-256 checksum
How to use checksums
64acefc47208b02736aa74e5abe34f2e0d6dc834fa14a0fbc3ded42c710b9b14
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page