Skip to content

Commit

Permalink
feat: add retrying mechanism for request
Browse files Browse the repository at this point in the history
  • Loading branch information
dylandoamaral committed Feb 21, 2023
1 parent bdae791 commit 1b6c821
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ trakt_tv:
max_medias: 5
recommendation:
movie:
max_medias: 5
max_medias: 10
show:
max_medias: 2
30 changes: 20 additions & 10 deletions custom_components/trakt_tv/apis/trakt.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,22 @@ async def async_get_access_token(self) -> str:

return self.oauth_session.token["access_token"]

async def request(self, method, url, **kwargs) -> ClientResponse:
async def retry_request(self, wait_time, response, method, url, retry, **kwargs):
"""Retry a request {retry} times before logging an error and raising an exception."""
content = await response.text()
error = f"Can't request {url} with {method} because it returns a {response.status} status code with content {content}."

if retry > 0:
retry = retry - 1
guidance = f"Retrying at least {retry} times."
LOGGER.warn(f"{error} {guidance}")
await sleep(wait_time)
return await self.request(method, url, retry, **kwargs)
else:
guidance = f"Too many retries, if you find this error, please raise an issue at https://github.com/dylandoamaral/trakt-integration/issues."
raise TraktException(f"{error} {guidance}")

async def request(self, method, url, retry=5, **kwargs) -> ClientResponse:
"""Make a request."""
access_token = await self.async_get_access_token()
client_id = self.hass.data[DOMAIN]["configuration"]["client_id"]
Expand All @@ -68,16 +83,11 @@ async def request(self, method, url, **kwargs) -> ClientResponse:
return deserialize_json(text)
elif response.status == 429:
wait_time = int(response.headers["Retry-After"])
await sleep(wait_time)
return await self.request(method, url, **kwargs)
elif response.status in [502, 503, 504]:
await sleep(30)
return await self.request(method, url, **kwargs)
await self.retry_request(
wait_time, response, method, url, retry, **kwargs
)
else:
content = await response.text()
error = f"Can't request {url} with {method} because it returns a {response.status} status code with content {content}."
guidance = "If you find this error, please raise an issue at https://github.com/dylandoamaral/trakt-integration/issues."
raise TraktException(f"{error}\n{guidance}")
await self.retry_request(30, response, method, url, retry, **kwargs)

async def fetch_calendar(
self, path: str, from_date: str, nb_days: int, all_medias: bool
Expand Down

0 comments on commit 1b6c821

Please sign in to comment.