network_handler.py: Implement get() wrapper

This commit is contained in:
Mykola Grymalyuk
2023-03-31 21:22:13 -06:00
parent 8512aad33d
commit b83cd8191b
5 changed files with 37 additions and 5 deletions

View File

@@ -13,6 +13,8 @@
- Credit to [Ausdauersportler](https://github.com/Ausdauersportler) for implementation
- Backend changes:
- Use `.AppleSystemUIFont` for wxPython text rendering (thanks [jazzzny](https://github.com/Jazzzny))
- Add extra error handling for network errors
- Handles `RemoteDisconnected('Remote end closed connection without response')` exceptions
- Increment Binaries:
- PatcherSupportPkg 0.9.2 - release

View File

@@ -99,7 +99,7 @@ class KernelDebugKitObject:
return KDK_ASSET_LIST
try:
results = network_handler.SESSION.get(
results = network_handler.NetworkUtilities().get(
KDK_API_LINK,
headers={
"User-Agent": f"OCLP/{self.constants.patcher_version}"

View File

@@ -270,7 +270,7 @@ class RemoteInstallerCatalog:
return catalog
try:
catalog = plistlib.loads(network_handler.SESSION.get(self.catalog_url).content)
catalog = plistlib.loads(network_handler.NetworkUtilities().get(self.catalog_url).content)
except plistlib.InvalidFileException:
return {}
@@ -311,7 +311,7 @@ class RemoteInstallerCatalog:
continue
try:
build_plist = plistlib.loads(network_handler.SESSION.get(bm_package["URL"]).content)
build_plist = plistlib.loads(network_handler.NetworkUtilities().get(bm_package["URL"]).content)
except plistlib.InvalidFileException:
continue

View File

@@ -80,6 +80,36 @@ class NetworkUtilities:
return False
def get(self, url: str, **kwargs) -> requests.Response:
"""
Wrapper for requests's get method
Implement additional error handling
Parameters:
url (str): URL to get
**kwargs: Additional parameters for requests.get
Returns:
requests.Response: Response object from requests.get
"""
result: requests.Response = None
try:
result = SESSION.get(url, **kwargs)
except (
requests.exceptions.Timeout,
requests.exceptions.TooManyRedirects,
requests.exceptions.ConnectionError,
requests.exceptions.HTTPError
) as error:
logging.warn(f"Error calling requests.get: {error}")
# Return empty response object
return requests.Response()
return result
class DownloadObject:
"""
Object for downloading files from the network
@@ -278,7 +308,7 @@ class DownloadObject:
if self._prepare_working_directory(self.filepath) is False:
raise Exception(self.error_msg)
response = SESSION.get(self.url, stream=True, timeout=10)
response = NetworkUtilities().get(self.url, stream=True, timeout=10)
with open(self.filepath, 'wb') as file:
for i, chunk in enumerate(response.iter_content(1024 * 1024 * 4)):

View File

@@ -96,7 +96,7 @@ class CheckBinaryUpdates:
if not network_handler.NetworkUtilities(REPO_LATEST_RELEASE_URL).verify_network_connection():
return None
response = requests.get(REPO_LATEST_RELEASE_URL)
response = network_handler.NetworkUtilities().get(REPO_LATEST_RELEASE_URL)
data_set = response.json()
self.remote_version = data_set["tag_name"]