Skip to main content

Python module to add general middleware

Project description

# Middleware for Python

**Copyright (c) 2016 David Betz**

## Installation

pip install middleware

[![Build Status](https://travis-ci.org/davidbetz/middleware.svg?branch=master)](https://travis-ci.org/davidbetz/middleware)
[![PyPI version](https://badge.fury.io/py/middleware.svg)](https://badge.fury.io/py/middleware)

## Compatibility

Python 2 and 3

## Purpose

Most everyone needs a concept of middleware.

Following are examples of using this, see ```test_middleware.py``` for full examples.

class AdditionMiddleware1(Middleware):
def create(self):
def func(mwa, context):
try:
counter = context['counter']
except:
counter = 0
context['counter'] = counter + 1

return next(mwa)

return func

When using a class, add a ```create``` function which returns a function. This inner fuction accepts the middleware array and the data context and returns ```next(mwa)``` to create a middleware chain.

For this example, I'll add two more:

class AdditionMiddleware2(AdditionMiddleware1):
pass


class AdditionMiddleware3(AdditionMiddleware2):
pass

Now to run it. Use ```set``` to set an array of middleware and ```add``` to add one to the array. ```set``` overwrites everything. That's just what ```set``` means.

handler = Handler()
handler.set([AdditionMiddleware1, AdditionMiddleware2])
handler.add(AdditionMiddleware3)
handler.execute()

# handler['counter'] == 3

In this case, there is no initial context and each of the three middleware increment a counter ending with ```handler['counter'] == 3```.

You can skip the entire class stuff too:

handler = Handler()
def inline(wma, context):
context['myvalue'] = 12
handler.add(inline)
handler.execute()
# handler['myvalue'] == 12

Use the following to send initial context:

handler = Handler(counter=1)

It's actually ```kwargs```, so you can load it up:

handler = Handler(**{'a': 1, 'b': 2})
def inline(wma, context):
context['a'] = context['a'] + context['b']
handler.add(inline)
handler.execute()

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

middleware-1.2.1.tar.gz (3.9 kB view details)

Uploaded Source

File details

Details for the file middleware-1.2.1.tar.gz.

File metadata

  • Download URL: middleware-1.2.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for middleware-1.2.1.tar.gz
Algorithm Hash digest
SHA256 03cca41fb244456935263b7278c31f1d258489d7cde7b358c6e6681231c67a19
MD5 3020968d6573f9face5fe0753bb34ce4
BLAKE2b-256 971cafb0bb09736580a999ee031838c1bfde0c4508662d817223f43a8f320125

See more details on using hashes here.

Supported by

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