Преглед на файлове

Add support of swagger ui view for pyramid

Guénaël Muller преди 6 години
родител
ревизия
747792acaf
променени са 2 файла, в които са добавени 36 реда и са изтрити 7 реда
  1. 28 0
      hapic/ext/pyramid/context.py
  2. 8 7
      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='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 Целия файл

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):
432
             return self.context.get_response(
427
             return self.context.get_response(
433
                 doc_yaml,
428
                 doc_yaml,
434
                 mimetype='text/x-yaml',
429
                 mimetype='text/x-yaml',
445
         )
440
         )
446
 
441
 
447
         # Declare the swaggerui view
442
         # Declare the swaggerui view
448
-        def api_doc_view():
443
+        def api_doc_view(*args, **kwargs):
449
             return self.context.get_response(
444
             return self.context.get_response(
450
                 doc_page_content,
445
                 doc_page_content,
451
                 http_code=HTTPStatus.OK,
446
                 http_code=HTTPStatus.OK,
465
             http_method='GET',
460
             http_method='GET',
466
             view_func=spec_yaml_view,
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
+        )