Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware by route #3590

Closed
rafa-acioly opened this issue Jan 31, 2019 · 9 comments
Closed

Middleware by route #3590

rafa-acioly opened this issue Jan 31, 2019 · 9 comments

Comments

@rafa-acioly
Copy link

Looking into the documentation about middleware i didn't found anything about adding a middleware to a single route, in my case i would like to validate the request payload on each route because each route has a diferente body request, is that possible?

@samuelcolvin samuelcolvin added the question StackOverflow label Jan 31, 2019
@samuelcolvin
Copy link
Member

samuelcolvin commented Jan 31, 2019

The whole point if middleware is to work on multiple routes.

If you want to run logic on just endpoint view, you should add the logic to the view.

However, if you really want to do this, you can do something like

@middlware
async def my_middleware(request, handler):
  if request.path == '/foobar/'
     ....  # my custom logic
  else:
    return await handler(request)

@aio-libs aio-libs deleted a comment from aio-libs-bot Jan 31, 2019
@samuelcolvin
Copy link
Member

samuelcolvin commented Jan 31, 2019

You can even look at route names, via

if isinstance(request.match_info, MatchInfoError):
  return
if request.match_info.route.name in {'foo', 'bar', ...}:
  ...

@aio-libs-bot aio-libs-bot added enhancement and removed question StackOverflow labels Jan 31, 2019
@rafa-acioly
Copy link
Author

rafa-acioly commented Jan 31, 2019

Ye, i get it, i was imagining something like laravel and spring does.

The whole point if middleware is to work on multiple routes.

not on every case, let suppose that i have a service where i charge my clients per feature, before the client enter the section of that feature i need to know if he paid for it, but this verification don't need to be on every route of my project.

here is a example on laravel.
https://laravel.com/docs/5.7/middleware#registering-middleware

Thank you anyway!

@parthibd
Copy link

It would be helpful if route middleware is available in the core of aiohttp . Also it would make up for a great feature !

@asvetlov
Copy link
Member

A wrapper/decorator for your web-handler can do this work pretty well; the decorator works with any aiohttp version pretty well.

@parthibd
Copy link

Can you kindly give me an example?

@asvetlov
Copy link
Member

import functools


def deco(func):
    @functools.wraps(func)
    async def wrapper(request):
        await prepare(request)
        try:
            return await func(request)
        finally:
            await finalize(request)

    return wrapper

@deco
async def handler(request):
    return web.Response(text='OK')


app.router.add_get('/', handler)

@parthibd
Copy link

Thank you so much for the prompt reply! That's exactly what I was looking for :)

@asvetlov
Copy link
Member

Welcome!

@lock lock bot added the outdated label Feb 21, 2020
@lock lock bot locked as resolved and limited conversation to collaborators Feb 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants