首页 文章

单独使用Swagger和Flask

提问于
浏览
2

我已经发现Swagger为我开发的Restful Api生成了doc,但问题是我单独使用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)

但是当我试图获得 /api/spec/ 时,我获得了 Not Found . 我的问题是,有任何方法可以避免使用burn-restful-swagger并将Swagger仅与Flask集成 .

任何帮助表示赞赏 .

2 回答

  • 0

    我个人使用Flask的"Swagger-first"方法,即在实现操作之前编写Swagger API(YAML) . Connexion库非常好地支持这种方法:https://pypi.python.org/pypi/connexion(我是其中一位作者)

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

  • 4

    试试https://github.com/gangverk/flask-swagger

    它支持MethodView类(如Flask-RESTful用法)和vanilla flask方法

相关问题