Skip to main content

aiohttp-routed-middleware

Overview

An extension for aiohttp which provides route local middleware while remainining compatible with the existing router.

With the built in router the technique for managing route local middleware is to make nested applications. However nested applications require a unique url prefix. so the following cannot be achieved:

Request Middleware Handler
GET /post/{id} authenticate, authorise(['post:read']) get_post
POST /post/{id} authenticate, authorise(['post:read:', 'post:write']) create_post
DELETE /post/{id} authenticate, authorise(['post:read:', 'post:write']) delete_post

This router allows a chain of middleware terminated by a handler. For example:

post_app = web.Application(router=UrlDispatcherEx())
post_app.router.add_get('/{id}', authenticate, authorise(['post:read']), get_posts)
post_app.router.add_post('/{id}', authenticate, authorise(['post:read', 'post:write']), get_posts)
post_app.router.add_delete('/{id}', authenticate, authorise(['post:read', 'post:write']), get_posts)

app = web.Application()
app.add_subapp('/post', post_app)

Usage

Basic

A middleware function differs from a normal request handler, in that it gets given the next handler to call.

The following example shows how to add middleware to a route.

from aiohttp import web
from aiohttp_route_middleware import UrlDispatcherEx

app = web.Application(router=UrlDispatcherEx())
app.router.add_get('/', middleware1, middleware2, test)

async def test(request):
    print("..entering handler")
    response = web.Response(text=f"extra_stuff=[{', '.join(request.extra_stuff)}]")
    print("..exiting handler")
    return response

@web.middleware
async def middleware1(request, handler):
    print("entering middleware 1")
    request.extra_stuff = ['foo']
    response = await handler(request)
    print("exiting middleware 1")
    return response

@web.middleware
async def middleware2(request, handler):
    print(".entering middleware 2")
    request.extra_stuff.append('bar')
    response = await handler(request)
    print(".exiting middleware 2")
    return response

app = web.Application(router=UrlDispatcherEx())
app.router.add_get('/', middleware1, middleware2, test)
web.run_app(app)

This would print out the following:

entering middleware 1
.entering middleware 2
..entering handler
..exiting handler
.exiting middleware 2
exiting middleware 1

Middleware failure

A middleware function may choose not to call the next handler; for example if there was an authentication error.

from aiohttp import web
from aiohttp_route_middleware import UrlDispatcherEx

async def test(request):
    return web.Response(text="Success")

@web.middleware
async def authenticate(request, handler):
    return web.Response(body="unauthenticated", status=401)

app = web.Application(router=UrlDispatcherEx())
app.router.add_get('/', authenticate, test)
web.run_app(app)

Installation

You can install it using pip:

pip install aiohttp-route-middleware

Details

The extension provides a router UrlDispatcherEx which extends from the built in class UrlDispatcher. The class can be used in the following manner:

from aiohttp_route_middleware import UrlDispatcherEx

...

app = web.Application(router=UrlDispatcherEx())

The extension allows multiple handlers to be specified. The handlers are called in order until a handler returns a non None response, at which point the response is returned and execution stops.

An example of this might be a route to update a comment on a post, The sequence might be:

  1. Authenticate the user.
  2. Check the user is authorised to post a comment.
  3. Fetch the post.
  4. Post the comment.
app.router.add_post('/comment?post_id=1234', authenticate, authorise, fetch_post, post_comment)

Each handler is written in the same manner as a normal handler, in that it takes a single request argument. The request argument may be modified or enriched by each handler.

Release files for aiohttp-route-middleware 0.0.2

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

Source distribution (sdist)

Source distribution for aiohttp-route-middleware 0.0.2
File Size Uploaded
aiohttp_route_middleware-0.0.2.tar.gz 7.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aiohttp-route-middleware 0.0.2
File Interpreter ABI Platform
aiohttp_route_middleware-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 11.9 kB

Release files / aiohttp_route_middleware-0.0.2.tar.gz

Download URL aiohttp_route_middleware-0.0.2.tar.gz
Size 7.9 kB
Tags Source
SHA-256 checksum
How to use checksums
c112d0eccb1cc5091f163f16e99ebb7d5fa224b0b9eea81f9d69dd6f1b088153
BLAKE2b-256 checksum
How to use checksums
ef3fe76710e7c80bb31cbbf1fcb2e7707a1ef9eec18600c408a8495f42727f19
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.1 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5

Release files / aiohttp_route_middleware-0.0.2-py3-none-any.whl

Download URL aiohttp_route_middleware-0.0.2-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d64e228da14c69ffacf6306d61e1f149db379b3c2c18633ba374ae6435241e32
BLAKE2b-256 checksum
How to use checksums
858b1627b776a7c7a0a868224bfc38af431ad82a92a0435ef45eaa7f9e28d70a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.1 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5

Release history Release notifications | RSS feed

This release

0.0.2 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