|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""rest_grouping.py |
| 3 | +
|
| 4 | + This module implements ``RestTaskLoader`` class, which is used to load a group of RESTful services (call tasks) from a JSON payload into ``TaskContainer``. |
| 5 | +
|
| 6 | + This module was originally shipped as an example code from https://github.com/DataBooster/PyWebApi, licensed under the MIT license. |
| 7 | + Anyone who obtains a copy of this code is welcome to modify it for any purpose, and holds all rights to the modified part only. |
| 8 | + The above license notice and permission notice shall be included in all copies or substantial portions of the Software. |
| 9 | +""" |
| 10 | +from typing import List, Tuple, Dict, Any |
| 11 | +from concurrent.futures import ThreadPoolExecutor |
| 12 | +from task_grouping import TaskContainer, ITaskLoader |
| 13 | +from simple_rest_call import request_json |
| 14 | + |
| 15 | + |
| 16 | +_reserved_key_parallel_group : str = '[###]' |
| 17 | +_reserved_key_serial_group : str = '[+++]' |
| 18 | +_reserved_key_rest_url : str = '(://)' |
| 19 | +_reserved_key_payload : str = '(...)' |
| 20 | +_reserved_key_payload_with_pipe : str = '(.|.)' |
| 21 | + |
| 22 | + |
| 23 | +def _pipeargs_merge_fn(kw_args:Dict[str, Any], pipe_args:Dict[str, Any]) -> Dict[str, Any]: |
| 24 | + if pipe_args and isinstance(pipe_args, dict): |
| 25 | + merged_args = kw_args.copy() if kw_args else {} |
| 26 | + payload = kw_args.get('data', {}) |
| 27 | + if payload is None: |
| 28 | + payload = {} |
| 29 | + |
| 30 | + if isinstance(payload, dict): |
| 31 | + merged_args['data'] = payload.copy().update(pipe_args) |
| 32 | + |
| 33 | + return merged_args |
| 34 | + else: |
| 35 | + return kw_args |
| 36 | + |
| 37 | + |
| 38 | +class RestTaskLoader(ITaskLoader): |
| 39 | + """This class is used to load a group of RESTful services (call tasks) from a JSON payload into ``TaskContainer``""" |
| 40 | + def __init__(self, thread_pool:ThreadPoolExecutor): |
| 41 | + self.thread_pool = thread_pool |
| 42 | + self.func = request_json |
| 43 | + self.merge_fn = _pipeargs_merge_fn |
| 44 | + self.timeout = None |
| 45 | + |
| 46 | + |
| 47 | + def create_base_container(self) -> TaskContainer: |
| 48 | + return TaskContainer(self.func, self.merge_fn, self.thread_pool, **{'timeout':self.timeout}) |
| 49 | + |
| 50 | + |
| 51 | + def extract_single_task(self, task_node:Dict[str, Any]) -> Tuple[tuple, Dict[str, Any], bool]: |
| 52 | + url = task_node.get(_reserved_key_rest_url) |
| 53 | + if url: |
| 54 | + data = task_node.get(_reserved_key_payload) |
| 55 | + if data is None: |
| 56 | + data = {} |
| 57 | + |
| 58 | + data_ = task_node.get(_reserved_key_payload_with_pipe) |
| 59 | + if data_ is not None or _reserved_key_payload_with_pipe in task_node: |
| 60 | + if data_: |
| 61 | + data.update(data_) |
| 62 | + with_pipe = True |
| 63 | + else: |
| 64 | + with_pipe = False |
| 65 | + |
| 66 | + return ((), {'url': url, 'data': data}, with_pipe) |
| 67 | + else: |
| 68 | + return None |
| 69 | + |
| 70 | + |
| 71 | + def extract_serial_group(self, task_node:Dict[str, Any]) -> List[Dict[str, Any]]: |
| 72 | + return task_node.get(_reserved_key_serial_group) |
| 73 | + |
| 74 | + |
| 75 | + def extract_parallel_group(self, task_node:Dict[str, Any]) -> List[Dict[str, Any]]: |
| 76 | + return task_node.get(_reserved_key_parallel_group) |
0 commit comments