-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexternal_agent_dispatch.py
More file actions
50 lines (44 loc) · 1.72 KB
/
Copy pathexternal_agent_dispatch.py
File metadata and controls
50 lines (44 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class ExternalAgentCommandDispatcher:
def __init__(
self,
*,
command_auth_handlers,
authenticated_handlers,
validate_token,
build_error,
invalid_data_error_code,
action_not_allowed_error_code,
):
self.command_auth_handlers = dict(command_auth_handlers)
self.authenticated_handlers = dict(authenticated_handlers)
self.validate_token = validate_token
self.build_error = build_error
self.invalid_data_error_code = invalid_data_error_code
self.action_not_allowed_error_code = action_not_allowed_error_code
def normalize_op(self, command):
if not isinstance(command, dict):
return None, self.invalid_data_error_code
op = command.get('op')
if not isinstance(op, str):
return None, self.invalid_data_error_code
op = op.strip().lower()
if not op:
return None, self.invalid_data_error_code
return op, None
def dispatch(self, command):
op, error_code = self.normalize_op(command)
if error_code:
return self.build_error(error_code)
command_auth_handler = self.command_auth_handlers.get(op)
if command_auth_handler:
return command_auth_handler(op, command)
record, state, terminal_id, error_code = self.validate_token(command)
if error_code:
return self.build_error(error_code, terminal_id=terminal_id)
handler = self.authenticated_handlers.get(op)
if handler:
return handler(op, command, record, state, terminal_id)
return self.build_error(
self.action_not_allowed_error_code,
terminal_id=terminal_id,
)