Skip to content

add route decorators#26

Open
r0fls wants to merge 1 commit intosqueaky-pl:masterfrom
r0fls:5
Open

add route decorators#26
r0fls wants to merge 1 commit intosqueaky-pl:masterfrom
r0fls:5

Conversation

@r0fls
Copy link
Contributor

@r0fls r0fls commented Feb 3, 2017

Addresses issue #5

@r0fls
Copy link
Contributor Author

r0fls commented Feb 3, 2017

Usage example:

from japronto import Application

app = Application()

@app.get('/')
def hello(request):
    return request.Response(text='Hello world!')

app.run(debug=True)

@r0fls
Copy link
Contributor Author

r0fls commented Feb 3, 2017

Oh, it looks like you're not ready to implement this :) Didn't read the whole thread... feel free to close.

@ojss
Copy link

ojss commented Feb 7, 2017

@r0fls we can keep it open and maybe modify it as and when @squeaky-pl revamps the routing?

@squeaky-pl
Copy link
Owner

squeaky-pl commented Feb 7, 2017

Yes, I would like to keep it open, if it takes too much time to revamp routing I am gonna covert it into a recipe.

@Martmists-GH
Copy link
Contributor

What's the progress on this?

@gugadev
Copy link

gugadev commented Jan 20, 2018

@squeaky-pl @r0fls Any update about this feature? This is the most "pythonic" and practic way to do routing, like Flask or Sanic.

@sharkguto
Copy link

Any update?

@Detrous
Copy link

Detrous commented Jun 19, 2019

Any update ? @squeaky-pl

@devblack
Copy link

devblack commented Sep 4, 2021

Looks great, add this if to support awaitable handlers.

# import
from inspect import iscoroutinefunction
####

def route(self, path: str = '/', methods: list = []):
        '''
        Shorthand route decorator. Avoids need to register
        handlers to the router directly with `app.router.add_route()`.
        '''
        def decorator(handler):
            async def wrapper(*args, **kwargs):
                # check awaitable handler
                if iscoroutinefunction(handler):
                    return await handler(*args, **kwargs)
                return handler(*args, **kwargs)
            self.router.add_route(path, wrapper, methods=methods)
            return wrapper
        return decorator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants