|
@@ -73,14 +73,15 @@ class ControllerWrapper(object):
|
73
|
73
|
self,
|
74
|
74
|
func: 'typing.Callable[..., typing.Any]',
|
75
|
75
|
) -> 'typing.Callable[..., typing.Any]':
|
76
|
|
- async def wrapper(*args, **kwargs) -> typing.Any:
|
|
76
|
+ # async def wrapper(*args, **kwargs) -> typing.Any:
|
|
77
|
+ def wrapper(*args, **kwargs) -> typing.Any:
|
77
|
78
|
# Note: Design of before_wrapped_func can be to update kwargs
|
78
|
79
|
# by reference here
|
79
|
|
- replacement_response = await self.before_wrapped_func(args, kwargs)
|
|
80
|
+ replacement_response = self.before_wrapped_func(args, kwargs)
|
80
|
81
|
if replacement_response:
|
81
|
82
|
return replacement_response
|
82
|
83
|
|
83
|
|
- response = await self._execute_wrapped_function(func, args, kwargs)
|
|
84
|
+ response = self._execute_wrapped_function(func, args, kwargs)
|
84
|
85
|
new_response = self.after_wrapped_function(response)
|
85
|
86
|
return new_response
|
86
|
87
|
return functools.update_wrapper(wrapper, func)
|
|
@@ -191,9 +192,25 @@ class InputControllerWrapper(InputOutputControllerWrapper):
|
191
|
192
|
|
192
|
193
|
|
193
|
194
|
# TODO BS 2018-07-23: This class is an async version of InputControllerWrapper
|
194
|
|
-# to permit async compatibility. Please re-think about code refact
|
195
|
|
-# TAG: REFACT_ASYNC
|
|
195
|
+# (and ControllerWrapper.get_wrapper rewrite) to permit async compatibility.
|
|
196
|
+# Please re-think about code refact. TAG: REFACT_ASYNC
|
196
|
197
|
class AsyncInputControllerWrapper(InputControllerWrapper):
|
|
198
|
+ def get_wrapper(
|
|
199
|
+ self,
|
|
200
|
+ func: 'typing.Callable[..., typing.Any]',
|
|
201
|
+ ) -> 'typing.Callable[..., typing.Any]':
|
|
202
|
+ async def wrapper(*args, **kwargs) -> typing.Any:
|
|
203
|
+ # Note: Design of before_wrapped_func can be to update kwargs
|
|
204
|
+ # by reference here
|
|
205
|
+ replacement_response = await self.before_wrapped_func(args, kwargs)
|
|
206
|
+ if replacement_response:
|
|
207
|
+ return replacement_response
|
|
208
|
+
|
|
209
|
+ response = await self._execute_wrapped_function(func, args, kwargs)
|
|
210
|
+ new_response = self.after_wrapped_function(response)
|
|
211
|
+ return new_response
|
|
212
|
+ return functools.update_wrapper(wrapper, func)
|
|
213
|
+
|
197
|
214
|
async def before_wrapped_func(
|
198
|
215
|
self,
|
199
|
216
|
func_args: typing.Tuple[typing.Any, ...],
|
|
@@ -203,27 +220,25 @@ class AsyncInputControllerWrapper(InputControllerWrapper):
|
203
|
220
|
# hapic_data is given though decorators
|
204
|
221
|
# Important note here: func_kwargs is update by reference !
|
205
|
222
|
hapic_data = self.ensure_hapic_data(func_kwargs)
|
206
|
|
- request_parameters = await self.get_request_parameters(
|
|
223
|
+ request_parameters = self.get_request_parameters(
|
207
|
224
|
func_args,
|
208
|
225
|
func_kwargs,
|
209
|
226
|
)
|
210
|
227
|
|
211
|
228
|
try:
|
212
|
|
- processed_data = self.get_processed_data(request_parameters)
|
|
229
|
+ processed_data = await self.get_processed_data(request_parameters)
|
213
|
230
|
self.update_hapic_data(hapic_data, processed_data)
|
214
|
231
|
except ProcessException:
|
215
|
232
|
error_response = self.get_error_response(request_parameters)
|
216
|
233
|
return error_response
|
217
|
234
|
|
218
|
|
- async def get_request_parameters(
|
|
235
|
+ async def get_processed_data(
|
219
|
236
|
self,
|
220
|
|
- func_args: typing.Tuple[typing.Any, ...],
|
221
|
|
- func_kwargs: typing.Dict[str, typing.Any],
|
222
|
|
- ) -> RequestParameters:
|
223
|
|
- return await self.context.get_request_parameters(
|
224
|
|
- *func_args,
|
225
|
|
- **func_kwargs
|
226
|
|
- )
|
|
237
|
+ request_parameters: RequestParameters,
|
|
238
|
+ ) -> typing.Any:
|
|
239
|
+ parameters_data = await self.get_parameters_data(request_parameters)
|
|
240
|
+ processed_data = self.processor.process(parameters_data)
|
|
241
|
+ return processed_data
|
227
|
242
|
|
228
|
243
|
|
229
|
244
|
class OutputControllerWrapper(InputOutputControllerWrapper):
|
|
@@ -298,6 +313,25 @@ class OutputBodyControllerWrapper(OutputControllerWrapper):
|
298
|
313
|
pass
|
299
|
314
|
|
300
|
315
|
|
|
316
|
+class AsyncOutputBodyControllerWrapper(OutputControllerWrapper):
|
|
317
|
+ def get_wrapper(
|
|
318
|
+ self,
|
|
319
|
+ func: 'typing.Callable[..., typing.Any]',
|
|
320
|
+ ) -> 'typing.Callable[..., typing.Any]':
|
|
321
|
+ # async def wrapper(*args, **kwargs) -> typing.Any:
|
|
322
|
+ async def wrapper(*args, **kwargs) -> typing.Any:
|
|
323
|
+ # Note: Design of before_wrapped_func can be to update kwargs
|
|
324
|
+ # by reference here
|
|
325
|
+ replacement_response = self.before_wrapped_func(args, kwargs)
|
|
326
|
+ if replacement_response:
|
|
327
|
+ return replacement_response
|
|
328
|
+
|
|
329
|
+ response = await self._execute_wrapped_function(func, args, kwargs)
|
|
330
|
+ new_response = self.after_wrapped_function(response)
|
|
331
|
+ return new_response
|
|
332
|
+ return functools.update_wrapper(wrapper, func)
|
|
333
|
+
|
|
334
|
+
|
301
|
335
|
class OutputHeadersControllerWrapper(OutputControllerWrapper):
|
302
|
336
|
pass
|
303
|
337
|
|
|
@@ -323,20 +357,6 @@ class InputPathControllerWrapper(InputControllerWrapper):
|
323
|
357
|
return request_parameters.path_parameters
|
324
|
358
|
|
325
|
359
|
|
326
|
|
-# TODO BS 2018-07-23: This class is an copy-patse of InputPathControllerWrapper
|
327
|
|
-# to permit async compatibility. Please re-think about code refact
|
328
|
|
-# TAG: REFACT_ASYNC
|
329
|
|
-class AsyncInputPathControllerWrapper(AsyncInputControllerWrapper):
|
330
|
|
- def update_hapic_data(
|
331
|
|
- self, hapic_data: HapicData,
|
332
|
|
- processed_data: typing.Any,
|
333
|
|
- ) -> None:
|
334
|
|
- hapic_data.path = processed_data
|
335
|
|
-
|
336
|
|
- def get_parameters_data(self, request_parameters: RequestParameters) -> dict: # nopep8
|
337
|
|
- return request_parameters.path_parameters
|
338
|
|
-
|
339
|
|
-
|
340
|
360
|
class InputQueryControllerWrapper(InputControllerWrapper):
|
341
|
361
|
def __init__(
|
342
|
362
|
self,
|
|
@@ -394,6 +414,20 @@ class InputBodyControllerWrapper(InputControllerWrapper):
|
394
|
414
|
return request_parameters.body_parameters
|
395
|
415
|
|
396
|
416
|
|
|
417
|
+# TODO BS 2018-07-23: This class is an async version of InputControllerWrapper
|
|
418
|
+# to permit async compatibility. Please re-think about code refact
|
|
419
|
+# TAG: REFACT_ASYNC
|
|
420
|
+class AsyncInputBodyControllerWrapper(AsyncInputControllerWrapper):
|
|
421
|
+ def update_hapic_data(
|
|
422
|
+ self, hapic_data: HapicData,
|
|
423
|
+ processed_data: typing.Any,
|
|
424
|
+ ) -> None:
|
|
425
|
+ hapic_data.body = processed_data
|
|
426
|
+
|
|
427
|
+ async def get_parameters_data(self, request_parameters: RequestParameters) -> dict: # nopep8
|
|
428
|
+ return await request_parameters.body_parameters
|
|
429
|
+
|
|
430
|
+
|
397
|
431
|
class InputHeadersControllerWrapper(InputControllerWrapper):
|
398
|
432
|
def update_hapic_data(
|
399
|
433
|
self, hapic_data: HapicData,
|