-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.py
More file actions
57 lines (39 loc) · 1.18 KB
/
Copy pathprotocol.py
File metadata and controls
57 lines (39 loc) · 1.18 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
51
52
53
54
55
56
57
import json
TYPE = 'type'
STATUS = 'status'
RUN = 'run'
GET_RESULT = 'get_result'
TASK_NAME = 'task_name'
DATA = 'data'
TASK_ID = 'task_id'
class Protocol:
def serialize(self):
return json.dumps(self.request).encode("utf-8")
class TaskReq(Protocol):
def __init__(self, task_name: str, data):
self.request = {TYPE: RUN,
TASK_NAME: task_name,
DATA: data}
class StatusReq(Protocol):
def __init__(self, task_id: int):
self.request = {TYPE: STATUS,
TASK_ID: task_id}
class ResultReq(Protocol):
def __init__(self, task_id: int):
self.request = {TYPE: GET_RESULT,
TASK_ID: task_id}
class TaskIdRes(Protocol):
def __init__(self, task_id: int):
self.request = {TASK_ID: task_id}
class DataRes(Protocol):
def __init__(self, data, status):
self.request = {STATUS: status,
DATA: data}
class StatusRes(Protocol):
def __init__(self, status):
self.request = {STATUS: status}
def deserialize(data):
try:
return json.loads(data)
except json.decoder.JSONDecodeError:
return {}