Browse Source

Use callback for context and fix wrong class usage

Bastien Sevajol 6 years ago
parent
commit
83f2903db9
4 changed files with 32 additions and 17 deletions
  1. 10 4
      hapic/decorator.py
  2. 20 11
      hapic/hapic.py
  3. 1 1
      hapic/processor.py
  4. 1 1
      setup.py

+ 10 - 4
hapic/decorator.py View File

54
 class InputOutputControllerWrapper(ControllerWrapper):
54
 class InputOutputControllerWrapper(ControllerWrapper):
55
     def __init__(
55
     def __init__(
56
         self,
56
         self,
57
-        context: ContextInterface,
57
+        context: typing.Union[ContextInterface, typing.Callable[[], ContextInterface]],  # nopep8
58
         processor: ProcessorInterface,
58
         processor: ProcessorInterface,
59
         error_http_code: HTTPStatus=HTTPStatus.BAD_REQUEST,
59
         error_http_code: HTTPStatus=HTTPStatus.BAD_REQUEST,
60
         default_http_code: HTTPStatus=HTTPStatus.OK,
60
         default_http_code: HTTPStatus=HTTPStatus.OK,
61
     ) -> None:
61
     ) -> None:
62
-        self.context = context
62
+        self._context = context
63
         self.processor = processor
63
         self.processor = processor
64
         self.error_http_code = error_http_code
64
         self.error_http_code = error_http_code
65
         self.default_http_code = default_http_code
65
         self.default_http_code = default_http_code
66
 
66
 
67
+    @property
68
+    def context(self) -> ContextInterface:
69
+        if callable(self._context):
70
+            return self._context()
71
+        return self._context
72
+
67
 
73
 
68
 class InputControllerWrapper(InputOutputControllerWrapper):
74
 class InputControllerWrapper(InputOutputControllerWrapper):
