Skip to content

Commit

Permalink
Merge pull request #164 from rudderlabs/fix/etl-163-nonJsonResponse-h…
Browse files Browse the repository at this point in the history
…ubspot

fix: fixes empty response from api
  • Loading branch information
a-rampalli committed Oct 11, 2023
2 parents 5e099a8 + 1beebf6 commit 5afd202
Showing 1 changed file with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from functools import cached_property, lru_cache
from http import HTTPStatus
from typing import Any, Dict, Iterable, List, Mapping, MutableMapping, Optional, Set, Tuple, Union
from json.decoder import JSONDecodeError

import backoff
import pendulum as pendulum
Expand Down Expand Up @@ -77,6 +78,8 @@ def giveup_handler(exc):
if (isinstance(exc, HubspotInvalidAuth) or isinstance(exc, HTTPError)) \
and any([m in exc.response.text.lower() for m in TOKEN_EXPIRED_ERROR]):
return False
if isinstance(exc, JSONDecodeError):
return False
if TOKEN_REFRESH_RETRIES_EXCEEDED_ERROR.lower() in exc.response.text.lower():
return False
if isinstance(exc, (HubspotInvalidAuth, HubspotAccessDenied)):
Expand All @@ -85,7 +88,7 @@ def giveup_handler(exc):

return backoff.on_exception(
backoff.expo,
requests.exceptions.RequestException,
(requests.exceptions.RequestException, JSONDecodeError),
jitter=None,
on_backoff=log_retry_attempt,
giveup=giveup_handler,
Expand Down Expand Up @@ -206,7 +209,15 @@ def get(
self, url: str, params: MutableMapping[str, Any] = None
) -> Tuple[Union[MutableMapping[str, Any], List[MutableMapping[str, Any]]], requests.Response]:
response = self._session.get(self.BASE_URL + url, params=params)
if any([m in response.json() for m in TOKEN_EXPIRED_ERROR]):
responseJson = None
try:
responseJson = response.json()
except JSONDecodeError as e:
err_msg = f"Failed to parse response text: {response.text} with JSONDecodeError."
logger.warn(err_msg)
raise JSONDecodeError(err_msg, e.doc, e.pos)

if any([m in responseJson for m in TOKEN_EXPIRED_ERROR]):
logger.info("Oauth token expired. Re-fetching token")
self._session.auth = self.get_authenticator()
return self._parse_and_handle_errors(response), response
Expand Down

0 comments on commit 5afd202

Please sign in to comment.