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 2 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 @@ -205,8 +206,24 @@ def _parse_and_handle_errors(response) -> Union[MutableMapping[str, Any], List[M
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
MAX_RETRIES = 5

for retries in range(MAX_RETRIES):
a-rampalli marked this conversation as resolved.
Show resolved Hide resolved
response = self._session.get(self.BASE_URL + url, params=params)
try:
responseJson = response.json()
break # Success, exit the loop
except JSONDecodeError as e:
err_msg = f"Failed to parse response text: {response.text} with JSONDecodeError. Retrying ({retries+1}/{MAX_RETRIES})..."
logger.warn(err_msg)
retries += 1

if retries == MAX_RETRIES:
logger.error(f"Failed to parse response: {response.text} with JSONDecodeError")
raise JSONDecodeError("Unable to parse response", 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