Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/pyconnectwise/clients/connectwise_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import contextlib
import json
import threading
import warnings
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Any, cast
Expand Down Expand Up @@ -115,6 +116,20 @@ def _make_request( # noqa: C901
raise MethodNotAllowedException(response)
if response.status_code == 409:
raise ConflictException(response)
if response.status_code == 429:
# Rate limit exceeded
if "Retry-After" in response.headers:
retry_after = int(response.headers["Retry-After"])
warnings.warn(
f"Rate limit exceeded. Retrying after {retry_after} seconds.",
stacklevel=1,
)
# Delay the request for the specified time
threading.Event.wait(retry_after)
if retry_count < self.config.max_retries:
retry_count += 1
return self._make_request(method, url, data, params, headers, retry_count)
raise MalformedRequestException(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
Expand Down
5 changes: 5 additions & 0 deletions src/pyconnectwise/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ class ConflictException(ConnectWiseException):
_error_suggestion = "This resource is possibly in use or conflicts with another record."


class TooManyRequestsException(ConnectWiseException):
_code_explanation = "Too Many Requests"
_error_suggestion = "You have exceeded the rate limit for this resource. Please wait and try again later."


class ServerError(ConnectWiseException):
_code_explanation = "Internal Server Error"

Expand Down
Loading