|
@@ -0,0 +1,171 @@
|
|
1
|
+# -*- coding: utf-8 -*-
|
|
2
|
+import re
|
|
3
|
+import typing
|
|
4
|
+
|
|
5
|
+import bottle
|
|
6
|
+from apispec import APISpec, Path
|
|
7
|
+from apispec.ext.marshmallow.swagger import schema2jsonschema
|
|
8
|
+
|
|
9
|
+from hapic.decorator import DecoratedController
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+# Bottle regular expression to locate url parameters
|
|
13
|
+from hapic.description import ControllerDescription
|
|
14
|
+
|
|
15
|
+BOTTLE_RE_PATH_URL = re.compile(r'<(?:[^:<>]+:)?([^<>]+)>')
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+def bottle_route_for_view(reference, app):
|
|
19
|
+ for route in app.routes:
|
|
20
|
+ if route.callback == reference:
|
|
21
|
+ return route
|
|
22
|
+ # TODO: specialize exception
|
|
23
|
+ raise Exception('Not found')
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+def bottle_generate_operations(
|
|
27
|
+ spec,
|
|
28
|
+ bottle_route: bottle.Route,
|
|
29
|
+ description: ControllerDescription,
|
|
30
|
+):
|
|
31
|
+ method_operations = dict()
|
|
32
|
+
|
|
33
|
+ # schema based
|
|
34
|
+ if description.input_body:
|
|
35
|
+ schema_class = type(description.input_body.wrapper.processor.schema)
|
|
36
|
+ method_operations.setdefault('parameters', []).append({
|
|
37
|
+ 'in': 'body',
|
|
38
|
+ 'name': 'body',
|
|
39
|
+ 'schema': {
|
|
40
|
+ '$ref': '#/definitions/{}'.format(schema_class.__name__)
|
|
41
|
+ }
|
|
42
|
+ })
|
|
43
|
+
|
|
44
|
+ if description.output_body:
|
|
45
|
+ schema_class = type(description.output_body.wrapper.processor.schema)
|
|
46
|
+ method_operations.setdefault('responses', {})\
|
|
47
|
+ [int(description.output_body.wrapper.default_http_code)] = {
|
|
48
|
+ 'description': str(description.output_body.wrapper.default_http_code), # nopep8
|
|
49
|
+ 'schema': {
|
|
50
|
+ '$ref': '#/definitions/{}'.format(schema_class.__name__)
|
|
51
|
+ }
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ if description.errors:
|
|
55
|
+ for error in description.errors:
|
|
56
|
+ method_operations.setdefault('responses', {})\
|
|
57
|
+ [int(error.wrapper.http_code)] = {
|
|
58
|
+ 'description': str(error.wrapper.http_code),
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ # jsonschema based
|
|
62
|
+ if description.input_path:
|
|
63
|
+ schema_class = type(description.input_path.wrapper.processor.schema)
|
|
64
|
+ jsonschema = schema2jsonschema(schema_class, spec=spec)
|
|
65
|
+ for name, schema in jsonschema.get('properties', {}).items():
|
|
66
|
+ method_operations.setdefault('parameters', []).append({
|
|
67
|
+ 'in': 'path',
|
|
68
|
+ 'name': name,
|
|
69
|
+ 'required': name in jsonschema.get('required', []),
|
|
70
|
+ 'type': schema['type']
|
|
71
|
+ })
|
|
72
|
+
|
|
73
|
+ if description.input_query:
|
|
74
|
+ schema_class = type(description.input_query.wrapper.processor.schema)
|
|
75
|
+ jsonschema = schema2jsonschema(schema_class, spec=spec)
|
|
76
|
+ for name, schema in jsonschema.get('properties', {}).items():
|
|
77
|
+ method_operations.setdefault('parameters', []).append({
|
|
78
|
+ 'in': 'query',
|
|
79
|
+ 'name': name,
|
|
80
|
+ 'required': name in jsonschema.get('required', []),
|
|
81
|
+ 'type': schema['type']
|
|
82
|
+ })
|
|
83
|
+
|
|
84
|
+ operations = {
|
|
85
|
+ bottle_route.method.lower(): method_operations,
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ return operations
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+class DocGenerator(object):
|
|
92
|
+ def get_doc(
|
|
93
|
+ self,
|
|
94
|
+ controllers: typing.List[DecoratedController],
|
|
95
|
+ app,
|
|
96
|
+ ) -> dict:
|
|
97
|
+ # TODO: Découper
|
|
98
|
+ # TODO: bottle specific code !
|
|
99
|
+ if not app:
|
|
100
|
+ app = bottle.default_app()
|
|
101
|
+ else:
|
|
102
|
+ bottle.default_app.push(app)
|
|
103
|
+ flatten = lambda l: [item for sublist in l for item in sublist]
|
|
104
|
+
|
|
105
|
+ spec = APISpec(
|
|
106
|
+ title='Swagger Petstore',
|
|
107
|
+ version='1.0.0',
|
|
108
|
+ plugins=[
|
|
109
|
+ 'apispec.ext.bottle',
|
|
110
|
+ 'apispec.ext.marshmallow',
|
|
111
|
+ ],
|
|
112
|
+ )
|
|
113
|
+
|
|
114
|
+ schemas = []
|
|
115
|
+ # parse schemas
|
|
116
|
+ for controller in controllers:
|
|
117
|
+ description = controller.description
|
|
118
|
+
|
|
119
|
+ if description.input_body:
|
|
120
|
+ schemas.append(type(
|
|
121
|
+ description.input_body.wrapper.processor.schema
|
|
122
|
+ ))
|
|
123
|
+
|
|
124
|
+ if description.input_forms:
|
|
125
|
+ schemas.append(type(
|
|
126
|
+ description.input_forms.wrapper.processor.schema
|
|
127
|
+ ))
|
|
128
|
+
|
|
129
|
+ if description.output_body:
|
|
130
|
+ schemas.append(type(
|
|
131
|
+ description.output_body.wrapper.processor.schema
|
|
132
|
+ ))
|
|
133
|
+
|
|
134
|
+ for schema in set(schemas):
|
|
135
|
+ spec.definition(schema.__name__, schema=schema)
|
|
136
|
+
|
|
137
|
+ # add views
|
|
138
|
+ # with app.test_request_context():
|
|
139
|
+ paths = {}
|
|
140
|
+ for controller in controllers:
|
|
141
|
+ bottle_route = bottle_route_for_view(controller.reference, app)
|
|
142
|
+ swagger_path = BOTTLE_RE_PATH_URL.sub(r'{\1}', bottle_route.rule)
|
|
143
|
+
|
|
144
|
+ operations = bottle_generate_operations(
|
|
145
|
+ spec,
|
|
146
|
+ bottle_route,
|
|
147
|
+ controller.description,
|
|
148
|
+ )
|
|
149
|
+
|
|
150
|
+ path = Path(path=swagger_path, operations=operations)
|
|
151
|
+
|
|
152
|
+ if swagger_path in paths:
|
|
153
|
+ paths[swagger_path].update(path)
|
|
154
|
+ else:
|
|
155
|
+ paths[swagger_path] = path
|
|
156
|
+
|
|
157
|
+ spec.add_path(path)
|
|
158
|
+
|
|
159
|
+ return spec.to_dict()
|
|
160
|
+
|
|
161
|
+ # route_by_callbacks = []
|
|
162
|
+ # routes = flatten(app.router.dyna_routes.values())
|
|
163
|
+ # for path, path_regex, route, func_ in routes:
|
|
164
|
+ # route_by_callbacks.append(route.callback)
|
|
165
|
+ #
|
|
166
|
+ # for description in self._controllers:
|
|
167
|
+ # for path, path_regex, route, func_ in routes:
|
|
168
|
+ # if route.callback == description.reference:
|
|
169
|
+ # # TODO: use description to feed apispec
|
|
170
|
+ # print(route.method, path, description)
|
|
171
|
+ # continue
|