简体   繁体   English

使用 Flask 创建 RESTful API?

[英]Creating a RESTful API using Flask?

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.但是,我已经看过很多教程,它们都只是使用带有@app.route('/path')装饰器的函数,我更习惯在 Flask 应用程序中看到它们。 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?使用restful.Resource类和仅使用装饰函数(如果有的话)有什么区别? If there are no differences, what should I be doing by convention to create a RESTful API?如果没有差异,我应该按照惯例做什么来创建 RESTful API?

Short answer :简短的回答

restful.Resource is from a Flask-Restful extension, which is not Flask itself. restful.Resource 来自Flask-Restful扩展,它不是 Flask 本身。 Miguel's tutorial uses Flask to write a restful interface. Miguel 的教程使用Flask编写了一个宁静的界面。

Long answer :长答案

First of all, along with Flask, there are a number of Flask extensions .首先,除了 Flask 之外,还有许多Flask 扩展 Although they work together, they are separate packages and are written by individual authors.尽管它们一起工作,但它们是单独的包,并且是由各个作者编写的。 Flask-Restful is an extension to Flask . Flask-RestfulFlask的扩展。

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 服务。 Flask-RESTX , a fork of Flask-Restful , auto-generates api documentation with swagger UI. Flask-RESTXFlask-Restful 的一个分支,使用 swagger UI 自动生成 api 文档。

In addition, Flask also documented the usage of MethodView to allow developers to write their own restful APIs.此外,Flask 还记录MethodView的用法,以允许开发人员编写自己的Restful API。 In parallel, Flask-Restless promises to turn a SqlAlchemy class into a restful web service.同时, Flask-Restless承诺将 SqlAlchemy 类变成一个安静的 Web 服务。

An update(18/07/2016), flask-api turns a function/view into a restful interface and is designed by Tom Christie, the author of django restful framework .更新(18/07/2016), flask-api将函数/视图变成了一个宁静的界面,由django restful framework的作者 Tom Christie 设计。

an update(17/03/2021), Flask-RESTPlus does smiliar things as above libraries but it also helps you construct swagger API documentation, which is an extra bonus.更新(17/03/2021), Flask-RESTPlus做了与上述库类似的事情,但它还可以帮助您构建 swagger API 文档,这是一个额外的好处。

There are many roads to Roma .通往罗马的道路很多

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.我在flask-restful 之上构建了这个轻量级框架,它可以让您轻松构建restful api,而无需担心所需的所有接线,只需专注于定义您的api 和编码业务逻辑。 You can check it out here: https://github.com/sebastiandev/peach你可以在这里查看: 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 制作一个代理(或等到我找到时间来构建它)。

#from flask import Flask

app = Flask(__name__)

@app.route('/')

def index():

    return "Hello, World!"

if __name__ == '__main__':

    app.run(debug=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM