如何获得aiohttp-swagger来识别GET查询变量?

时间:2018-07-26 16:01:19

标签: python-3.x swagger aiohttp openapi

我想在python项目中使用aiohttp-swagger,但是我不知道它如何处理GET URL和POST负载变量。我有基于quick start example here的示例代码。我所做的唯一更改是,我在GET URL中包含了一个查询参数。

from aiohttp import web
from aiohttp_swagger import *


async def ping(request):
    """
    ---
    description: This end-point allow to test that service is up.
    tags:
    - Health check
    produces:
    - text/plain
    responses:
        "200":
            description: successful operation. Return "pong" text
        "405":
            description: invalid HTTP Method
    """
    return web.Response(text="pong %s" % request.match_info['var']) # change here (1/2)


app = web.Application()
app.router.add_route('GET', "/ping/{var}", ping) # change here (2/2)

setup_swagger(app)

web.run_app(app, host="127.0.0.1")

生成的Swagger / OpenAPI页面似乎不知道该变量。我预计它将生成一个文本框,在其中可以为“ var”查询变量填写一个值。我该如何使用aio-http?可能吗?如果没有,还有另一个库可以处理吗?

Swagger page missing "var" textbox

为了便于记录,我拥有C#背景,并且过去使用Swashbuckle library做到了这一点。

1 个答案:

答案 0 :(得分:1)

您必须在注释中添加parameters属性。

工作示例:

from aiohttp import web
from aiohttp_swagger import *


async def ping(request):
    """
    ---
    description: This end-point allow to test that service is up.
    tags:
    - Health check
    parameters: 
    - in: query
      name: var
    produces:
    - text/plain
    responses:
        "200":
            description: successful operation. Return "pong" text
        "405":
            description: invalid HTTP Method
    """
    return web.Response(text="pong %s" % request.match_info['var']) # change here (1/2)


app = web.Application()
app.router.add_route('GET', "/ping/{var}", ping) # change here (2/2)

setup_swagger(app)

web.run_app(app, host="127.0.0.1")