|
21 | 21 | from td.oauth import run |
22 | 22 | from td.oauth import shutdown |
23 | 23 |
|
| 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) |
24 | 38 |
|
25 | 39 | class TDClient(): |
26 | 40 |
|
@@ -438,25 +452,25 @@ def _token_seconds(self, token_type: str = 'access_token') -> int: |
438 | 452 | if token_type == 'access_token': |
439 | 453 |
|
440 | 454 | # 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']: |
442 | 456 | return 0 |
443 | 457 |
|
444 | 458 | # 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) |
446 | 460 |
|
447 | 461 | # if needed check the refresh token. |
448 | 462 | elif token_type == 'refresh_token': |
449 | 463 |
|
450 | 464 | # 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']: |
452 | 466 | return 0 |
453 | 467 |
|
454 | 468 | # 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) |
456 | 470 |
|
457 | 471 | return token_exp |
458 | 472 |
|
459 | | - def _token_validation(self, nseconds: int = 5): |
| 473 | + def _token_validation(self, nseconds: int = 60): |
460 | 474 | """Checks if a token is valid. |
461 | 475 |
|
462 | 476 | 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 |
569 | 583 | print("RESPONSE TEXT: {text}".format(text=response.text)) |
570 | 584 | print('-'*80) |
571 | 585 |
|
| 586 | + if response.status_code == 401: |
| 587 | + raise TknExpError(message=response.text) |
| 588 | + |
572 | 589 | def _validate_arguments(self, endpoint: str, parameter_name: str, parameter_argument: List[str]) -> bool: |
573 | 590 | """Validates arguments for an API call. |
574 | 591 |
|
|
0 commit comments