Build and document REST APIs with aiohttp and apispec
Project description
## Install
```
pip install aiohttp-apispec
```
## Quickstart
```Python
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(
app=app, title='My Documentation', version='v1', url='/api/docs/api-docs'
)
# now we can find it on 'http://localhost:8080/api/docs/api-docs'
web.run_app(app)
```
## Adding validation middleware
```Python
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']``` like so:
```Python
@docs(
tags=['mytag'],
summary='Test method summary',
description='Test method description',
)
@use_kwargs(RequestSchema(strict=True))
@marshal_with(ResponseSchema(), 200)
async def index(request):
uid = request['data']['id']
name = request['data']['name']
return web.json_response(
{'msg': 'done', 'data': {'info': f'name - {name}, id - {uid}'}}
)
```
## Build swagger web client
You can do it easily with [aiohttp_swagger](https://github.com/cr0hn/aiohttp-swagger) library:
```Python
from aiohttp_swagger import setup_swagger
...
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 Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for aiohttp_apispec-0.5.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b537001e8382d59b9ebde1842d6a19f01b3882922016a9ce609f933f594a4be4 |
|
MD5 | 4aa238f7fbd166c5d60b189a92c683fc |
|
BLAKE2b-256 | 09a407ac75c80a0837be18bca66cab97b3405a90faf2be21561d9f3d1fcb00b6 |