Skip to content

Commit

Permalink
Merge pull request #309 from bug-or-feature/invalid_token
Browse files Browse the repository at this point in the history
Invalid token
  • Loading branch information
bug-or-feature authored Nov 16, 2023
2 parents d8e1a98 + 3858f2f commit f7d5d21
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions trading_ig/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ class ApiExceededException(Exception):
pass


class TokenInvalidException(Exception):
"""Raised when the v3 session token is invalid or expired"""

pass


class IGException(Exception):
pass

Expand Down Expand Up @@ -385,6 +391,10 @@ def _request(self, action, endpoint, params, session, version="1", check=True):
response.encoding = "utf-8"
if self._api_limit_hit(response.text):
raise ApiExceededException()
if self._token_invalid(response.text):
logger.error("Invalid authentication token, triggering refresh...")
self._valid_until = datetime.now() - timedelta(seconds=15)
raise TokenInvalidException()
return response

@staticmethod
Expand All @@ -397,6 +407,10 @@ def _api_limit_hit(response_text):
or "exceeded-account-trading-allowance" in response_text
)

@staticmethod
def _token_invalid(response_text):
return "oauth-token-invalid" in response_text

# ---------- PARSE_RESPONSE ----------- #

@staticmethod
Expand Down

0 comments on commit f7d5d21

Please sign in to comment.