Add handling of 429 Too Many Attempts response for when calls are ratelimited

This commit is contained in:
Peter Annabel 2025-07-29 11:07:32 -05:00
parent f305f898f0
commit 1f7fe70ffb
2 changed files with 8 additions and 10 deletions

View File

@ -20,6 +20,7 @@ from pyhuntress.exceptions import (
ObjectExistsError,
PermissionsFailedException,
ServerError,
TooManyRequestsException,
)
if TYPE_CHECKING:
@ -80,15 +81,6 @@ class HuntressClient(ABC):
params=cast(dict[str, Any], params or {}),
stream=stream,
)
# elif rawdata:
# response = requests.request(
# method,
# url,
# headers=headers,
# data=rawdata,
# params=cast(dict[str, Any], params or {}),
# stream=stream,
# )
else:
response = requests.request(
method,
@ -100,7 +92,7 @@ class HuntressClient(ABC):
if not response.ok:
with contextlib.suppress(json.JSONDecodeError):
details: dict = response.json()
if response.status_code == 400: # noqa: SIM102 (Expecting to handle other codes in the future)
if response.status_code == 400:
if details.get("code") == "InvalidObject":
errors = details.get("errors", [])
if len(errors) > 1:
@ -125,6 +117,8 @@ class HuntressClient(ABC):
raise MethodNotAllowedException(response)
if response.status_code == 409:
raise ConflictException(response)
if response.status_code == 429:
raise TooManyRequestsException(response)
if response.status_code == 500:
# if timeout is mentioned anywhere in the response then we'll retry.
# Ideally we'd return immediately on any non-timeout errors (since

View File

@ -75,6 +75,10 @@ class ConflictException(HuntressException):
_code_explanation = "Conflict"
_error_suggestion = "This resource is possibly in use or conflicts with another record."
class TooManyRequestsException(HuntressException):
_code_explanation = "Too Many Requests"
_error_suggestion = "This resource is currently being rate limited. Please wait and try again."
class ServerError(HuntressException):
_code_explanation = "Internal Server Error"