Beispiel #1
0
    def __call__(self, event):
        action = get_one_from_dict(event, 'action', str)

        mapper = {
            'mark_task_as_completed': {
                'function': self.mark_task_as_completed,
                'required_params': ['task_id']
            },
            'mark_task_as_failed': {
                'function': self.mark_task_as_failed,
                'required_params': ['task_id']
            }
        }

        if action in mapper:
            func = mapper[action]['function']
            required_params = mapper[action]['required_params']

            for req_param in required_params:
                if req_param not in event:
                    raise Exception(
                        f"Missing required parameter `{req_param}` in event for action `{action}`"
                    )

            func_kwargs = {k: event[k] for k in event if k in required_params}

            if 'stats' in event:
                if isinstance(event['stats'], str):
                    func_kwargs.update({'stats': json.loads(event['stats'])})
                else:
                    func_kwargs.update({'stats': event['stats']})

            if 'result' in event:
                if isinstance(event['result'], str):
                    func_kwargs.update({'result': json.loads(event['result'])})
                else:
                    func_kwargs.update({'result': event['result']})

            return func(**func_kwargs)
        else:
            raise Exception(f"Action `{action}` is not supported")
Beispiel #2
0
    def __call__(self, event):
        action = get_one_from_dict(event, 'action', str)

        mapper = {
            'mark_task_as_completed': {
                'function':        self.mark_task_as_completed,
                'required_params': ['task_id']
            }
        }

        if action in mapper:
            func = mapper[action]['function']
            required_params = mapper[action]['required_params']

            for req_param in required_params:
                if req_param not in event:
                    raise Exception(f"Missing required parameter `{req_param}` in event for action `{action}`")

            func_kwargs = {k: event[k] for k in event if k in required_params}
            return func(**func_kwargs)
        else:
            raise Exception(f"Action `{action}` is not supported")