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:
这里的Flask教程网站说,要创建一个RESTful API,你可以编写扩展restful.Resource的类,然后通过以下方法将它们添加到API:
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('/ path')装饰器的功能,我在Flask应用程序中比较常见。例如,在这里,他们有:
@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类和装饰函数(如果有的话)有什么区别?如果没有差异,我应该按惯例做什么来创建RESTful API?
2 个解决方案
#1
33
Short answer:
简短回答:
restful.Resource is from a Flask-Restful extension, which is not Flask itself. Miguel's tutorial uses Flask to write a restful interface.
restful.Resource来自Flask-Restful扩展,它不是Flask本身。 Miguel的教程使用Flask编写一个安静的界面。
Long answer:
答案很长:
First of all, along with Flask, there are a number of Flask extensions. Although they work together, they are separate packages and are written by individual authors. Flask-Restful is an extension to Flask.
首先,与Flask一起,有许多Flask扩展。虽然它们一起工作,但它们是独立的包,由个别作者编写。 Flask-Restful是Flask的扩展。
Miguel's tutorial explains how you can make a restful api using Flask by itself.
Miguel的教程解释了如何使用Flask自己制作一个安静的api。
Flask-Restful with the aim to saving some of us from re-inventing the wheel, promises to turn a custom class(or a custom Python data structure) to a restful web service.
Flask-Restful旨在使我们中的一些人不再重新发明*,承诺将自定义类(或自定义Python数据结构)转换为宁静的Web服务。
In addition, Flask also documented the usage of MethodView to allow developers to write their own restful APIs. In parallel, Flask-Restless promises to turn a SqlAlchemy class into a restful web service.
此外,Flask还记录了MethodView的用法,以允许开发人员编写自己的restful API。与此同时,Flask-Restless承诺将SqlAlchemy类转换为一个宁静的Web服务。
An update(18/07/2016), flask-api turns a function/view into a restful interface and is designed by the same author(Tom Christie) of django restful framework.
更新(2016年7月18日),flask-api将一个函数/视图转换为一个宁静的界面,由django restful框架的同一作者(Tom Christie)设计。
There are many roads to Roma.
罗马有很多道路。
#2
0
Using methods instead of classes can quickly become confusing when lots of endpoints are needed. Classes/resource are a better way of separating and decoupling logic. Plus your code becomes much more readable and easier to change/fix
当需要大量端点时,使用方法而不是类很快就会变得混乱。类/资源是分离和解耦逻辑的更好方法。此外,您的代码变得更易读,更容易更改/修复
Using flask-restful is probably the best way, although I will have some work to do in order to create the blueprint for your api, configuring routing, handling the request parameters, and many things that every normal api needs, that gets quite annoying.
使用flask-restful可能是最好的方法,虽然为了创建api的蓝图,配置路由,处理请求参数以及每个正常api需要的许多东西,我会做一些工作要做,这非常烦人。
I have built this lightweight framework on top of flask-restful that lets you build restful apis easily without worrying about all the wiring needed, and just focus on defining your api and coding the business logic. You can check it out here: https://github.com/sebastiandev/peach
我已经在burn-restful上构建了这个轻量级框架,可以让您轻松构建restful apis而无需担心所需的所有布线,只需专注于定义api并编写业务逻辑。你可以在这里查看:https://github.com/sebastiandev/peach
It is more oriented to NOSQL databases, but thats only because the proxy for the database that comes as default is for mongodb, if you need sql, you can just make a proxy for sqlachemy (or wait until i find time to build it).
它更面向NOSQL数据库,但这只是因为默认情况下数据库的代理是mongodb,如果你需要sql,你可以只为sqlachemy创建一个代理(或者等到我找到时间构建它)。
#1
33
Short answer:
简短回答:
restful.Resource is from a Flask-Restful extension, which is not Flask itself. Miguel's tutorial uses Flask to write a restful interface.
restful.Resource来自Flask-Restful扩展,它不是Flask本身。 Miguel的教程使用Flask编写一个安静的界面。
Long answer:
答案很长:
First of all, along with Flask, there are a number of Flask extensions. Although they work together, they are separate packages and are written by individual authors. Flask-Restful is an extension to Flask.
首先,与Flask一起,有许多Flask扩展。虽然它们一起工作,但它们是独立的包,由个别作者编写。 Flask-Restful是Flask的扩展。
Miguel's tutorial explains how you can make a restful api using Flask by itself.
Miguel的教程解释了如何使用Flask自己制作一个安静的api。
Flask-Restful with the aim to saving some of us from re-inventing the wheel, promises to turn a custom class(or a custom Python data structure) to a restful web service.
Flask-Restful旨在使我们中的一些人不再重新发明*,承诺将自定义类(或自定义Python数据结构)转换为宁静的Web服务。
In addition, Flask also documented the usage of MethodView to allow developers to write their own restful APIs. In parallel, Flask-Restless promises to turn a SqlAlchemy class into a restful web service.
此外,Flask还记录了MethodView的用法,以允许开发人员编写自己的restful API。与此同时,Flask-Restless承诺将SqlAlchemy类转换为一个宁静的Web服务。
An update(18/07/2016), flask-api turns a function/view into a restful interface and is designed by the same author(Tom Christie) of django restful framework.
更新(2016年7月18日),flask-api将一个函数/视图转换为一个宁静的界面,由django restful框架的同一作者(Tom Christie)设计。
There are many roads to Roma.
罗马有很多道路。
#2
0
Using methods instead of classes can quickly become confusing when lots of endpoints are needed. Classes/resource are a better way of separating and decoupling logic. Plus your code becomes much more readable and easier to change/fix
当需要大量端点时,使用方法而不是类很快就会变得混乱。类/资源是分离和解耦逻辑的更好方法。此外,您的代码变得更易读,更容易更改/修复
Using flask-restful is probably the best way, although I will have some work to do in order to create the blueprint for your api, configuring routing, handling the request parameters, and many things that every normal api needs, that gets quite annoying.
使用flask-restful可能是最好的方法,虽然为了创建api的蓝图,配置路由,处理请求参数以及每个正常api需要的许多东西,我会做一些工作要做,这非常烦人。
I have built this lightweight framework on top of flask-restful that lets you build restful apis easily without worrying about all the wiring needed, and just focus on defining your api and coding the business logic. You can check it out here: https://github.com/sebastiandev/peach
我已经在burn-restful上构建了这个轻量级框架,可以让您轻松构建restful apis而无需担心所需的所有布线,只需专注于定义api并编写业务逻辑。你可以在这里查看:https://github.com/sebastiandev/peach
It is more oriented to NOSQL databases, but thats only because the proxy for the database that comes as default is for mongodb, if you need sql, you can just make a proxy for sqlachemy (or wait until i find time to build it).
它更面向NOSQL数据库,但这只是因为默认情况下数据库的代理是mongodb,如果你需要sql,你可以只为sqlachemy创建一个代理(或者等到我找到时间构建它)。