-
-
Notifications
You must be signed in to change notification settings - Fork 8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[py] implement configurable configuration class for the http client #13286
base: trunk
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# Licensed to the Software Freedom Conservancy (SFC) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The SFC licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
import os | ||
from urllib import parse | ||
|
||
from selenium.webdriver.common.proxy import Proxy | ||
from selenium.webdriver.common.proxy import ProxyType | ||
|
||
|
||
class ClientConfig: | ||
def __init__( | ||
self, | ||
remote_server_addr: str, | ||
keep_alive: bool = True, | ||
proxy=None, | ||
) -> None: | ||
self.remote_server_addr = remote_server_addr | ||
self.keep_alive = keep_alive | ||
self.proxy = proxy | ||
|
||
@property | ||
def remote_server_addr(self) -> str: | ||
return self._remote_server_addr | ||
|
||
@remote_server_addr.setter | ||
def remote_server_addr(self, value: str): | ||
self._remote_server_addr = value | ||
|
||
@property | ||
def keep_alive(self) -> bool: | ||
""":Returns: The keep alive value.""" | ||
return self._keep_alive | ||
|
||
@keep_alive.setter | ||
def keep_alive(self, value: bool) -> None: | ||
"""Toggles the keep alive value. | ||
|
||
:Args: | ||
- value: whether to keep the http connection alive | ||
""" | ||
self._keep_alive = value | ||
|
||
@property | ||
def proxy(self) -> Proxy: | ||
""":Returns: The proxy used for communicating to the driver/server.""" | ||
|
||
self._proxy = self._proxy or Proxy(raw={"proxyType": ProxyType.SYSTEM}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks like an unexpected side effect to me. Would it make sense to set the default value in the constructor? |
||
return self._proxy | ||
|
||
@proxy.setter | ||
def proxy(self, proxy: Proxy) -> None: | ||
"""Provides the information for communicating with the driver or | ||
server. | ||
|
||
:Args: | ||
- value: the proxy information to use to communicate with the driver or server | ||
""" | ||
self._proxy = proxy | ||
|
||
def get_proxy_url(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we define the type of the returned value? |
||
if self.proxy.proxy_type == ProxyType.DIRECT: | ||
return None | ||
elif self.proxy.proxy_type == ProxyType.SYSTEM: | ||
_no_proxy = os.environ.get("no_proxy", os.environ.get("NO_PROXY")) | ||
if _no_proxy: | ||
for npu in _no_proxy.split(","): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we have better variable names? I have no idea what |
||
npu = npu.strip() | ||
if npu == "*": | ||
return None | ||
n_url = parse.urlparse(npu) | ||
remote_add = parse.urlparse(self.remote_server_addr) | ||
if n_url.netloc: | ||
if remote_add.netloc == n_url.netloc: | ||
return None | ||
else: | ||
if n_url.path in remote_add.netloc: | ||
return None | ||
if self.remote_server_addr.startswith("https://"): | ||
return os.environ.get("https_proxy", os.environ.get("HTTPS_PROXY")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this type of call might be extracted to a separate method, so there is less unnecessary code duplication |
||
if self.remote_server_addr.startswith("http://"): | ||
return os.environ.get("http_proxy", os.environ.get("HTTP_PROXY")) | ||
elif self.proxy.proxy_type == ProxyType.MANUAL: | ||
if self.remote_server_addr.startswith("https://"): | ||
return self.proxy.sslProxy | ||
elif self.remote_server_addr.startswith("http://"): | ||
return self.proxy.http_proxy | ||
else: | ||
return None | ||
else: | ||
return None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the argument type is missing. also the spacing does not look correct