| 1 |
def response_schema(schema, code=200, required=False, description=None): |
| 2 |
""" |
| 3 |
Add response info into the swagger spec |
| 4 |
|
| 5 |
Usage: |
| 6 |
|
| 7 |
.. code-block:: python |
| 8 |
|
| 9 |
from aiohttp import web |
| 10 |
from marshmallow import Schema, fields |
| 11 |
|
| 12 |
|
| 13 |
class ResponseSchema(Schema): |
| 14 |
msg = fields.Str() |
| 15 |
data = fields.Dict() |
| 16 |
|
| 17 |
@response_schema(ResponseSchema(), 200) |
| 18 |
async def index(request): |
| 19 |
return web.json_response({'msg': 'done', 'data': {}}) |
| 20 |
|
| 21 |
:param str description: response description |
| 22 |
:param bool required: |
| 23 |
:param schema: :class:`Schema <marshmallow.Schema>` class or instance |
| 24 |
:param int code: HTTP response code |
| 25 |
""" |
| 26 |
if callable(schema): |
| 27 |
schema = schema() |
| 28 |
|
| 29 |
def wrapper(func): |
| 30 |
if not hasattr(func, "__apispec__"): |
| 31 |
func.__apispec__ = {"schemas": [], "responses": {}, "parameters": []} |
| 32 |
func.__schemas__ = [] |
| 33 |
func.__apispec__["responses"]["%s" % code] = { |
| 34 |
"schema": schema, |
| 35 |
"required": required, |
| 36 |
"description": description or "", |
| 37 |
} |
| 38 |
return func |
| 39 |
|
| 40 |
return wrapper |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
marshal_with = response_schema |