librelist archives

« back to archive

Method routing

Method routing

From:
Live Flex
Date:
2012-07-24 @ 10:28
At the moment, flask handles method routing with the "methods" option in
decorators.

1. If you only want to handle only POST methods, for example, then you have
to do something like the following (not expressive):

@app.route('/myuri', methods=['POST'])

2. It also means that if you have a single uri that needs to handle both
GET and POST, you end up with (possibly lengthy) if ... elif statements

One way around this is to have separate method decorators, such as:
@app.get('/myuri') # this is the same as the current @app.route, since get
is the default method anyway
@app.post('/myuri') # this route will only respond to post requests

Looking at the current structure of flask/app.py, this would seem a
relatively easy mod to add in.

3. I realise that one of the downsides is you may have to duplicate the
route syntax if you decide to separate it out like this. For complicated
routes, you would still be able to use the existing routing mechanism.

Any thoughts on this?

Re: [flask] Method routing

From:
Serge S. Koval
Date:
2012-07-24 @ 10:46
There are class-based views which can route requests based on the request
method: http://flask.pocoo.org/docs/api/#flask.views.MethodView

While it is not same as having per-view decorators, but it is usually
enough to implement REST-based views.

Serge.

On Tue, Jul 24, 2012 at 1:28 PM, Live Flex <liveflex8@gmail.com> wrote:

> At the moment, flask handles method routing with the "methods" option in
> decorators.
>
> 1. If you only want to handle only POST methods, for example, then you
> have to do something like the following (not expressive):
>
> @app.route('/myuri', methods=['POST'])
>
> 2. It also means that if you have a single uri that needs to handle both
> GET and POST, you end up with (possibly lengthy) if ... elif statements
>
> One way around this is to have separate method decorators, such as:
> @app.get('/myuri') # this is the same as the current @app.route, since get
> is the default method anyway
> @app.post('/myuri') # this route will only respond to post requests
>
> Looking at the current structure of flask/app.py, this would seem a
> relatively easy mod to add in.
>
> 3. I realise that one of the downsides is you may have to duplicate the
> route syntax if you decide to separate it out like this. For complicated
> routes, you would still be able to use the existing routing mechanism.
>
> Any thoughts on this?
>

Re: [flask] Method routing

From:
Smartboy
Date:
2012-07-24 @ 16:08
Actually, I'm building a REST interface myself with Flask, and I don't need
to have if...elif in my code. You can already do the following:

@app.route('/myuri', methods=['GET'])
def send_something():
    pass

@app.route('/myuri', methods=['POST'])
def create_something():
    pass

If you GET /myuri, then it will call send_something, and if you POST
/myuri, then it will call create_something(). I find this much cleaner than
trying to handle both in the same method. While what you proposed looks
nice, it just seems like syntactic sugar to me, unless I don't understand
what you're trying to do. :)

Smartboy

On Tue, Jul 24, 2012 at 3:28 AM, Live Flex <liveflex8@gmail.com> wrote:

> At the moment, flask handles method routing with the "methods" option in
> decorators.
>
> 1. If you only want to handle only POST methods, for example, then you
> have to do something like the following (not expressive):
>
> @app.route('/myuri', methods=['POST'])
>
> 2. It also means that if you have a single uri that needs to handle both
> GET and POST, you end up with (possibly lengthy) if ... elif statements
>
> One way around this is to have separate method decorators, such as:
> @app.get('/myuri') # this is the same as the current @app.route, since get
> is the default method anyway
> @app.post('/myuri') # this route will only respond to post requests
>
> Looking at the current structure of flask/app.py, this would seem a
> relatively easy mod to add in.
>
> 3. I realise that one of the downsides is you may have to duplicate the
> route syntax if you decide to separate it out like this. For complicated
> routes, you would still be able to use the existing routing mechanism.
>
> Any thoughts on this?
>

Re: [flask] Method routing

From:
Live Flex
Date:
2012-07-24 @ 16:59
syntactic sugar - thats exactly what it is ;)

On Tue, Jul 24, 2012 at 5:08 PM, Smartboy <smartboyathome@gmail.com> wrote:

