Browse Source

Add support of swagger ui view for pyramid

Guénaël Muller 6 years ago
parent
commit
747792acaf
2 changed files with 36 additions and 7 deletions
  1. 28 0
      hapic/ext/pyramid/context.py
  2. 8 7
      hapic/hapic.py

+ 28 - 0
hapic/ext/pyramid/context.py View File

@@ -147,3 +147,31 @@ class PyramidContext(BaseContext):
147 147
 
148 148
     def by_pass_output_wrapping(self, response: typing.Any) -> bool:
149 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='GET'
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
+        )

+ 8 - 7
hapic/hapic.py View File

@@ -409,16 +409,11 @@ class Hapic(object):
409 409
         if not route.endswith('/'):
410 410
             route = '{}/'.format(route)
411 411
 
412
-        # Add swagger directory as served static dir
413 412
         swaggerui_path = os.path.join(
414 413
             os.path.dirname(os.path.abspath(__file__)),
415 414
             'static',
416 415
             'swaggerui',
417 416
         )
418
-        self.context.serve_directory(
419
-            route,
420
-            swaggerui_path,
421
-        )
422 417
 
423 418
         # Documentation file view
424 419
         doc_yaml = self.doc_generator.get_doc_yaml(
@@ -428,7 +423,7 @@ class Hapic(object):
428 423
             description=description,
429 424
         )
430 425
 
431
-        def spec_yaml_view():
426
+        def spec_yaml_view( *args, **kwargs):
432 427
             return self.context.get_response(
433 428
                 doc_yaml,
434 429
                 mimetype='text/x-yaml',
@@ -445,7 +440,7 @@ class Hapic(object):
445 440
         )
446 441
 
447 442
         # Declare the swaggerui view
448
-        def api_doc_view():
443
+        def api_doc_view(*args, **kwargs):
449 444
             return self.context.get_response(
450 445
                 doc_page_content,
451 446
                 http_code=HTTPStatus.OK,
@@ -465,3 +460,9 @@ class Hapic(object):
465 460
             http_method='GET',
466 461
             view_func=spec_yaml_view,
467 462
         )
463
+
464
+        # Add swagger directory as served static dir
465
+        self.context.serve_directory(
466
+            route,
467
+            swaggerui_path,
468
+        )