Skip to content

Commit

Permalink
add more backoffs
Browse files Browse the repository at this point in the history
  • Loading branch information
keyn4 committed Jul 8, 2024
1 parent ccc50f7 commit b475af9
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions src/tap_intacct/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class InvalidXmlResponse(Exception):
pass
class BadGatewayError(Exception):
pass
class OfflineServiceError(Exception):
pass
class RateLimitError(Exception):
pass

def _format_date_for_intacct(datetime: dt.datetime) -> str:
"""
Expand Down Expand Up @@ -128,8 +132,16 @@ def _set_session_id(self, user_id: str, company_id: str, user_password: str):

@backoff.on_exception(
backoff.expo,
(BadGatewayError, ConnectionError, ConnectionResetError, requests.exceptions.ConnectionError),
max_tries=5,
(
BadGatewayError,
OfflineServiceError,
ConnectionError,
ConnectionResetError,
requests.exceptions.ConnectionError,
InternalServerError,
RateLimitError,
),
max_tries=8,
factor=3,
)
@singer.utils.ratelimit(10, 1)
Expand All @@ -151,14 +163,28 @@ def _post_request(self, dict_body: dict, api_url: str) -> Dict:
logger.info(f"request to {api_url} with body {body}")
response = requests.post(api_url, headers=api_headers, data=body)

logger.info(f"request to {api_url} response {response.text}, statuscode {response.status_code}")
logger.info(
f"request to {api_url} response {response.text}, statuscode {response.status_code}"
)
try:
parsed_xml = xmltodict.parse(response.text)
parsed_response = json.loads(json.dumps(parsed_xml))
except:
if response.status_code == 502:
raise BadGatewayError(f"Response status code: {response.status_code}, response: {response.text}")
raise InvalidXmlResponse(f"Response status code: {response.status_code}, response: {response.text}")
raise BadGatewayError(
f"Response status code: {response.status_code}, response: {response.text}"
)
if response.status_code == 503:
raise OfflineServiceError(
f"Response status code: {response.status_code}, response: {response.text}"
)
if response.status_code == 429:
raise RateLimitError(
f"Response status code: {response.status_code}, response: {response.text}"
)
raise InvalidXmlResponse(
f"Response status code: {response.status_code}, response: {response.text}"
)

if response.status_code == 200:
if parsed_response['response']['control']['status'] == 'success':
Expand All @@ -180,7 +206,7 @@ def _post_request(self, dict_body: dict, api_url: str) -> Dict:

if api_response['result']['status'] == 'success':
return api_response

if (
api_response['result']['status'] == 'failure'
and "There was an error processing the request"
Expand Down Expand Up @@ -215,6 +241,7 @@ def _post_request(self, dict_body: dict, api_url: str) -> Dict:
raise InternalServerError('Internal server error', parsed_response)

raise SageIntacctSDKError('Error: {0}'.format(parsed_response))


def support_id_msg(self, errormessages) -> Union[List, Dict]:
"""
Expand Down

0 comments on commit b475af9

Please sign in to comment.