瀏覽代碼

documentation view: dynamic render of doc instead rite file

Bastien Sevajol 6 年之前
父節點
當前提交
1f866c2453
共有 3 個文件被更改,包括 42 次插入105 次删除
  1. 21 8
      hapic/doc.py
  2. 21 3
      hapic/hapic.py
  3. 0 94
      hapic/static/swaggerui/spec.yml

+ 21 - 8
hapic/doc.py 查看文件

@@ -191,15 +191,13 @@ class DocGenerator(object):
191 191
 
192 192
         return spec.to_dict()
193 193
 
194
-    def save_in_file(
194
+    def get_doc_yaml(
195 195
         self,
196
-        doc_file_path: str,
197 196
         controllers: typing.List[DecoratedController],
198 197
         context: ContextInterface,
199
-        title: str='',
200
-        description: str='',
201
-    ) -> None:
202
-        # generate this file
198
+        title: str = '',
199
+        description: str = '',
200
+    ) -> str:
203 201
         dict_doc = self.get_doc(
204 202
             controllers=controllers,
205 203
             context=context,
@@ -211,9 +209,24 @@ class DocGenerator(object):
211 209
         # We dump then load with json to use real scalar dict.
212 210
         # If not, yaml dump dict-like objects
213 211
         clean_dict_doc = json.loads(json_doc)
214
-        yaml_doc = yaml.dump(clean_dict_doc, default_flow_style=False)
212
+        return yaml.dump(clean_dict_doc, default_flow_style=False)
213
+
214
+    def save_in_file(
215
+        self,
216
+        doc_file_path: str,
217
+        controllers: typing.List[DecoratedController],
218
+        context: ContextInterface,
219
+        title: str='',
220
+        description: str='',
221
+    ) -> None:
222
+        doc_yaml = self.get_doc_yaml(
223
+            controllers=controllers,
224
+            context=context,
225
+            title=title,
226
+            description=description,
227
+        )
215 228
         with open(doc_file_path, 'w+') as doc_file:
216
-            doc_file.write(yaml_doc)
229
+            doc_file.write(doc_yaml)
217 230
 
218 231
 
219 232
 # TODO BS 20171109: Must take care of already existing definition names

+ 21 - 3
hapic/hapic.py 查看文件

@@ -420,9 +420,20 @@ class Hapic(object):
420 420
             swaggerui_path,
421 421
         )
422 422
 
423
-        # Generate documentation file
424
-        doc_page_path = os.path.join(swaggerui_path, 'spec.yml')
425
-        self.save_doc_in_file(doc_page_path)
423
+        # Documentation file view
424
+        doc_yaml = self.doc_generator.get_doc_yaml(
425
+            controllers=self._controllers,
426
+            context=self.context,
427
+            title=title,
428
+            description=description,
429
+        )
430
+
431
+        def spec_yaml_view():
432
+            return self.context.get_response(
433
+                doc_yaml,
434
+                mimetype='text/x-yaml',
435
+                http_code=HTTPStatus.OK,
436
+            )
426 437
 
427 438
         # Prepare views html content
428 439
         doc_index_path = os.path.join(swaggerui_path, 'index.html')
@@ -447,3 +458,10 @@ class Hapic(object):
447 458
             http_method='GET',
448 459
             view_func=api_doc_view,
449 460
         )
461
+
462
+        # Add a doc yaml view
463
+        self.context.add_view(
464
+            route=os.path.join(route, 'spec.yml'),
465
+            http_method='GET',
466
+            view_func=spec_yaml_view,
467
+        )

+ 0 - 94
hapic/static/swaggerui/spec.yml 查看文件

@@ -1,94 +0,0 @@
1
-definitions:
2
-  DefaultErrorBuilder:
3
-    properties:
4
-      code:
5
-        type: string
6
-        x-nullable: true
7
-      details:
8
-        type: object
9
-      message:
10
-        type: string
11
-    required:
12
-    - message
13
-    type: object
14
-  HelloJsonSchema:
15
-    properties:
16
-      color:
17
-        minLength: 3
18
-        type: string
19
-    required:
20
-    - color
21
-    type: object
22
-  HelloResponseSchema:
23
-    properties:
24
-      color:
25
-        type: string
26
-      name:
27
-        type: string
28
-      sentence:
29
-        type: string
30
-    required:
31
-    - name
32
-    - sentence
33
-    type: object
34
-info:
35
-  description: ''
36
-  title: ''
37
-  version: 1.0.0
38
-parameters: {}
39
-paths:
40
-  /hello/{name}:
41
-    get:
42
-      description: "my endpoint hello\n        ---\n        get:\n            description:\
43
-        \ my description\n            parameters:\n                - in: \"path\"\n\
44
-        \                  description: \"hello\"\n                  name: \"name\"\
45
-        \n                  type: \"string\"\n            responses:\n           \
46
-        \     200:\n                    description: A pet to be returned\n      \
47
-        \              schema: HelloResponseSchema"
48
-      parameters:
49
-      - in: path
50
-        name: name
51
-        required: true
52
-        type: string
53
-      - in: query
54
-        name: alive
55
-        required: false
56
-        type: boolean
57
-      responses:
58
-        '200':
59
-          description: '200'
60
-          schema:
61
-            $ref: '#/definitions/HelloResponseSchema'
62
-        '400':
63
-          description: '400'
64
-          schema:
65
-            $ref: '#/definitions/DefaultErrorBuilder'
66
-    post:
67
-      parameters:
68
-      - in: body
69
-        name: body
70
-        schema:
71
-          $ref: '#/definitions/HelloJsonSchema'
72
-      - in: path
73
-        name: name
74
-        required: true
75
-        type: string
76
-      responses:
77
-        '200':
78
-          description: '200'
79
-          schema:
80
-            $ref: '#/definitions/HelloResponseSchema'
81
-  /hello3/{name}:
82
-    get:
83
-      parameters:
84
-      - in: path
85
-        name: name
86
-        required: true
87
-        type: string
88
-      responses:
89
-        '200':
90
-          description: '200'
91
-          schema:
92
-            $ref: '#/definitions/HelloResponseSchema'
93
-swagger: '2.0'
94
-tags: []