Re: [flask] API: Seperated methods?
- From:
- Simon Sapin
- Date:
- 2011-01-29 @ 13:38
Le 29/01/2011 20:39, Petar Radosevic a écrit :
> The first is creating the API by using seperated methods for every HTTP
> method. For example:
>
> @api.route("/", methods=["GET"])
> def get_articles():
> return "Get a list of articles."
>
> @api.route("/", methods=["POST"])
> def post_article():
> return "Create an article."
>
> Or would it be better to do it all in one method:
>
> @api.route("/", methods=["GET", "POST"])
> def articles():
> if request.method == "GET":
> return "Get a list of articles"
> if request.method == "POST":
> return "Create an article"
> else: abort(405)
Hi,
Definitely the first solution. They have the same behavior except that
in the first, Flask does the same dispatching the second does with if
and abort. And Flask does it in a tested and documented way.
If both your views share much code, put that in an utility function.
Regards,
--
Simon Sapin
http://exyr.org/
Re: [flask] API: Seperated methods?
- From:
- Petar Radosevic
- Date:
- 2011-01-29 @ 19:36
> Definitely the first solution. They have the same behavior except that
> in the first, Flask does the same dispatching the second does with if
> and abort. And Flask does it in a tested and documented way.
>
> If both your views share much code, put that in an utility function.
Thanks for your input. This also seemed the more logical approach to me.
I starting building my REST API as mentioned in the first solution.
--
Petar Radošević, Programmer
wunki.org | @wunki_
PGP Fingerprint: CCDD 7545 4907 D40D C0AC 3AC2 FDDC 2E15 AC44 2A15
PGP Publickey : http://wunki.org/pgp.txt