> Actually, I'm building a REST interface myself with Flask, and I don't
> need to have if...elif in my code. You can already do the following:
>
> @app.route('/myuri', methods=['GET'])
> def send_something():
>     pass
>
>
> @app.route('/myuri', methods=['POST'])
> def create_something():
>     pass
>
> If you GET /myuri, then it will call send_something, and if you POST
> /myuri, then it will call create_something(). I find this much cleaner than
> trying to handle both in the same method. While what you proposed looks
> nice, it just seems like syntactic sugar to me, unless I don't understand
> what you're trying to do. :)
>
> Smartboy
>
>
> On Tue, Jul 24, 2012 at 3:28 AM, Live Flex <liveflex8@gmail.com> wrote:
>
>> At the moment, flask handles method routing with the "methods" option in
>> decorators.
>>
>> 1. If you only want to handle only POST methods, for example, then you
>> have to do something like the following (not expressive):
>>
>> @app.route('/myuri', methods=['POST'])
>>
>> 2. It also means that if you have a single uri that needs to handle both
>> GET and POST, you end up with (possibly lengthy) if ... elif statements
>>
>> One way around this is to have separate method decorators, such as:
>> @app.get('/myuri') # this is the same as the current @app.route, since
>> get is the default method anyway
>> @app.post('/myuri') # this route will only respond to post requests
>>
>> Looking at the current structure of flask/app.py, this would seem a
>> relatively easy mod to add in.
>>
>> 3. I realise that one of the downsides is you may have to duplicate the
>> route syntax if you decide to separate it out like this. For complicated
>> routes, you would still be able to use the existing routing mechanism.
>>
>> Any thoughts on this?
>>
>
>

Re: [flask] Method routing

From:
Todd Kennedy
Date:
2012-07-24 @ 16:49
I'm using the MethodView pattern ing the API I'm generating and it's 
working out well.

I took the code from the docs for register_api and extended it to allow 
additional routes added to the "GET" method.  This allows you, for 
example, to access a blog entry not only by it's entry ID, but also from 
the combination of other uniquely-identifying data (like a date and a 
slug).

I just finished playing with it this AM and it seems to be working for my 
use-cases, but I haven't done any extensive testing on it (nor have I 
added the ability to add more than one additional route). And I suppose 
the type could be auto-detected, etc.

Comments/suggestions are welcome!

https://gist.github.com/3171129



On Jul 24, 2012, at 9:08 AM, Smartboy wrote:

> Actually, I'm building a REST interface myself with Flask, and I don't 
need to have if...elif in my code. You can already do the following:
> 
> @app.route('/myuri', methods=['GET'])
> def send_something():
>     pass
> 
> @app.route('/myuri', methods=['POST'])
> def create_something():
>     pass
> 
> If you GET /myuri, then it will call send_something, and if you POST 
/myuri, then it will call create_something(). I find this much cleaner 
than trying to handle both in the same method. While what you proposed 
looks nice, it just seems like syntactic sugar to me, unless I don't 
understand what you're trying to do. :)
> 
> Smartboy
> 
> On Tue, Jul 24, 2012 at 3:28 AM, Live Flex <liveflex8@gmail.com> wrote:
> At the moment, flask handles method routing with the "methods" option in
decorators. 
> 
> 1. If you only want to handle only POST methods, for example, then you 
have to do something like the following (not expressive):
> 
> @app.route('/myuri', methods=['POST'])
> 
> 2. It also means that if you have a single uri that needs to handle both
GET and POST, you end up with (possibly lengthy) if ... elif statements
> 
> One way around this is to have separate method decorators, such as:
> @app.get('/myuri') # this is the same as the current @app.route, since 
get is the default method anyway
> @app.post('/myuri') # this route will only respond to post requests
> 
> Looking at the current structure of flask/app.py, this would seem a 
relatively easy mod to add in.
> 
> 3. I realise that one of the downsides is you may have to duplicate the 
route syntax if you decide to separate it out like this. For complicated 
routes, you would still be able to use the existing routing mechanism.
> 
> Any thoughts on this?
>