Browse Source

add doc and todos

Bastien Sevajol 6 years ago
parent
commit
9c6121e60d
6 changed files with 49 additions and 7 deletions
  1. 6 2
      hapic/context.py
  2. 5 0
      hapic/decorator.py
  3. 10 0
      hapic/doc.py
  4. 24 2
      hapic/hapic.py
  5. 4 0
      tests/func/test_doc.py
  6. 0 3
      tests/func/test_marshmallow_decoration.py

+ 6 - 2
hapic/context.py View File

@@ -30,6 +30,7 @@ class ContextInterface(object):
30 30
 
31 31
     def get_response(
32 32
         self,
33
+        # TODO BS 20171228: rename into response_content
33 34
         response: dict,
34 35
         http_code: int,
35 36
     ) -> typing.Any:
@@ -48,18 +49,21 @@ class ContextInterface(object):
48 49
     ) -> RouteRepresentation:
49 50
         raise NotImplementedError()
50 51
 
52
+    # TODO BS 20171228: rename into openapi !
51 53
     def get_swagger_path(self, contextualised_rule: str) -> str:
52 54
         """
53 55
         Return OpenAPI path with context path
56
+        TODO BS 20171228: Give example
54 57
         :param contextualised_rule: path of original context
55 58
         :return: OpenAPI path
56 59
         """
57 60
         raise NotImplementedError()
58 61
 
62
+    # TODO BS 20171228: rename into "bypass"
59 63
     def by_pass_output_wrapping(self, response: typing.Any) -> bool:
60 64
         """
61
-        Return True if the controller response is in final state: we do not
62
-        have to apply output wrapper on it.
65
+        Return True if the controller response is the final response object:
66
+        we do not have to apply any processing on it.
63 67
         :param response: the original response of controller
64 68
         :return:
65 69
         """

+ 5 - 0
hapic/decorator.py View File

@@ -377,6 +377,11 @@ class InputFilesControllerWrapper(InputControllerWrapper):
377 377
 
378 378
 
379 379
 class ExceptionHandlerControllerWrapper(ControllerWrapper):
380
+    """
381
+    This wrapper is used to wrap a controller and catch given exception if
382
+    raised. An error will be generated in collaboration with context and
383
+    returned.
384
+    """
380 385
     def __init__(
381 386
         self,
382 387
         handled_exception_class: typing.Type[Exception],

+ 10 - 0
hapic/doc.py View File

@@ -111,6 +111,16 @@ class DocGenerator(object):
111 111
         title: str='',
112 112
         description: str='',
113 113
     ) -> dict:
114
+        """
115
+        Generate an OpenApi 2.0 documentation. Th given context will be used
116
+        to found controllers matching with given DecoratedController.
117
+        :param controllers: List of DecoratedController to match with context
118
+        controllers
119
+        :param context: a context instance
120
+        :param title: The generated doc title
121
+        :param description: The generated doc description
122
+        :return: a apispec documentation dict
123
+        """
114 124
         spec = APISpec(
115 125
             title=title,
116 126
             info=dict(description=description),

+ 24 - 2
hapic/hapic.py View File

@@ -87,6 +87,21 @@ class Hapic(object):
87 87
         self._context = None
88 88
 
89 89
     def with_api_doc(self, tags: typing.List['str']=None):
90
+        """
91
+        Permit to generate doc about a controller. Use as a decorator:
92
+
93
+        ```
94
+        @hapic.with_api_doc()
95
+        def my_controller(self):
96
+            # ...
97
+        ```
98
+
99
+        What it do: Register this controller with all previous given
100
+        information like `@hapic.input_path(...)` etc.
101
+
102
+        :param tags: list of string tags (OpenApi)
103
+        :return: The decorator
104
+        """
90 105
         # FIXME BS 20171228: Documenter sur ce que ça fait vraiment (tester:
91 106
         # on peut l'enlever si on veut pas generer la doc ?)
92 107
         tags = tags or []  # FDV
@@ -350,9 +365,16 @@ class Hapic(object):
350 365
             return decoration.get_wrapper(func)
351 366
         return decorator
352 367
 
353
-    def generate_doc(self, title: str='', description: str=''):
368
+    def generate_doc(self, title: str='', description: str='') -> dict:
369
+        """
370
+        See hapic.doc.DocGenerator#get_doc docstring
371
+        :param title: Title of generated doc
372
+        :param description: Description of generated doc
373
+        :return: dict containing apispec doc
374
+        """
354 375
         return self.doc_generator.get_doc(
355 376
             self._controllers,
356 377
             self.context,
357 378
             title=title,
358
-            description=description)
379
+            description=description,
380
+        )

+ 4 - 0
tests/func/test_doc.py View File

@@ -10,6 +10,7 @@ from tests.base import MyContext
10 10
 class TestDocGeneration(Base):
11 11
     def test_func__input_files_doc__ok__one_file(self):
12 12
         hapic = Hapic()
13
+        # TODO BS 20171113: Make this test non-bottle
13 14
         app = bottle.Bottle()
14 15
         hapic.set_context(MyContext(app=app))
15 16
 
@@ -39,6 +40,7 @@ class TestDocGeneration(Base):
39 40
 
40 41
     def test_func__input_files_doc__ok__two_file(self):
41 42
         hapic = Hapic()
43
+        # TODO BS 20171113: Make this test non-bottle
42 44
         app = bottle.Bottle()
43 45
         hapic.set_context(MyContext(app=app))
44 46
 
@@ -75,6 +77,7 @@ class TestDocGeneration(Base):
75 77
 
76 78
     def test_func__output_file_doc__ok__nominal_case(self):
77 79
         hapic = Hapic()
80
+        # TODO BS 20171113: Make this test non-bottle
78 81
         app = bottle.Bottle()
79 82
         hapic.set_context(MyContext(app=app))
80 83
 
@@ -94,6 +97,7 @@ class TestDocGeneration(Base):
94 97
 
95 98
     def test_func__input_files_doc__ok__one_file_and_text(self):
96 99
         hapic = Hapic()
100
+        # TODO BS 20171113: Make this test non-bottle
97 101
         app = bottle.Bottle()
98 102
         hapic.set_context(MyContext(app=app))
99 103
 

+ 0 - 3
tests/func/test_marshmallow_decoration.py View File

@@ -24,7 +24,6 @@ class TestMarshmallowDecoration(Base):
24 24
         class MySchema(marshmallow.Schema):
25 25
             file_abc = marshmallow.fields.Raw(required=True)
26 26
 
27
-        @hapic.with_api_doc()
28 27
         @hapic.input_files(MySchema())
29 28
         def my_controller(hapic_data=None):
30 29
             assert hapic_data
@@ -46,7 +45,6 @@ class TestMarshmallowDecoration(Base):
46 45
         class MySchema(marshmallow.Schema):
47 46
             file_abc = marshmallow.fields.Raw(required=True)
48 47
 
49
-        @hapic.with_api_doc()
50 48
         @hapic.input_files(MySchema())
51 49
         def my_controller(hapic_data=None):
52 50
             assert hapic_data
@@ -72,7 +70,6 @@ class TestMarshmallowDecoration(Base):
72 70
         class MySchema(marshmallow.Schema):
73 71
             file_abc = marshmallow.fields.Raw(required=True)
74 72
 
75
-        @hapic.with_api_doc()
76 73
         @hapic.input_files(MySchema())
77 74
         def my_controller(hapic_data=None):
78 75
             assert hapic_data