-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathaiohttp.py
More file actions
70 lines (56 loc) · 2.05 KB
/
aiohttp.py
File metadata and controls
70 lines (56 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""Aiohttp plugin. Includes a path helper that allows you to pass an AbstractRoute.
Takes the method from the route and docstring from the route handler.
::
from aiohttp import web
from apispec import APISpec
from pprint import pprint
async def hello(request):
'''Get a greeting endpoint.
---
description: Get a greeting
responses:
200:
description: A greeting to the client
content:
text/plain:
schema:
$ref: '#/definitions/Greeting'
'''
return web.Response(text="hello")
app = web.Application()
app.add_routes([web.get("/hello", hello)])
# Add all aiohttp routes to the APISpec
for route in app.router.routes():
# Don't include HEAD mehods in OpenAPI spec
if route.method == "HEAD":
continue
spec.path(
route=route,
)
pprint(spec.to_dict()["paths"])
# {'/hello': {'get': {'description': 'Get a greeting',
# 'responses': {'200': {'content': {'text/plain': {'schema': {'$ref': '#/definitions/Greeting'}}},
# 'description': 'A greeting to the '
# 'client'}}}}}
""" # noqa: E501
from typing import Any
from aiohttp.web import AbstractRoute
from apispec import BasePlugin, yaml_utils
class AiohttpPlugin(BasePlugin):
def path_helper(
self,
path: str | None = None,
operations: dict | None = None,
parameters: list[dict] | None = None,
*,
route: AbstractRoute | None = None,
**kwargs: Any,
) -> str | None:
"""Path helper that allows passing a aiohttp AbstractRoute"""
assert operations is not None
assert route is not None
docstring = route.handler.__doc__ or ""
operations.update(
{route.method.lower(): yaml_utils.load_yaml_from_docstring(docstring)}
)
return route.resource.canonical