69
     def before_wrapped_func(
75
     def before_wrapped_func(
140
 class OutputControllerWrapper(InputOutputControllerWrapper):
146
 class OutputControllerWrapper(InputOutputControllerWrapper):
141
     def __init__(
147
     def __init__(
142
         self,
148
         self,
143
-        context: ContextInterface,
149
+        context: typing.Union[ContextInterface, typing.Callable[[], ContextInterface]],  # nopep8
144
         processor: ProcessorInterface,
150
         processor: ProcessorInterface,
145
         error_http_code: HTTPStatus=HTTPStatus.INTERNAL_SERVER_ERROR,
151
         error_http_code: HTTPStatus=HTTPStatus.INTERNAL_SERVER_ERROR,
146
         default_http_code: HTTPStatus=HTTPStatus.OK,
152
         default_http_code: HTTPStatus=HTTPStatus.OK,
294
     def __init__(
300
     def __init__(
295
         self,
301
         self,
296
         handled_exception_class: typing.Type[Exception],
302
         handled_exception_class: typing.Type[Exception],
297
-        context: ContextInterface,
303
+        context: typing.Union[ContextInterface, typing.Callable[[], ContextInterface]],  # nopep8
298
         http_code: HTTPStatus=HTTPStatus.INTERNAL_SERVER_ERROR,
304
         http_code: HTTPStatus=HTTPStatus.INTERNAL_SERVER_ERROR,
299
     ) -> None:
305
     ) -> None:
300
         self.handled_exception_class = handled_exception_class
306
         self.handled_exception_class = handled_exception_class

+ 20 - 11
hapic/hapic.py View File

27
 from hapic.doc import DocGenerator
27
 from hapic.doc import DocGenerator
28
 from hapic.processor import ProcessorInterface
28
 from hapic.processor import ProcessorInterface
29
 from hapic.processor import MarshmallowInputProcessor
29
 from hapic.processor import MarshmallowInputProcessor
30
+from hapic.processor import MarshmallowOutputProcessor
30
 
31
 
31
 # TODO: Gérer les erreurs avec schema
32
 # TODO: Gérer les erreurs avec schema
32
 # TODO: Gérer les erreurs avec schema: pouvoir le spécialiser
33
 # TODO: Gérer les erreurs avec schema: pouvoir le spécialiser
50
         self._buffer = DecorationBuffer()
51
         self._buffer = DecorationBuffer()
51
         self._controllers = []  # type: typing.List[DecoratedController]
52
         self._controllers = []  # type: typing.List[DecoratedController]
52
         self._context = None
53
         self._context = None
54
+
55
+        # This local function will be pass to different components
56
+        # who will need context but declared (like with decorator)
57
+        # before context declaration
58
+        def context_getter():
59
+            return self._context
60
+
61
+        self._context_getter = context_getter
62
+
53
         # TODO: Permettre la surcharge des classes utilisés ci-dessous
63
         # TODO: Permettre la surcharge des classes utilisés ci-dessous
54
 
64
 
55
     def with_api_doc(self):
65
     def with_api_doc(self):
86
         error_http_code: HTTPStatus = HTTPStatus.BAD_REQUEST,
96
         error_http_code: HTTPStatus = HTTPStatus.BAD_REQUEST,
87
         default_http_code: HTTPStatus = HTTPStatus.OK,
97
         default_http_code: HTTPStatus = HTTPStatus.OK,
88
     ) -> typing.Callable[[typing.Callable[..., typing.Any]], typing.Any]:
98
     ) -> typing.Callable[[typing.Callable[..., typing.Any]], typing.Any]:
89
-        processor = processor or MarshmallowInputProcessor()
99
+        processor = processor or MarshmallowOutputProcessor()
90
         processor.schema = schema
100
         processor.schema = schema
91
-        context = context or self._context
92
-
101
+        context = context or self._context_getter
93
         decoration = OutputBodyControllerWrapper(
102
         decoration = OutputBodyControllerWrapper(
94
             context=context,
103
             context=context,
95
             processor=processor,
104
             processor=processor,
110
         error_http_code: HTTPStatus = HTTPStatus.BAD_REQUEST,
119
         error_http_code: HTTPStatus = HTTPStatus.BAD_REQUEST,
111
         default_http_code: HTTPStatus = HTTPStatus.OK,
120
         default_http_code: HTTPStatus = HTTPStatus.OK,
112
     ) -> typing.Callable[[typing.Callable[..., typing.Any]], typing.Any]:
121
     ) -> typing.Callable[[typing.Callable[..., typing.Any]], typing.Any]:
113
-        processor = processor or MarshmallowInputProcessor()
122
+        processor = processor or MarshmallowOutputProcessor()
114
         processor.schema = schema
123
         processor.schema = schema
115
-        context = context or self._context
124
+        context = context or self._context_getter
116
 
125
 
117
         decoration = OutputHeadersControllerWrapper(
126
         decoration = OutputHeadersControllerWrapper(
118
             context=context,
127
             context=context,
136
     ) -> typing.Callable[[typing.Callable[..., typing.Any]], typing.Any]:
145
     ) -> typing.Callable[[typing.Callable[..., typing.Any]], typing.Any]:
137
         processor = processor or MarshmallowInputProcessor()
146
         processor = processor or MarshmallowInputProcessor()
138
         processor.schema = schema
147
         processor.schema = schema
139
-        context = context or self._context
148
+        context = context or self._context_getter
140
 
149
 
141
         decoration = InputHeadersControllerWrapper(
150
         decoration = InputHeadersControllerWrapper(
142
             context=context,
151
             context=context,
160
     ) -> typing.Callable[[typing.Callable[..., typing.Any]], typing.Any]:
169
     ) -> typing.Callable[[typing.Callable[..., typing.Any]], typing.Any]:
161
         processor = processor or MarshmallowInputProcessor()
170
         processor = processor or MarshmallowInputProcessor()
162
         processor.schema = schema
171
         processor.schema = schema
163
-        context = context or self._context
172
+        context = context or self._context_getter
164
 
173
 
165
         decoration = InputPathControllerWrapper(
174
         decoration = InputPathControllerWrapper(
166
             context=context,
175
             context=context,
184
     ) -> typing.Callable[[typing.Callable[..., typing.Any]], typing.Any]:
193
     ) -> typing.Callable[[typing.Callable[..., typing.Any]], typing.Any]:
185
         processor = processor or MarshmallowInputProcessor()
194
         processor = processor or MarshmallowInputProcessor()
186
         processor.schema = schema
195
         processor.schema = schema
187
-        context = context or self._context
196
+        context = context or self._context_getter
188
 
197
 
189
         decoration = InputQueryControllerWrapper(
198
         decoration = InputQueryControllerWrapper(
190
             context=context,
199
             context=context,
208
     ) -> typing.Callable[[typing.Callable[..., typing.Any]], typing.Any]:
217
     ) -> typing.Callable[[typing.Callable[..., typing.Any]], typing.Any]:
209
         processor = processor or MarshmallowInputProcessor()
218
         processor = processor or MarshmallowInputProcessor()
210
         processor.schema = schema
219
         processor.schema = schema
211
-        context = context or self._context
220
+        context = context or self._context_getter
212
 
221
 
213
         decoration = InputBodyControllerWrapper(
222
         decoration = InputBodyControllerWrapper(
214
             context=context,
223
             context=context,
232
     ) -> typing.Callable[[typing.Callable[..., typing.Any]], typing.Any]:
241
     ) -> typing.Callable[[typing.Callable[..., typing.Any]], typing.Any]:
233
         processor = processor or MarshmallowInputProcessor()
242
         processor = processor or MarshmallowInputProcessor()
234
         processor.schema = schema
243
         processor.schema = schema
235
-        context = context or self._context
244
+        context = context or self._context_getter
236
 
245
 
237
         decoration = InputBodyControllerWrapper(
246
         decoration = InputBodyControllerWrapper(
238
             context=context,
247
             context=context,
252
         http_code: HTTPStatus = HTTPStatus.INTERNAL_SERVER_ERROR,
261
         http_code: HTTPStatus = HTTPStatus.INTERNAL_SERVER_ERROR,
253
         context: ContextInterface = None,
262
         context: ContextInterface = None,
254
     ) -> typing.Callable[[typing.Callable[..., typing.Any]], typing.Any]:
263
     ) -> typing.Callable[[typing.Callable[..., typing.Any]], typing.Any]:
255
-        context = context or self._context
264
+        context = context or self._context_getter
256
 
265
 
257
         decoration = ExceptionHandlerControllerWrapper(
266
         decoration = ExceptionHandlerControllerWrapper(
258
             handled_exception_class,
267
             handled_exception_class,

+ 1 - 1
hapic/processor.py View File

76
         )
76
         )
77
 
77
 
78
 
78
 
79
-class MarshmallowInputProcessor(OutputProcessor):
79
+class MarshmallowInputProcessor(InputProcessor):
80
     def process(self, data: dict):
80
     def process(self, data: dict):
81
         unmarshall = self.schema.load(data)
81
         unmarshall = self.schema.load(data)
82
         if unmarshall.errors:
82
         if unmarshall.errors:

+ 1 - 1
setup.py View File

23
     # Versions should comply with PEP440.  For a discussion on single-sourcing
23
     # Versions should comply with PEP440.  For a discussion on single-sourcing
24
     # the version across setup.py and the project code, see
24
     # the version across setup.py and the project code, see
25
     # https://packaging.python.org/en/latest/single_source_version.html
25
     # https://packaging.python.org/en/latest/single_source_version.html
26
-    version='0.0.2.3',
26
+    version='0.0.3',
27
 
27
 
28
     description='HTTP api input/output manager',
28
     description='HTTP api input/output manager',
29
     # long_description=long_description,
29
     # long_description=long_description,