Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask MethodView vs Flask-Restful Resource

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.

like image 855
Tony Avatar asked Jan 29 '16 07:01

Tony


People also ask

What is the difference between Flask and Flask RESTful?

Flask-RESTful is an extension for Flask that adds support for quickly building REST APIs. It is a lightweight abstraction that works with your existing ORM/libraries. Flask-RESTful encourages best practices with minimal setup. If you are familiar with Flask, Flask-RESTful should be easy to pick up.

What is resource in Flask RESTful?

resource ( Type[Resource] ) – the class name of your resource. urls (str) – one or more url routes to match for the resource, standard flask routing rules apply. Any url variables will be passed to the resource method as args. endpoint (str) – endpoint name (defaults to Resource. __name__.

Is Flask GOOD FOR REST API?

Being lightweight, easy to adopt, well-documented, and popular, Flask is a good option for developing RESTful APIs.

What is Flask RESTful API?

Flask Restful is an extension for Flask that adds support for building REST APIs in Python using Flask as the back-end. It encourages best practices and is very easy to set up. Flask restful is very easy to pick up if you're already familiar with flask. In flask_restful , the main building block is a resource.


1 Answers

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.

like image 58
Eino Mäkitalo Avatar answered Oct 24 '22 04:10

Eino Mäkitalo