|
5 | 5 | from enum import Enum |
6 | 6 | from typing import Union, List, Tuple, Dict |
7 | 7 |
|
| 8 | +class Address: |
| 9 | + def __init__(self, json: dict): |
| 10 | + self._ip = json['ip'] |
| 11 | + self._port = json['port'] |
| 12 | + |
| 13 | + # The IP address. |
| 14 | + @property |
| 15 | + def ip(self) -> str: |
| 16 | + return self._ip |
| 17 | + |
| 18 | + # The port number. |
| 19 | + @property |
| 20 | + def port(self) -> int: |
| 21 | + return self._port |
| 22 | + |
| 23 | + # Serialize the address to a dict. |
| 24 | + def serialize(self) -> dict: |
| 25 | + return { |
| 26 | + 'ip': self._ip, |
| 27 | + 'port': self._port, |
| 28 | + } |
| 29 | + |
| 30 | +class Connection: |
| 31 | + def __init__(self, json: dict): |
| 32 | + self._id = json['id'] |
| 33 | + self._timestamp = json['timestamp'] |
| 34 | + self._local = Address(json['local']) |
| 35 | + self._remote = Address(json['remote']) |
| 36 | + |
| 37 | + # The TCP connection id. |
| 38 | + @property |
| 39 | + def id(self) -> int: |
| 40 | + return self._id |
| 41 | + |
| 42 | + # The TCP connection establised timestamp. |
| 43 | + @property |
| 44 | + def timestamp(self) -> int: |
| 45 | + return self._timestamp |
| 46 | + |
| 47 | + # The local(client) address. |
| 48 | + @property |
| 49 | + def local(self) -> Address: |
| 50 | + return self._local |
| 51 | + |
| 52 | + # The remote(server) address. |
| 53 | + @property |
| 54 | + def remote(self) -> Address: |
| 55 | + return self._remote |
| 56 | + |
| 57 | + # Serialize the connection info to a dict. |
| 58 | + def serialize(self) -> dict: |
| 59 | + return { |
| 60 | + 'id': self._id, |
| 61 | + 'timestamp': self._timestamp, |
| 62 | + 'local': self._local.serialize(), |
| 63 | + 'remote': self._remote.serialize(), |
| 64 | + } |
| 65 | + |
8 | 66 | class App: |
9 | 67 | def __init__(self, json: dict): |
10 | 68 | self._name = json['name'] |
@@ -51,17 +109,20 @@ def __init__(self, json: dict): |
51 | 109 | self._scheme = json['scheme'] |
52 | 110 | self._host = json['host'] |
53 | 111 | self._port = json['port'] |
54 | | - self._cid = json['cid'] |
55 | | - self._ctime = json['ctime'] |
56 | | - self._sid = json['sid'] |
57 | | - self._stime = json['stime'] |
58 | | - self._env = json.get('env') |
59 | | - self._comment = json.get('comment') |
| 112 | + self._id = json['id'] |
| 113 | + self._timestamp = json['timestamp'] |
| 114 | + connection = json['connection'] |
| 115 | + if connection is None: |
| 116 | + self._connection = None |
| 117 | + else: |
| 118 | + self._connection = Connection(connection) |
60 | 119 | app = json.get('app') |
61 | 120 | if app is None: |
62 | 121 | self._app = None |
63 | 122 | else: |
64 | 123 | self._app = App(app) |
| 124 | + self._env = json.get('env') |
| 125 | + self._comment = json.get('comment') |
65 | 126 | self._highlight = None |
66 | 127 | self.shared = json.get('shared') |
67 | 128 |
|
@@ -94,25 +155,44 @@ def host(self) -> str: |
94 | 155 | def port(self) -> int: |
95 | 156 | return self._port |
96 | 157 |
|
97 | | - # TCP connection id. |
| 158 | + # Deprecated! Use `connection.id` instead. |
98 | 159 | @property |
99 | | - def cid(self) -> int: |
100 | | - return self._cid |
| 160 | + def cid(self) -> Union[int, None]: |
| 161 | + if self._connection is None: |
| 162 | + return None |
| 163 | + return self._connection.id |
| 164 | + |
| 165 | + # Deprecated! Use `connection.timestamp` instead. |
| 166 | + @property |
| 167 | + def ctime(self) -> Union[int, None]: |
| 168 | + if self._connection is None: |
| 169 | + return None |
| 170 | + return self._connection.timestamp |
101 | 171 |
|
102 | | - # TCP connection establised timestamp. |
| 172 | + # HTTP request session id. |
103 | 173 | @property |
104 | | - def ctime(self) -> int: |
105 | | - return self._ctime |
| 174 | + def id(self) -> int: |
| 175 | + return self._id |
106 | 176 |
|
107 | | - # HTTP session id. |
| 177 | + # Deprecated! Use `id` instead. |
108 | 178 | @property |
109 | 179 | def sid(self) -> int: |
110 | | - return self._sid |
| 180 | + return self._id |
111 | 181 |
|
112 | | - # HTTP session timestamp. |
| 182 | + # HTTP request session timestamp. |
| 183 | + @property |
| 184 | + def timestamp(self) -> int: |
| 185 | + return self._timestamp |
| 186 | + |
| 187 | + # Deprecated! Use `timestamp` instead. |
113 | 188 | @property |
114 | 189 | def stime(self) -> int: |
115 | | - return self._stime |
| 190 | + return self._timestamp |
| 191 | + |
| 192 | + # TCP connection info. In REST API, it always be None. |
| 193 | + @property |
| 194 | + def connection(self) -> Union[Connection, None]: |
| 195 | + return self._connection |
116 | 196 |
|
117 | 197 | # HTTP uniqued id. |
118 | 198 | @property |
@@ -154,12 +234,11 @@ def toJson(self) -> str: |
154 | 234 | 'scheme': self._scheme, |
155 | 235 | 'host': self._host, |
156 | 236 | 'port': self._port, |
157 | | - 'cid': self._cid, |
158 | | - 'ctime': self._ctime, |
159 | | - 'sid': self._sid, |
160 | | - 'stime': self._stime, |
| 237 | + 'id': self._id, |
| 238 | + 'timestamp': self._timestamp, |
161 | 239 | 'env': self._env, |
162 | | - 'app': self._app.serialize(), |
| 240 | + 'connection': None if self._connection is None else self._connection.serialize(), |
| 241 | + 'app': None if self._app is None else self._app.serialize(), |
163 | 242 | 'shared': self.shared, |
164 | 243 | 'highlight': self._highlight, |
165 | 244 | 'comment': self._comment, |
|
0 commit comments