| 1 | def docs(**kwargs): |
| 2 | """ |
| 3 | Annotate the decorated view function with the specified Swagger |
| 4 | attributes. |
| 5 | |
| 6 | Usage: |
| 7 | |
| 8 | .. code-block:: python |
| 9 | |
| 10 | from aiohttp import web |
| 11 | |
| 12 | @docs(tags=['my_tag'], |
| 13 | summary='Test method summary', |
| 14 | description='Test method description', |
| 15 | parameters=[{ |
| 16 | 'in': 'header', |
| 17 | 'name': 'X-Request-ID', |
| 18 | 'schema': {'type': 'string', 'format': 'uuid'}, |
| 19 | 'required': 'true' |
| 20 | }] |
| 21 | ) |
| 22 | async def index(request): |
| 23 | return web.json_response({'msg': 'done', 'data': {}}) |
| 24 | |
| 25 | """ |
| 26 | |
| 27 | def wrapper(func): |
| 28 | if not kwargs.get("produces"): |
| 29 | kwargs["produces"] = ["application/json"] |
| 30 | if not hasattr(func, "__apispec__"): |
| 31 | func.__apispec__ = {"schemas": [], "responses": {}, "parameters": []} |
| 32 | func.__schemas__ = [] |
| 33 | extra_parameters = kwargs.pop("parameters", []) |
| 34 | extra_responses = kwargs.pop("responses", {}) |
| 35 | func.__apispec__["parameters"].extend(extra_parameters) |
| 36 | func.__apispec__["responses"].update(extra_responses) |
| 37 | func.__apispec__.update(kwargs) |
| 38 | return func |
| 39 | |
| 40 | return wrapper |