The Flask tutorial site here says that to create a RESTful API, you would write classes that extend restful.Resource
, then add them to the API by:
app = Flask(__name__)
api = restful.Api(app)
class HelloWorld(restful.Resource):
def get(self):
return {'hello': 'world'}
api.add_resource(HelloWorld, '/')
However, I've looked at quite a few tutorials that all just use functions with the @app.route('/path')
decorator that I'm more used to seeing in Flask apps. For example, here, they have:
@app.route('/todo/api/v1.0/tasks', methods=['GET'])
def get_tasks():
return jsonify({'tasks': tasks})
And here:
@app.route('/')
def api_root():
return 'Welcome'
What's the difference between using the restful.Resource
class and just the decorated functions if any? If there are no differences, what should I be doing by convention to create a RESTful API?
restful.Resource
originates from Flask extension.app.route
is a solution based on pure Flask (and you can implement simple APIs very easily that way). After adding somethings like simple API exceptions this basic solution it is pretty much enough for simple APIs. – Forney