Skip to content

Commit

Permalink
refactor: raise custom exception in get_user_status_request
Browse files Browse the repository at this point in the history
  • Loading branch information
Barabazs committed Jun 7, 2024
1 parent 8c30dcb commit 067d745
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
23 changes: 19 additions & 4 deletions archivooor/archiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

from archivooor import exceptions


class NetworkHandler:
"""
Expand Down Expand Up @@ -185,15 +187,28 @@ def get_user_status_request(self):
"""
url = f"https://web.archive.org/save/status/user?_t={int(time.time())}"
response = self.session.get(url=url)

if response.status_code == 200:
try:
return response.json()
except:
except Exception as exc:
if "Too Many Requests" in response.text:
return 429, "Too Many Requests"
return response.text
raise exceptions.ArchivooorException(
"Too Many Requests - Please try again later."
) from exc
raise exc
elif response.status_code == 429:
raise exceptions.ArchivooorException(
"Too Many Requests - Please try again later."
)
elif response.status_code == 401:
raise exceptions.ArchivooorException(
"Unauthorized - Please check if the keys are correct."
)
else:
return response.status_code, response.text
raise exceptions.ArchivooorException(
f"Unexpected error: {response.status_code} - {response.text}"
)


class Sitemap:
Expand Down
5 changes: 5 additions & 0 deletions archivooor/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Exceptions for Archivooor."""


class ArchivooorException(Exception):
"""Base class for archivooor exceptions."""

0 comments on commit 067d745

Please sign in to comment.