Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixes empty response from api #164

Merged
merged 3 commits into from
Oct 11, 2023
Merged
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
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
Loading