2017-06-05 56 views
1

我是SwaggerUI的新手。在我的python代碼中,我有一個名爲'work'的API,它支持POST,PUT和DELETE HTTP方法。使用Swagger在相同def中使用HTTP方法的語法

現在我想創建相同的Swagger文檔。我正在使用以下代碼:

@app.route('/work', methods=['POST', 'PUT', 'DELETE']) 
def work(): 
""" 
    Micro Service Based API for work operations 
    This API is for work to task matching operations 
    --- 
    paths: 
     /cv: 
     put: 
      parameters: 
      - name: body 
       in: body 
       required: true 
       schema: 
       id: data 
       properties: 
        _id: 
         type: string 
       description: Id 
      responses: 
        200: 
         description: Please wait the calculation, you'll receive an email with results 
     delete: 
      parameters: 
      - name: body 
       in: body 
       required: true 
       schema: 
       id: data 
       properties: 
        _id: 
         type: string 
       description: Id 
      responses: 
        200: 
         description: Please wait the calculation, you'll receive an email with results 
     post: 
      responses: 
        200: 
         description: done 
""" 

但是,它似乎沒有工作。

我試圖瀏覽下面的文檔鏈接,但不要太大的幫助 https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#pathsObject

能否請你幫我嗎?

每個HTTP方法請求的參數都不同,我也希望爲我的HTTP UI中的每個方法指定不同的描述。

編輯

將此添加到index.yml文件。

swagger: "2.0" 
info: 
    description: "This is a sample server Petstore server. You can find out more about  Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).  For this sample, you can use the api key `special-key` to test the authorization  filters." 
    version: "1.0.0" 
    title: "Swagger Petstore" 
    schemes: 
- "http" 
paths: 
    /work: 
    put: 
     tags: 
    - "WORK" 
    summary: "Update a Work Score" 
    description: "" 
    consumes: 
    - "application/json" 
    parameters: 
    - in: "body" 
    name: "body" 
    description: "Work ID whose score needs to be updates" 
    required: true 
    schema: 
     $ref: "#/definitions/Data" 
    responses: 
    200: 
     description: "Invalid input" 
    /scoreCompute: 
    post: 
    tags: 
    - "ABCD" 
    summary: "Compute ABCD" 
    description: "" 
    consumes: 
    - "application/json" 
    parameters: 
    - in: "body" 
    name: "body" 
    description: "Compute ABCD" 
    required: true 
    schema: 
     $ref: "#/definitions/TaskId" 
    responses: 
    200: 
     description: "Invalid input" 
definitions: 
    Data: 
type: object 
properties: 
    _id: 
    type: string 
    description: Enter ID 
    TaskId: 
type: object 
properties: 
    job_id: 
    type: string 
    description: Enter ID 

對python代碼進行了上述修改。

@app.route('/work', methods=['POST', 'PUT', 'DELETE']) 
@swag_from('index.yml') 
def work(): 

但是http://127.0.0.1:5000/apidocs/#!/default/什麼也沒有顯示。

+0

如果你把你的yaml代碼放在編輯器http://editor.swagger.io/#/中是什麼意思? –

+0

@ Dan-Dev您是否可以檢查編輯部分,我已經添加了 – amankedia

回答

1

如果您正在使用Flasgger(http://github.com/rochacbruno/flasgger) 可悲的是它不支持定義在同一個文檔字符串不同的HTTP方法呢, 有這個issue opened

但是,有一種解決方法可以使其工作。

1)把你的YAML在一個單獨的文件
2)從揚鞭加載它template_file

YAML文件中,保存爲test.yaml:

definitions: 
    Data: 
    type: object 
    properties: 
     _id: 
     type: string 

paths: 
    /cv: 
    put: 
     parameters: 
     - name: body 
      in: body 
      required: true 
      schema: 
      $ref: '#/definitions/Data' 
     responses: 
     200: 
      description: | 
      Please wait the calculation, you'll receive an email with results 
    delete: 
     parameters: 
     - name: body 
      in: body 
      required: true 
      schema: 
      $ref: '#/definitions/Data' 
     responses: 
     200: 
      description: | 
      Please wait the calculation, you'll receive an email with results 
    post: 
     responses: 
     200: 
      description: done 

然後test.py

from flask import Flask 
from flasgger import Swagger 


app = Flask(__name__) 
Swagger(app, template_file='test.yaml') 


@app.route('/cv', methods=['POST', 'PUT', 'DELETE']) 
def cv(): 
    """ 
    Micro Service Based API for CV operations 
    This API is for job to CVs matching operations 
    """ 


app.run(debug=True) 

而你得到

flasgger

+0

我收到以下錯誤:TypeError:__init __()得到了一個意外的關鍵字參數'template_file' – amankedia

+0

@akedkedia確保您已安裝最新版本0.6.4'pip安裝flasgger == 0.6.4' –

相關問題