File size: 1,453 Bytes
6f8c8ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from dataclasses import dataclass
from typing import Union

"""
Child bound messages.
"""


@dataclass
class RequestMessage:
    # Request ID. Must be unique for each Module process.
    request_id: int
    # Name of the Module method to call, not the REST method name.
    method_name: str
    # aiohttp.web.Request is explicitly not serializable, so we use bytes instead.
    # TODO(ryw): add headers if needed
    body: bytes


# Now it only contains RequestMessage. If later we need to add more messages, use Union.
ChildBoundMessage = RequestMessage

"""
Parent bound messages.
"""


@dataclass
class UnaryResponseMessage:
    request_id: int
    # aiohttp.web.Response is explicitly not serializable, so we use bytes instead.
    status: int
    # TODO(ryw): add headers if needed
    # headers: Dict[str, str]
    body: bytes


@dataclass
class StreamResponseStartMessage:
    # TODO(ryw): if needed, add header: Dict[str, str]
    request_id: int
    body: bytes


@dataclass
class StreamResponseDataMessage:
    request_id: int
    body: bytes


@dataclass
class StreamResponseEndMessage:
    request_id: int


@dataclass
class ErrorMessage:
    request_id: int
    # Will be raised in the parent's aiohttp handler coroutine.
    # Must be serializable.
    error: Exception


ParentBoundMessage = Union[
    UnaryResponseMessage,
    StreamResponseStartMessage,
    StreamResponseDataMessage,
    StreamResponseEndMessage,
    ErrorMessage,
]