From 32cdfe63eb2e20aba3fd47caefc864e6033fcdc2 Mon Sep 17 00:00:00 2001 From: oitsjustjose Date: Sun, 30 Apr 2023 10:48:37 -0500 Subject: [PATCH] Safer Response Body Fetching --- processor/src/mgmt_tools.py | 11 +++++++++++ processor/src/x_fast_provider.py | 3 +-- processor/src/x_slow_provider.py | 6 +----- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/processor/src/mgmt_tools.py b/processor/src/mgmt_tools.py index 47f8327..e1c6a09 100644 --- a/processor/src/mgmt_tools.py +++ b/processor/src/mgmt_tools.py @@ -109,3 +109,14 @@ class MgmtApiLogger: Args: msg(str): the message to log """ self.helper.append_job_log(self._job_id, msg) + + def decode_modrinth_resp(self, resp: requests.Response) -> str: + """ + Decodes a modrinth response into a normal, human-readable string + Args: resp (Response): the response from your request + Returns: (str): the human-readable string + """ + try: + return resp.json()["description"] + except (requests.exceptions.JSONDecodeError, KeyError): + return resp.text diff --git a/processor/src/x_fast_provider.py b/processor/src/x_fast_provider.py index 26bb0b0..070b8a9 100644 --- a/processor/src/x_fast_provider.py +++ b/processor/src/x_fast_provider.py @@ -100,13 +100,12 @@ class FastProvider(MgmtApiLogger): statuses.append(Status.SUCCESS) self.logmsg(f"✅ {display_nm}") else: - print(response.json()["description"]) statuses.append(Status.FAIL) self.logmsg( dedent( f"""----- 🔥 {display_nm} ----- API Response from Modrinth FAIL for {display_nm}: - {response.json()["description"]} + {self.decode_modrinth_resp(response)} """ ).strip("\n") ) diff --git a/processor/src/x_slow_provider.py b/processor/src/x_slow_provider.py index 03383de..4b6effd 100644 --- a/processor/src/x_slow_provider.py +++ b/processor/src/x_slow_provider.py @@ -40,7 +40,6 @@ def driver_get(driver: webdriver, url: str, timeout=30) -> bool: (bool): True if successfully navigated, False otherwise """ try: - print(f"Attempting to get URL {url}") driver.get(url) WebDriverWait(driver, timeout).until( EC.presence_of_element_located((By.TAG_NAME, "body")) @@ -226,16 +225,13 @@ class SlowProvider(MgmtApiLogger): if response.status_code == 200: statuses.append(Status.SUCCESS) self.logmsg(f"✅ {display_nm}") - print(f"✅ {display_nm}") else: - print(response.text) - print(response.json()) statuses.append(Status.FAIL) self.logmsg( dedent( f"""----- 🔥 {display_nm} ----- API Response from Modrinth FAIL for {display_nm}: - {response.json()["description"]} + {self.decode_modrinth_resp(response)} """ ).strip("\n") )