Example #1
0
def main():
    app = web.Application()
    s = SwaggerFile(app, "/docs", "petstore.yaml")
    s.add_routes([
        web.get("/pets", get_all_pets),
        web.get("/pets/{pet_id}", get_one_pet),
        web.delete("/pets/{pet_id}", delete_one_pet),
        web.post("/pets", create_pet),
    ])
    app["storage"] = {}
    web.run_app(app)
Example #2
0
async def test_class_based_spec_file(aiohttp_client, loop):
    app = web.Application(loop=loop)
    s = SwaggerFile(app, "/docs", "tests/testdata/petstore.yaml")

    class Pets(web.View):
        async def get(self, limit: Optional[int] = None):
            pets = []
            for i in range(limit or 3):
                pets.append({"id": i, "name": f"pet_{i}", "tag": f"tag_{i}"})
            return web.json_response(pets)

        async def post(self, body: Dict):
            return web.json_response(body, status=201)

    s.add_routes([web.view("/pets", Pets)])

    client = await aiohttp_client(app)

    resp = await client.get("/pets", params={"limit": 1})
    assert resp.status == 200
    assert await resp.json() == [{"id": 0, "name": "pet_0", "tag": "tag_0"}]

    resp = await client.get("/pets")
    assert resp.status == 200
    assert await resp.json() == [
        {
            "id": 0,
            "name": "pet_0",
            "tag": "tag_0"
        },
        {
            "id": 1,
            "name": "pet_1",
            "tag": "tag_1"
        },
        {
            "id": 2,
            "name": "pet_2",
            "tag": "tag_2"
        },
    ]

    req = {"id": 10, "name": "pet", "tag": "tag"}
    resp = await client.post("/pets", json=req)
    assert resp.status == 201
    assert await resp.json() == req
Example #3
0
def main():
    app = web.Application()
    s = SwaggerFile(
        app,
        spec_file="petstore.yaml",
        swagger_ui_settings=SwaggerUiSettings(path="/docs"),
    )
    s.add_routes(
        [
            web.get("/pets", get_all_pets),
            web.get("/pets/{pet_id}", get_one_pet),
            web.delete("/pets/{pet_id}", delete_one_pet),
            web.post("/pets", create_pet),
        ]
    )
    app["storage"] = {}
    web.run_app(app)
async def test_spec_file(aiohttp_client, loop):
    app = web.Application(loop=loop)
    s = SwaggerFile(app, "/docs", "tests/testdata/petstore.yaml")

    async def get_all_pets(request, limit: Optional[int] = None):
        pets = []
        for i in range(limit or 3):
            pets.append({"id": i, "name": f"pet_{i}", "tag": f"tag_{i}"})
        return web.json_response(pets)

    async def create_pet(request, body: Dict):
        return web.json_response(body, status=201)

    async def get_one_pet(request, pet_id: int):
        if pet_id in (1, 2, 3):
            return web.json_response({
                "id": pet_id,
                "name": f"pet_{pet_id}",
                "tag": f"tag_{pet_id}"
            })
        return web.json_response(
            {
                "code": 10,
                "message": f"pet with ID '{pet_id}' not found"
            },
            status=500)

    s.add_routes([
        web.get("/pets", get_all_pets),
        web.get("/pets/{pet_id}", get_one_pet),
        web.post("/pets", create_pet),
    ])

    client = await aiohttp_client(app)

    resp = await client.get("/pets", params={"limit": 1})
    assert resp.status == 200
    assert await resp.json() == [{"id": 0, "name": "pet_0", "tag": "tag_0"}]

    resp = await client.get("/pets")
    assert resp.status == 200
    assert await resp.json() == [
        {
            "id": 0,
            "name": "pet_0",
            "tag": "tag_0"
        },
        {
            "id": 1,
            "name": "pet_1",
            "tag": "tag_1"
        },
        {
            "id": 2,
            "name": "pet_2",
            "tag": "tag_2"
        },
    ]

    req = {"id": 10, "name": "pet", "tag": "tag"}
    resp = await client.post("/pets", json=req)
    assert resp.status == 201
    assert await resp.json() == req

    resp = await client.get("/pets/1")
    assert resp.status == 200
    assert await resp.json() == {"id": 1, "name": "pet_1", "tag": "tag_1"}

    resp = await client.get(f"/pets/1")
    assert resp.status == 200
    assert await resp.json() == {"id": 1, "name": "pet_1", "tag": "tag_1"}

    resp = await client.get(f"/pets/100")
    assert resp.status == 500
    assert await resp.json() == {
        "code": 10,
        "message": f"pet with ID '100' not found"
    }