瀏覽代碼

Merge pull request #38 from inkhey/fix/pyramid-swagger-ui

Damien Accorsi 6 年之前
父節點
當前提交
b190b87151
沒有帳戶連結到提交者的電子郵件
共有 2 個文件被更改,包括 53 次插入8 次删除
  1. 28 0
      hapic/ext/pyramid/context.py
  2. 25 8
      hapic/hapic.py

+ 28 - 0
hapic/ext/pyramid/context.py 查看文件

147
 
147
 
148
     def by_pass_output_wrapping(self, response: typing.Any) -> bool:
148
     def by_pass_output_wrapping(self, response: typing.Any) -> bool:
149
         return False
149
         return False
150
+
151
+    def add_view(
152
+        self,
153
+        route: str,
154
+        http_method: str,
155
+        view_func: typing.Callable[..., typing.Any],
156
+    ) -> None:
157
+
158
+        self.configurator.add_route(
159
+            name=route,
160
+            path=route,
161
+            request_method=http_method
162
+        )
163
+
164
+        self.configurator.add_view(
165
+            view_func,
166
+            route_name=route,
167
+        )
168
+
169
+    def serve_directory(
170
+        self,
171
+        route_prefix: str,
172
+        directory_path: str,
173
+    ) -> None:
174
+        self.configurator.add_static_view(
175
+            name=route_prefix,
176
+            path=directory_path,
177
+        )

+ 25 - 8
hapic/hapic.py 查看文件

409
         if not route.endswith('/'):
409
         if not route.endswith('/'):
410
             route = '{}/'.format(route)
410
             route = '{}/'.format(route)
411
 
411
 
412
-        # Add swagger directory as served static dir
413
         swaggerui_path = os.path.join(
412
         swaggerui_path = os.path.join(
414
             os.path.dirname(os.path.abspath(__file__)),
413
             os.path.dirname(os.path.abspath(__file__)),
415
             'static',
414
             'static',
416
             'swaggerui',
415
             'swaggerui',
417
         )
416
         )
418
-        self.context.serve_directory(
419
-            route,
420
-            swaggerui_path,
421
-        )
422
 
417
 
423
         # Documentation file view
418
         # Documentation file view
424
         doc_yaml = self.doc_generator.get_doc_yaml(
419
         doc_yaml = self.doc_generator.get_doc_yaml(
428
             description=description,
423
             description=description,
429
         )
424
         )
430
 
425
 
431
-        def spec_yaml_view():
426
+        def spec_yaml_view(*args, **kwargs):
427
+            """
428
+            Method to return swagger generated yaml spec file.
429
+
430
+            This method will be call as a framework view, like those,
431
+            it need to handle the default arguments of a framework view.
432
+            As frameworks have different arguments patterns, we should
433
+            allow any arguments patterns (args, kwargs).
434
+            """
432
             return self.context.get_response(
435
             return self.context.get_response(
433
                 doc_yaml,
436
                 doc_yaml,
434
                 mimetype='text/x-yaml',
437
                 mimetype='text/x-yaml',
445
         )
448
         )
446
 
449
 
447
         # Declare the swaggerui view
450
         # Declare the swaggerui view
448
-        def api_doc_view():
451
+        def api_doc_view(*args, **kwargs):
452
+            """
453
+            Method to return html index view of swagger ui.
454
+
455
+            This method will be call as a framework view, like those,
456
+            it need to handle the default arguments of a framework view.
457
+            As frameworks have different arguments patterns, we should
458
+            allow any arguments patterns (args, kwargs).
459
+            """
449
             return self.context.get_response(
460
             return self.context.get_response(
450
                 doc_page_content,
461
                 doc_page_content,
451
                 http_code=HTTPStatus.OK,
462
                 http_code=HTTPStatus.OK,
452
                 mimetype='text/html',
463
                 mimetype='text/html',
453
             )
464
             )
454
 
465
 
455
-        # Add a view to generate the html index page of swaggerui
466
+        # Add a view to generate the html index page of swagger-ui
456
         self.context.add_view(
467
         self.context.add_view(
457
             route=route,
468
             route=route,
458
             http_method='GET',
469
             http_method='GET',
465
             http_method='GET',
476
             http_method='GET',
466
             view_func=spec_yaml_view,
477
             view_func=spec_yaml_view,
467
         )
478
         )
479
+
480
+        # Add swagger directory as served static dir
481
+        self.context.serve_directory(
482
+            route,
483
+            swaggerui_path,
484
+        )