Safer Response Body Fetching

This commit is contained in:
oitsjustjose 2023-04-30 10:48:37 -05:00
commit 32cdfe63eb
3 changed files with 13 additions and 7 deletions

View file

@ -109,3 +109,14 @@ class MgmtApiLogger:
Args: msg(str): the message to log Args: msg(str): the message to log
""" """
self.helper.append_job_log(self._job_id, msg) 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

View file

@ -100,13 +100,12 @@ class FastProvider(MgmtApiLogger):
statuses.append(Status.SUCCESS) statuses.append(Status.SUCCESS)
self.logmsg(f"✅ {display_nm}") self.logmsg(f"✅ {display_nm}")
else: else:
print(response.json()["description"])
statuses.append(Status.FAIL) statuses.append(Status.FAIL)
self.logmsg( self.logmsg(
dedent( dedent(
f"""----- 🔥 {display_nm} ----- f"""----- 🔥 {display_nm} -----
API Response from Modrinth FAIL for {display_nm}: API Response from Modrinth FAIL for {display_nm}:
{response.json()["description"]} {self.decode_modrinth_resp(response)}
""" """
).strip("\n") ).strip("\n")
) )

View file

@ -40,7 +40,6 @@ def driver_get(driver: webdriver, url: str, timeout=30) -> bool:
(bool): True if successfully navigated, False otherwise (bool): True if successfully navigated, False otherwise
""" """
try: try:
print(f"Attempting to get URL {url}")
driver.get(url) driver.get(url)
WebDriverWait(driver, timeout).until( WebDriverWait(driver, timeout).until(
EC.presence_of_element_located((By.TAG_NAME, "body")) EC.presence_of_element_located((By.TAG_NAME, "body"))
@ -226,16 +225,13 @@ class SlowProvider(MgmtApiLogger):
if response.status_code == 200: if response.status_code == 200:
statuses.append(Status.SUCCESS) statuses.append(Status.SUCCESS)
self.logmsg(f"✅ {display_nm}") self.logmsg(f"✅ {display_nm}")
print(f"✅ {display_nm}")
else: else:
print(response.text)
print(response.json())
statuses.append(Status.FAIL) statuses.append(Status.FAIL)
self.logmsg( self.logmsg(
dedent( dedent(
f"""----- 🔥 {display_nm} ----- f"""----- 🔥 {display_nm} -----
API Response from Modrinth FAIL for {display_nm}: API Response from Modrinth FAIL for {display_nm}:
{response.json()["description"]} {self.decode_modrinth_resp(response)}
""" """
).strip("\n") ).strip("\n")
) )