简体   繁体   English

Swagger 然后 flask 或相反

[英]Swagger and then flask or opposite

Swagger and then flask or opposite Hello, Swagger 然后 flask 或对面您好,

I'm starting a new poroject who need an API.我正在开始一个需要 API 的新项目。

I 'm going with flask and swagger.我将使用 flask 和 swagger。

A simple question: should i start by defining the API with swagger and then generate the python code or the opposite.一个简单的问题:我应该先用 swagger 定义 API,然后生成 python 代码还是相反。

Thank for your help.感谢您的帮助。

There are solutions for both approaches:两种方法都有解决方案:

If you end up preferring to define the API first, I would recommend connexion .如果您最终更喜欢先定义 API,我会推荐connexion You define OpenAPI (new name for Swagger) specification files, and then you do Python code accordingly.您定义 OpenAPI(Swagger 的新名称)规范文件,然后相应地执行 Python 代码。 I would say that this is the best approach as you can guarantee that whatever code you write afterwards will conform to the specification, that you can provide to whoever wants it.我想说这是最好的方法,因为您可以保证您之后编写的任何代码都符合规范,您可以提供给任何想要它的人。 Another advantage is not mixing up core concepts of your logic with the API specification.另一个优点是不会将逻辑的核心概念与 API 规范混淆。

Otherwise, flask-restplus does the trick.否则, flask-restplus可以解决问题。 This is the most common (but not necessarily better) approach, where you write your Python code and the specification is then generated.这是最常见(但不一定更好)的方法,您可以在其中编写 Python 代码,然后生成规范。 This is the approach I usually follow in simple use cases.这是我在简单用例中通常遵循的方法。

For information i finally choose the openapi-generator tool that allow me to generate python server stub on Flask/connexion stack and my client lib for Android on top of okhttp.有关信息,我最终选择了 openapi-generator 工具,该工具允许我在 Flask/connexion 堆栈上生成 python 服务器存根,并在 okhttp 之上为 Android 生成客户端库。

https://github.com/OpenAPITools/openapi-generator https://github.com/OpenAPITools/openapi-generator

I was rather satisfied with this choice during my project.在我的项目中,我对这个选择相当满意。

To merge your server stub last version with older one you can dedicated a git branch for raw server stub generation and another one wich you fill with your own code.要将您的服务器存根最后一个版本与旧版本合并,您可以专用一个 git 分支来生成原始服务器存根,而另一个分支则由您自己的代码填充。 That way you won't lose your previous version with the code you add to the server stub.这样一来,您添加到服务器存根的代码就不会丢失以前的版本。

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

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