Skip to content

Commit 1e1c8d1

Browse files
committed
Added more comprehensive docstrings ... minor tweaks
1 parent 1ac681e commit 1e1c8d1

File tree

1 file changed

+86
-18
lines changed

1 file changed

+86
-18
lines changed

py/selenium/webdriver/common/bidi/network.py

Lines changed: 86 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(self, conn):
4545
"network.authRequired": [],
4646
}
4747

48-
def command_iterator(self, command):
48+
def _command_iterator(self, command):
4949
"""Generator to yield command."""
5050
yield command
5151

@@ -54,7 +54,17 @@ def has_callbacks(self):
5454
return len(self.callbacks) > 0
5555

5656
def __add_intercept(self, phases=None, contexts=None, url_patterns=None):
57-
"""Add an intercept to the network."""
57+
"""Add an intercept to the network.
58+
59+
Parameters:
60+
----------
61+
phases (list, optional): A list of phases to intercept.
62+
Default is None.
63+
contexts (list, optional): A list of contexts to intercept.
64+
Default is None.
65+
url_patterns (list, optional): A list of URL patterns to intercept.
66+
Default is None.
67+
"""
5868
if phases is None:
5969
phases = []
6070
if contexts is None and url_patterns is None:
@@ -68,21 +78,44 @@ def __add_intercept(self, phases=None, contexts=None, url_patterns=None):
6878
else:
6979
params = {"phases": phases, "contexts": contexts, "urlPatterns": url_patterns}
7080
command = {"method": "network.addIntercept", "params": params}
71-
self.conn.execute(self.command_iterator(command))
81+
self.conn.execute(self._command_iterator(command))
7282

7383
def __remove_intercept(self, intercept=None, request_id=None):
74-
"""Remove an intercept from the network."""
84+
"""Remove an intercept from the network.
85+
86+
Parameters:
87+
----------
88+
intercept (str, optional): The intercept to remove.
89+
Default is None.
90+
request_id (str, optional): The request ID of the intercepted response.
91+
Default is None.
92+
93+
Raises:
94+
------
95+
ValueError: If neither requestId nor intercept is specified
96+
97+
Notes:
98+
-----
99+
Either requestId or intercept must be specified.
100+
"""
75101
if request_id is not None:
76102
command = {"method": "network.removeIntercept", "requestId": request_id}
77-
self.conn.execute(self.command_iterator(command))
103+
self.conn.execute(self._command_iterator(command))
78104
elif intercept is not None:
79105
command = {"method": "network.removeIntercept", "intercept": intercept}
80-
self.conn.execute(self.command_iterator(command))
106+
self.conn.execute(self._command_iterator(command))
81107
else:
82108
raise ValueError("Either requestId or intercept must be specified")
83109

84110
def __continue_with_auth(self, request_id, username, password):
85-
"""Continue with authentication."""
111+
"""Continue with authentication.
112+
113+
Parameters:
114+
----------
115+
request_id (str): The request ID of the intercepted response.
116+
username (str): The username to use for authentication.
117+
password (str): The password to use for authentication.
118+
"""
86119
command = {
87120
"method": "network.continueWithAuth",
88121
"params": {
@@ -91,10 +124,20 @@ def __continue_with_auth(self, request_id, username, password):
91124
"credentials": {"type": "password", "username": username, "password": password},
92125
},
93126
}
94-
self.conn.execute(self.command_iterator(command))
127+
self.conn.execute(self._command_iterator(command))
95128

96129
def __on(self, event, callback):
97-
"""Set a callback function to subscribe to a network event."""
130+
"""Set a callback function to subscribe to a network event.
131+
132+
Parameters:
133+
----------
134+
event (str): The event to subscribe to.
135+
callback (function): The callback function to execute on event.
136+
137+
Returns:
138+
-------
139+
str: The request ID of the intercepted response.
140+
"""
98141
event = self.EVENTS.get(event, event)
99142
self.conn.execute(session_subscribe(event))
100143
self.callbacks[event] = callback
@@ -103,12 +146,23 @@ def __on(self, event, callback):
103146
return self.conn.add_callback(event, self.__handle_event)
104147

