单独使用Swagger和Flask

Vic*_*ler 3 python api rest flask

我发现Swagger可以为我开发的Restful Api生成文档,但是问题是我只使用Flask而不是flask-restful,当我尝试使用flask-restful-swagger包装器时,它不起作用,因为像这样的例子:

from sqlalchemy.orm import create_session
from flask import Flask, request, jsonify
from Model import *
from flask_restful import Api
from flask_restful_swagger import swagger  

app = Flask(__name__)
api = swagger.docs(Api(app), apiVersion='0.1', api_spec_url="/api/spec")
session = create_session(bind=engine)


@app.route('/employees/', methods=['GET'])
@swagger.operation(
   parameters=[
      {
         "name": "body",
         "description": "Get the employees in the shop.",
         "required": True,
         "allowMultiple": False,
         "dataType": NsEmployee.__name__,
         "paramType": "body"
      }
    ],

   responseMessages=[
      {
          "code": 201,
          "message": "Created. The URL of the created blueprint should appear in the Location header"
      },
      {
          "code": 405,
          "message": "Invalid input"
      }
])

def employees():
   if request.method == 'GET':
     # do something...        

   return jsonify(json_results)


if __name__ == '__main__':
   app.run(debug=True)
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试获取时,会/api/spec/得到“ 未找到”。我的问题是有什么方法可以避免使用flask-restful-swagger并将Swagger仅与Flask集成。

任何帮助表示赞赏。

小智 5

我个人对Flask使用“ Swagger-first”方法,即在实现操作之前编写Swagger API(YAML)。Connexion库很好地支持了这种方法:https : //pypi.python.org/pypi/connexion(我是作者之一)

Connexion不使用flask-restful,而是为您提供“自动”验证和类型映射(根据Swagger规范)。