2

I'd like to use aiohttp-swagger in my python Project but I can't figure out how it handles GET URL and POST payload variables. I have this sample code based on the quick start example here. The only change I made is that I included a query parameter in the 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")

The Swagger/OpenAPI page that gets generated doesn't seem to be aware of the variable. I expeected that it'd generate a textbox where I can fill in a value for the "var" query variable. How do I do this with aio-http? Is it possible? If not, is there another library that can handle this?

Swagger page missing "var" textbox

For the record, I have a C# background and I've done this in the past using the Swashbuckle library.

user2023861
  • 8,030
  • 9
  • 57
  • 86

1 Answers1

1

You must add parameters attr to your annotation.

Working example:

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")
Yurii Kramarenko
  • 1,025
  • 6
  • 16
  • 1
    I got the URL to work as I'd expect by changing the parameter "in" value from "query" to "path". With "query", the URL would be "http://localhost:8888/ping/{var}?var=abc". With "path" it's "http://localhost:8888/ping/abc". I wish there was a way for the library to automatically figure out the parameters so that I don't have to manually type them in. That's what I'm used to with C#/Swagger/Swashbuckle. – user2023861 Aug 21 '18 at 21:59