12

What is difference between MethodView and Resource?

It implements API by flask-restful:

class API(Resource):
    decorators = [...,]

    def get(self):
        # do something
    def post(self):
        # do something
    def put(self):
        # do something
    def delete(self):
        # do something

Actually, it can be replaced by flask:

class API(MethodView):
    decorators = [...,]

    def get(self):
        # do something
    def post(self):
        # do something
    def put(self):
        # do something
    def delete(self):
        # do something

I think Flask has offered enough about establishing Restful API. I can't find flask-restful can do more something than flask because they have CRUD methods and decoraters in class of mechanism in the same. What is special about flask-restful?

I am evaluating whether or not Flask-Restful is really necessary for me. Please tell me, thanks.

Tony
  • 1,019
  • 2
  • 13
  • 25

1 Answers1

5

I was wondering same thing and according to this post Resource is inherited from Methodview (http://blog.miguelgrinberg.com/post/designing-a-restful-api-using-flask-restful). Article describes also added value to compared to plain Flask like "Flask-RESTful provides a much better way to handle this with the RequestParser class. This class works in a similar way as argparse for command line arguments." And much of work with your API is still something to do with authentication and security like parameters/data checking.

Thx to Miguel to excellent blog. I'm using flask-restful because it seems to be quite mature.

If your need is very tiny, then I think you can use flask only approach.

Eino Mäkitalo
  • 408
  • 3
  • 11
  • 8
    If you consider using flask-restful for its RequestParser, I suggest you read those links: http://flask-restful.readthedocs.io/en/0.3.5/reqparse.html and https://github.com/flask-restful/flask-restful/issues/335. Basically, the request parser is deprecated in favor of webargs. It is suggested (and blessed by flask-restful dev) that nowadays, you might as well use flask MethodView + flask-classful + marshmallow + webargs (flask-classful is a fork of abandoned flask-classy). – Jérôme May 12 '16 at 09:49