簡體   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