aiohttp-transmute¶
A transmute framework for aiohttp. This framework provides:
- declarative generation of http handler interfaces by parsing function annotations
- validation and serialization to and from a variety of content types (e.g. json or yaml).
- validation and serialization to and from native python objects, using schematics.
- autodocumentation of all handlers generated this way, via swagger.
Example¶
from aiohttp import web
import aiohttp_transmute
# define a GET endpoint, taking a query parameter integers left and right,
# which must be integers.
@aiohttp_transmute.describe(paths="/customers/{name}")
async def multiply(request, name: str, left: int, right: int) -> int:
return left + right
# define a POST endpoint, taking a query parameter integers left and right,
# which must be integers.
@aiohttp_transmute.describe(methods="POST", paths="/customers/{name}")
async def multiply_post(request, name: str, left: int, right: int) -> int:
return left + right
app = web.Application()
# use add_route to add the function to the app.
aiohttp_transmute.route(app, multiply)
aiohttp_transmute.route(app, multiply_post)
# this should be at the end, to ensure all routes are considered when
# constructing the handler.
# this will add:
# - a swagger.json definition
# - a static page that renders the swagger.json
aiohttp_transmute.add_swagger(app, "/swagger.json", "/swagger")
web.run_app(app)
A lot of relevant documentation for aiohttp-transmute can be found at transmute-core docs
Contents: