Skip to main content

Local middleware routing for aiohttp.

Project description

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.

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

aiohttp_route_middleware-0.0.2.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

aiohttp_route_middleware-0.0.2-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp_route_middleware-0.0.2.tar.gz.

File metadata

  • Download URL: aiohttp_route_middleware-0.0.2.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for aiohttp_route_middleware-0.0.2.tar.gz
Algorithm Hash digest
SHA256 c112d0eccb1cc5091f163f16e99ebb7d5fa224b0b9eea81f9d69dd6f1b088153
MD5 7df6982783075c6bd5751af84c334c6c
BLAKE2b-256 ef3fe76710e7c80bb31cbbf1fcb2e7707a1ef9eec18600c408a8495f42727f19

See more details on using hashes here.

File details

Details for the file aiohttp_route_middleware-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: aiohttp_route_middleware-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for aiohttp_route_middleware-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d64e228da14c69ffacf6306d61e1f149db379b3c2c18633ba374ae6435241e32
MD5 740cbf492615a33b2bad5cb97de1668d
BLAKE2b-256 858b1627b776a7c7a0a868224bfc38af431ad82a92a0435ef45eaa7f9e28d70a

See more details on using hashes here.

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