105148
def __handle_event(self, event, data):
106-
"""Perform callback function on event."""
149+
"""Perform callback function on event.
150+
151+
Parameters:
152+
event (str): The event to perform callback function on.
153+
data (dict): The data to pass to the callback function.
154+
"""
107155
if event in self.callbacks:
108156
self.callbacks[event](data)
109157

110158
def add_authentication_handler(self, username, password):
111-
"""Adds an authentication handler."""
159+
"""Adds an authentication handler.
160+
161+
Parameters:
162+
----------
163+
username (str): The username to use for authentication.
164+
password (str): The password to use for authentication.
165+
"""
112166
self.__add_intercept(phases=[self.PHASES["auth_required"]])
113167
self.__on(
114168
"auth_required", lambda data: self.__continue_with_auth(data["request"]["request"], username, password)
@@ -126,12 +180,14 @@ def add_request_handler(self, callback, url_pattern=""):
126180
request matches the given URL pattern.
127181
128182
Parameters:
183+
----------
129184
callback (function): A function to be executed when url is matched by a URL pattern
130-
The callback function receives a `Response` object as its argument.
185+
The callback function receives a `Request` object as its argument.
131186
url_pattern (str, optional): A substring to match against the response URL.
132187
Default is an empty string, which matches all URLs.
133188
134189
Returns:
190+
-------
135191
str: The request ID of the intercepted response.
136192
"""
137193
self.__add_intercept(phases=[self.PHASES["before_request"]])
@@ -156,7 +212,12 @@ def callback_on_url_match(data):
156212
return request_id
157213

158214
def remove_request_handler(self, request_id):
159-
"""Removes a request handler."""
215+
"""Removes a request handler.
216+
217+
Parameters:
218+
----------
219+
request_id (str): The request ID of the intercepted response.
220+
"""
160221
self.__remove_intercept(request_id=request_id)
161222
self.subscriptions["before_request"].remove(request_id)
162223
del self.callbacks[request_id]
@@ -168,12 +229,14 @@ def add_response_handler(self, callback, url_pattern=""):
168229
response matches the given URL pattern.
169230
170231
Parameters:
232+
----------
171233
callback (function): A function to be executed when url is matched by a url_pattern
172234
The callback function receives a `Response` object as its argument.
173235
url_pattern (str, optional): A substring to match against the response URL.
174236
Default is an empty string, which matches all URLs.
175237
176238
Returns:
239+
-------
177240
str: The request ID of the intercepted response.
178241
"""
179242
self.__add_intercept(phases=[self.PHASES["response_started"]])
@@ -198,7 +261,12 @@ def callback_on_url_match(data):
198261
return request_id
199262

200263
def remove_response_handler(self, response_id):
201-
"""Removes a response handler."""
264+
"""Removes a response handler.
265+
266+
Parameters:
267+
----------
268+
response_id (str): The request ID of the intercepted response.
269+
"""
202270
self.__remove_intercept(request_id=response_id)
203271
self.subscriptions["response_started"].remove(response_id)
204272
del self.callbacks[response_id]
@@ -227,9 +295,9 @@ def continue_request(self):
227295
if self.body is not None:
228296
params["body"] = self.body
229297
command = {"method": "network.continueRequest", "params": params}
230-
self.network.conn.execute(self.command_iterator(command))
298+
self.network.conn.execute(self._command_iterator(command))
231299

232-
def command_iterator(self, command):
300+
def _command_iterator(self, command):
233301
"""Generator to yield command."""
234302
yield command
235303

@@ -251,8 +319,8 @@ def continue_response(self):
251319
if self.body is not None:
252320
params["body"] = self.body
253321
command = {"method": "network.continueResponse", "params": params}
254-
self.network.conn.execute(self.command_iterator(command))
322+
self.network.conn.execute(self._command_iterator(command))
255323

256-
def command_iterator(self, command):
324+
def _command_iterator(self, command):
257325
"""Generator to yield command."""
258326
yield command

0 commit comments

Comments
 (0)