Skip to content

Commit 31aa337

Browse files
committed
Added connection info property in Context class
1 parent 973619a commit 31aa337

1 file changed

Lines changed: 100 additions & 21 deletions

File tree

reqable/reqable.py

Lines changed: 100 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,64 @@
55
from enum import Enum
66
from typing import Union, List, Tuple, Dict
77

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+
866
class App:
967
def __init__(self, json: dict):
1068
self._name = json['name']
@@ -51,17 +109,20 @@ def __init__(self, json: dict):
51109
self._scheme = json['scheme']
52110
self._host = json['host']
53111
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)
60119
app = json.get('app')
61120
if app is None:
62121
self._app = None
63122
else:
64123
self._app = App(app)
124+
self._env = json.get('env')
125+
self._comment = json.get('comment')
65126
self._highlight = None
66127
self.shared = json.get('shared')
67128

@@ -94,25 +155,44 @@ def host(self) -> str:
94155
def port(self) -> int:
95156
return self._port
96157

97-
# TCP connection id.
158+
# Deprecated! Use `connection.id` instead.
98159
@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
101171

102-
# TCP connection establised timestamp.
172+
# HTTP request session id.
103173
@property
104-
def ctime(self) -> int:
105-
return self._ctime
174+
def id(self) -> int:
175+
return self._id
106176

107-
# HTTP session id.
177+
# Deprecated! Use `id` instead.
108178
@property
109179
def sid(self) -> int:
110-
return self._sid
180+
return self._id
111181

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.
113188
@property
114189
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
116196

117197
# HTTP uniqued id.
118198
@property
@@ -154,12 +234,11 @@ def toJson(self) -> str:
154234
'scheme': self._scheme,
155235
'host': self._host,
156236
'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,
161239
'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(),
163242
'shared': self.shared,
164243
'highlight': self._highlight,
165244
'comment': self._comment,

0 commit comments

Comments
 (0)