Build and document REST APIs with aiohttp and apispec
Project description
Build and document REST APIs with aiohttp and apispec
Install
pip install aiohttp-apispec
Quickstart
from aiohttp_apispec import docs, use_kwargs, marshal_with, AiohttpApiSpec
from aiohttp import web
from marshmallow import Schema, fields
class RequestSchema(Schema):
id = fields.Int()
name = fields.Str(description='name')
bool_field = fields.Bool()
class ResponseSchema(Schema):
msg = fields.Str()
data = fields.Dict()
@docs(tags=['mytag'],
summary='Test method summary',
description='Test method description')
@use_kwargs(RequestSchema(strict=True))
@marshal_with(ResponseSchema(), 200)
async def index(request):
return web.json_response({'msg': 'done', 'data': {}})
app = web.Application()
app.router.add_post('/v1/test', index)
# init docs with all parameters, usual for ApiSpec
doc = AiohttpApiSpec(title='My Documentation',
version='v1',
url='/api/docs/api-docs')
# add method to form swagger json:
doc.register(app) # we should do it only after all routes are added to router!
# now we can find it on 'http://localhost:8080/api/docs/api-docs'
web.run_app(app)
Adding validation middleware
from aiohttp_apispec import aiohttp_apispec_middleware
...
app.middlewares.append(aiohttp_apispec_middleware)
# now you can access all validated data in route from request['data']
Build swagger client with aiohttp_swagger library
from aiohttp_swagger import setup_swagger
...
doc.register(app)
setup_swagger(app=app, swagger_url='/api/doc', swagger_info=app['swagger_dict'])
# now we can access swagger client on /api/doc url
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
aiohttp-apispec-0.3.1.tar.gz
(4.1 kB
view hashes)
Built Distribution
Close
Hashes for aiohttp_apispec-0.3.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ebafb4e4756b7993db647016dd0d3c56f52ccaf48abc82c80186a3fe7d97845 |
|
MD5 | f5ba1bbab131852cd959614e97213a66 |
|
BLAKE2b-256 | 23ac502c90d0d97b6cd7c6e55afd03983bddb91295989b488db5a1e208e56dca |