Skip to content

Commit b42dd54

Browse files
authored
Merge pull request #57 from Aspire1Inspire2/Auto-refresh-query-limit
More robust token refresh and exception handling
2 parents f962ef3 + 08edb38 commit b42dd54

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,4 @@ pypirc
116116
*State.json
117117
tests/parse_level_two.py
118118
certs/
119+
td/td_state.json

td/client.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@
2121
from td.oauth import run
2222
from td.oauth import shutdown
2323

24+
class TknExpError(Exception):
25+
"""Raise exception when refresh or access token is expired.
26+
27+
Args:
28+
Exception (Exception): The base python exception class
29+
"""
30+
def __init__(self, message):
31+
"""Print out message for this exception.
32+
33+
Args:
34+
message (str): Pass in the message returned by the server.
35+
"""
36+
self.message = message
37+
super().__init__(self.message)
2438

2539
class TDClient():
2640

@@ -438,25 +452,25 @@ def _token_seconds(self, token_type: str = 'access_token') -> int:
438452
if token_type == 'access_token':
439453

440454
# if the time to expiration is less than or equal to 0, return 0.
441-
if not self.state['access_token'] or time.time() >= self.state['access_token_expires_at']:
455+
if not self.state['access_token'] or time.time() + 60 >= self.state['access_token_expires_at']:
442456
return 0
443457

444458
# else return the number of seconds until expiration.
445-
token_exp = int(self.state['access_token_expires_at'] - time.time())
459+
token_exp = int(self.state['access_token_expires_at'] - time.time() - 60)
446460

447461
# if needed check the refresh token.
448462
elif token_type == 'refresh_token':
449463

450464
# if the time to expiration is less than or equal to 0, return 0.
451-
if not self.state['refresh_token'] or time.time() >= self.state['refresh_token_expires_at']:
465+
if not self.state['refresh_token'] or time.time() + 60 >= self.state['refresh_token_expires_at']:
452466
return 0
453467

454468
# else return the number of seconds until expiration.
455-
token_exp = int(self.state['refresh_token_expires_at'] - time.time())
469+
token_exp = int(self.state['refresh_token_expires_at'] - time.time() - 60)
456470

457471
return token_exp
458472

459-
def _token_validation(self, nseconds: int = 5):
473+
def _token_validation(self, nseconds: int = 60):
460474
"""Checks if a token is valid.
461475
462476
Verify the current access token is valid for at least N seconds, and
@@ -569,6 +583,9 @@ def _make_request(self, method: str, endpoint: str, mode: str = None, params: di
569583
print("RESPONSE TEXT: {text}".format(text=response.text))
570584
print('-'*80)
571585

586+
if response.status_code == 401:
587+
raise TknExpError(message=response.text)
588+
572589
def _validate_arguments(self, endpoint: str, parameter_name: str, parameter_argument: List[str]) -> bool:
573590
"""Validates arguments for an API call.
574591

0 commit comments

Comments
 (0)