mirror of
https://github.com/brygphilomena/pyhuntress.git
synced 2025-11-04 16:27:30 +00:00
Add handling of 429 Too Many Attempts response for when calls are ratelimited
This commit is contained in:
parent
f305f898f0
commit
1f7fe70ffb
@ -20,6 +20,7 @@ from pyhuntress.exceptions import (
|
|||||||
ObjectExistsError,
|
ObjectExistsError,
|
||||||
PermissionsFailedException,
|
PermissionsFailedException,
|
||||||
ServerError,
|
ServerError,
|
||||||
|
TooManyRequestsException,
|
||||||
)
|
)
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
@ -80,15 +81,6 @@ class HuntressClient(ABC):
|
|||||||
params=cast(dict[str, Any], params or {}),
|
params=cast(dict[str, Any], params or {}),
|
||||||
stream=stream,
|
stream=stream,
|
||||||
)
|
)
|
||||||
# elif rawdata:
|
|
||||||
# response = requests.request(
|
|
||||||
# method,
|
|
||||||
# url,
|
|
||||||
# headers=headers,
|
|
||||||
# data=rawdata,
|
|
||||||
# params=cast(dict[str, Any], params or {}),
|
|
||||||
# stream=stream,
|
|
||||||
# )
|
|
||||||
else:
|
else:
|
||||||
response = requests.request(
|
response = requests.request(
|
||||||
method,
|
method,
|
||||||
@ -100,7 +92,7 @@ class HuntressClient(ABC):
|
|||||||
if not response.ok:
|
if not response.ok:
|
||||||
with contextlib.suppress(json.JSONDecodeError):
|
with contextlib.suppress(json.JSONDecodeError):
|
||||||
details: dict = response.json()
|
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":
|
if details.get("code") == "InvalidObject":
|
||||||
errors = details.get("errors", [])
|
errors = details.get("errors", [])
|
||||||
if len(errors) > 1:
|
if len(errors) > 1:
|
||||||
@ -125,6 +117,8 @@ class HuntressClient(ABC):
|
|||||||
raise MethodNotAllowedException(response)
|
raise MethodNotAllowedException(response)
|
||||||
if response.status_code == 409:
|
if response.status_code == 409:
|
||||||
raise ConflictException(response)
|
raise ConflictException(response)
|
||||||
|
if response.status_code == 429:
|
||||||
|
raise TooManyRequestsException(response)
|
||||||
if response.status_code == 500:
|
if response.status_code == 500:
|
||||||
# if timeout is mentioned anywhere in the response then we'll retry.
|
# if timeout is mentioned anywhere in the response then we'll retry.
|
||||||
# Ideally we'd return immediately on any non-timeout errors (since
|
# Ideally we'd return immediately on any non-timeout errors (since
|
||||||
|
|||||||
@ -75,6 +75,10 @@ class ConflictException(HuntressException):
|
|||||||
_code_explanation = "Conflict"
|
_code_explanation = "Conflict"
|
||||||
_error_suggestion = "This resource is possibly in use or conflicts with another record."
|
_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):
|
class ServerError(HuntressException):
|
||||||
_code_explanation = "Internal Server Error"
|
_code_explanation = "Internal Server Error"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user