Skip to main content

Builds openapi schemas for aiohttp apps.

Project description

aiohttp-openapi-ermelo

This library helps you to build openapi schemas for your aiohttp app, and serve documentation uis (Swagger) for the schema.

Installation

Add "aiohttp-openapi-ermelo" to your project dependencies. If you wish to use yaml functionality, then please add "aiohttp-openapi-ermelo[yaml]".

How to use

Once you have created an aiohttp Application, create an OpenAPIApp, and specify the global parameters for your schema using classes from openapi-pydantic:

from aiohttp.web import Application, Request, Response
from aiohttp_openapi import OpenAPIApp, SwaggerUI, operation
from openapi_pydantic import Info, OpenAPI, Operation

app = Application()
api = OpenAPIApp(
    app,
    OpenAPI(info=Info(version="1.0.0", title="Petstore")),
    url_base="/api/",
    doc_uis=(SwaggerUI(),),
)

Then routes to handlers can be added while specifying the parameters for the operation.

async def get_pets(request: Request):
    return json_response([{"id": 10, "name": "doggie"}])

api.add_route("GET", "pets", get_pets, summary="List all pets", operationId="listPets", tags=["pets"])

api.add_route, and it's related helper functions works much like app.router.add_route. It also allows you to specify parameters for the operation. This operation gets set for the relevant path on the schema.

Note that the path is relative the the url_base, so for the example above, the path for this example will be "/api/pets". If you want to specify an absolute path, start the path with a /.

One can specify the operation parameters using the @operation decorator:

@operation(summary="List all pets", operationId="listPets", tags=["pets"])
async def get_pets(request: Request):
    return json_response([{"id": 10, "name": "doggie"}])

api.add_route("GET", "pets", get_pets)

Specifying operation parameters

When specifying operation parameters on either add_route or @operation, there are a few options on how to provide parameters:

  • Specify an Operation object:
@operation(operation=Operation(summary="List all pets", operationId="listPets", tags=["pets"]))
async def get_pets(request: Request): ...
  • Specify Operation arguments directly:
@operation(summary="List all pets", operationId="listPets", tags=["pets"])
async def get_pets(request: Request): ...
  • If no summary is provided, and the handler has a doc string, then the doc string is used as the summary. Hence the bellow example is the same as above:
@operation(operationId="listPets", tags=["pets"])
async def get_pets(request: Request):
    "List all pets"
    ...
  • Specify json text:
@operation(json='{"summary":"List all pets","operationId":"listPets","tags":["pets"]}')
async def get_pets(request: Request): ...
  • Specify yaml text:
@operation(yaml="""
    summary: List all pets
    operationId: listPets
    tags:
    - pets
""")
async def get_pets(request: Request): ...
  • Specify yaml text as a docstring:
@operation(yaml_docstring=True)
async def get_pets(request: Request):
    """
    summary: List all pets
    operationId: listPets
    tags:
    - pets
    """
    ...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiohttp_openapi_ermelo-2.0.tar.gz (644.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiohttp_openapi_ermelo-2.0-py3-none-any.whl (549.4 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp_openapi_ermelo-2.0.tar.gz.

File metadata

File hashes

Hashes for aiohttp_openapi_ermelo-2.0.tar.gz
Algorithm Hash digest
SHA256 557b9d7e215cfffb2d424462e47ba06fce6e4b27551811251c6e5fc9d77e2603
MD5 4c7759382f2dea351f17cc70c9164092
BLAKE2b-256 5590fe6ababbf19bac37544eacbda12c24565d2f8e25344ae76b3a9de255888a

See more details on using hashes here.

File details

Details for the file aiohttp_openapi_ermelo-2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for aiohttp_openapi_ermelo-2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1c2e84437c0fb7c1e472985d8506698a0201121cfa637f4ad921e45f9d8d50ab
MD5 6a0e4eb85787d131b59d805fcf7f5ac6
BLAKE2b-256 16e31e76e0a6c6cffea663bd3879431265f11ef99cc39a7161a966e00ff2dfb0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page