繁体   English   中英

单独使用Swagger和Flask

[英]Using Swagger with Flask alone

我发现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集成。

任何帮助表示赞赏。

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

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

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

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

暂无
暂无

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

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