我使用 Python+Flask 和 Flasgger 创建了一个应用程序来创建招摇页面。Swagger 正在正确生成并且工作正常。
我正在使用 WSO2 (v2.5.0) API 管理器并尝试使用 Swagger URL(由上述应用生成)添加新的 API。
在导入 Swagger 的 json 文件时;架构或对象或属性标签(存在于 Swagger 的 json 文件中),它们都没有被 WSO2 识别,这使我的 API 在 WSO2 中发布失败。
下面是我招摇的 json 文件
{
"definitions": {},
"info": {
"description": "powered by Flasgger",
"termsOfService": "/tos",
"title": "A swagger API",
"version": "0.0.1"
},
"paths": {
"/api/runTimeEngine": {
"get": {
"consumes": [
"application/x-www-form-urlencoded"
],
"parameters": [
{
"description": "output options json in string format holding value for output array body to contain intermediate data. {\"optParam1\" true,\"optParam2\" false,\"optParam3\" false,\"optParam4\" false,\"optParam5\" false,\"optParam6\" false,\"optParam7\" false}",
"in": "formData",
"name": "output_request",
"type": "string"
}
],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "Time Analysis Engine predicted output. JSON based",
"schema": {
"properties": {
"ResponseParam1": {
"description": "The ResponseParam1 output at the timestamp analysis was executed",
"type": "number"
},
"ResponseParam2": {
"description": "The ResponseParam2 output at the timestamp analysis was executed",
"type": "number"
},
"ResponseParam3": {
"description": "The ResponseParam3 output at the timestamp analysis was executed",
"type": "number"
},
"timestamp": {
"description": "The time of output in String format. Convert this to date time as per your convenience. The dateTime stays in the same zone format as supplied",
"type": "string"
}
}
}
},
"400": {
"description": "#Bad request.\n\nAuthorization Token Missing.\n\nMalformed Request, Analysis Engine cannot be executed\n
},
"401": {
"description": "You are not authorized to this request"
}
},
"security": [
{
"APIKeyHeader": []
},
{
"APIKeyQueryParam": []
}
],
"summary": "This is the API to execute Time Analysis Engine With Default Configuration",
"tags": [
"Time Analysis Engine API"
]
},
"post": {
"consumes": [
"multipart/form-data"
],
"parameters": [
{
"description": "Configuration File for execution of Time Analysis Engine",
"in": "formData",
"name": "fileParam1",
"required": true,
"type": "file"
},
{
"description": " Data File for execution of Time Analysis Engine",
"in": "formData",
"name": "fileParam2",
"required": true,
"type": "file"
},
{
"description": "output options json in string format holding value for output array body to contain intermediate data. {\"optParam1\" true,\"optParam2\" false,\"optParam3\" false,\"optParam4\" false,\"optParam5\" false,\"optParam6\" false,\"optParam7\" false}",
"in": "formData",
"name": "output_request",
"type": "string"
}
],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "Time Analysis engine predicted output. JSON based",
"schema": {
"properties": {
"ResponseParam1": {
"description": "The ResponseParam1 output at the timestamp analysis was executed",
"type": "number"
},
"ResponseParam2": {
"description": "The ResponseParam2 output at the timestamp analysis was executed",
"type": "number"
},
"ResponseParam3": {
"description": "The ResponseParam3 output at the timestamp analysis was executed",
"type": "number"
},
"timestamp": {
"description": "The time of output in String format. Convert this to date time as per your convenience. The dateTime stays in the same zone format as supplied",
"type": "string"
}
}
}
},
"400": {
"description": "#Bad request.\n\nAuthorization Token Missing.\n\nMalformed Request, Time Analysis Engine cannot be executed\n\nFile holder name is missing. The supported file holders are fileParam1, fileParam2\n\nWrong File uploaded against config param Supported extensions are xlsx or xls\n"
},
"401": {
"description": "You are not authorized to this request"
}
},
"security": [
{
"APIKeyHeader": []
},
{
"APIKeyQueryParam": []
}
],
"summary": "Call this api passing a Config File, Data File and choice of options in json body",
"tags": [
"Time Analysis Engine API"
]
}
}
},
"securityDefinitions": {
"APIKeyHeader": {
"in": "header",
"name": "X-Api-Key",
"type": "apiKey"
},
"APIKeyQueryParam": {
"in": "query",
"name": "api_key",
"type": "apiKey"
}
},
"swagger": "2.0"
}
下面是我写了swagger定义的python代码
from flask import Flask, request, abort, render_template, send_from_directory
from flasgger import Swagger
import os
import sys
engine_main = Flask(__name__)
Swagger(engine_main)
@engine_main.route('/')
def index():
return render_template('index.html')
@engine_main.route('/api/runEngine', methods=['GET'])
def runEngineGet():
"""
This is the API to execute Engine With Default Configuration
---
tags:
- Engine API,
consumes:
- application/x-www-form-urlencoded
produces:
- application/json
parameters:
- in: header
name: Authorization
type: string
schema:
type: string
format: Bearer ********
required: true
description: Bearer ******** where ****** is your api token name. This name is used to make directories
- name: X
in: formData
type: file
required: true
description: File1 for execution of Engine
- name: Y
in: formData
type: file
required: true
description: File2 for execution of Engine
- name: output_request
type: object
in: formData
schema: {"Op1": true,"Op2": false,"Op3": false,"Op4": false,"Op5": false,"Op6": false,"Op7": false}
description: output options json holding value for output array body to contain intermediate data. { "Op1" true, "Op2" false, "Op3" false, "Op4" false, "Op5" false, "Op6" false, "Op7" false}
responses:
400:
description: |
#Bad request.
Authorization Token Missing.
Malformed Request, Engine cannot be executed
File holder name is missing. The supported file holders are X, Y
Wrong File uploaded against config param Supported extensions are xlsx or xls
401:
description: You are not authorized to this request
200:
description: Predicted output. JSON based
schema:
properties:
timestamp:
type: datetime
description: The time of output
Response1:
type: float
description: The Response1 output at the timestamp analysis was executed
Response2:
type: float
description: The Response2 output at the timestamp analysis was executed
Response3:
type: float
description: The Response3 output at the timestamp analysis was executed
"""
//////Method Call
#print(request.json)
if __name__ == '__main__':
engine_main.run(port='9090', debug=True)
有没有其他方法可以为 Python+Flask 应用程序生成适当的招摇,该应用程序使用 json 格式的请求正文,并且 api 成功发布到 WSO2