Skip to content

Commit ac7bdb1

Browse files
committed
2 parents c0528db + fce3be3 commit ac7bdb1

3 files changed

Lines changed: 130 additions & 6 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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
from td.app.auth import FlaskTDAuth
2525
from td.oauth import run
2626
from td.oauth import shutdown
27-
27+
from td.exceptions import TknExpError, ExdLmtError, NotNulError, \
28+
ForbidError, NotFndError, ServerError, GeneralError
2829

2930
class TDClient():
3031

@@ -442,25 +443,25 @@ def _token_seconds(self, token_type: str = 'access_token') -> int:
442443
if token_type == 'access_token':
443444

444445
# if the time to expiration is less than or equal to 0, return 0.
445-
if not self.state['access_token'] or time.time() >= self.state['access_token_expires_at']:
446+
if not self.state['access_token'] or time.time() + 60 >= self.state['access_token_expires_at']:
446447
return 0
447448

448449
# else return the number of seconds until expiration.
449-
token_exp = int(self.state['access_token_expires_at'] - time.time())
450+
token_exp = int(self.state['access_token_expires_at'] - time.time() - 60)
450451

451452
# if needed check the refresh token.
452453
elif token_type == 'refresh_token':
453454

454455
# if the time to expiration is less than or equal to 0, return 0.
455-
if not self.state['refresh_token'] or time.time() >= self.state['refresh_token_expires_at']:
456+
if not self.state['refresh_token'] or time.time() + 60 >= self.state['refresh_token_expires_at']:
456457
return 0
457458

458459
# else return the number of seconds until expiration.
459-
token_exp = int(self.state['refresh_token_expires_at'] - time.time())
460+
token_exp = int(self.state['refresh_token_expires_at'] - time.time() - 60)
460461

461462
return token_exp
462463

463-
def _token_validation(self, nseconds: int = 5):
464+
def _token_validation(self, nseconds: int = 60):
464465
"""Checks if a token is valid.
465466
466467
Verify the current access token is valid for at least N seconds, and
@@ -573,6 +574,21 @@ def _make_request(self, method: str, endpoint: str, mode: str = None, params: di
573574
print("RESPONSE TEXT: {text}".format(text=response.text))
574575
print('-'*80)
575576

577+
if response.status_code == 400:
578+
raise NotNulError(message=response.text)
579+
elif response.status_code == 401:
580+
raise TknExpError(message=response.text)
581+
elif response.status_code == 403:
582+
raise ForbidError(message=response.text)
583+
elif response.status_code == 404:
584+
raise NotFndError(message=response.text)
585+
elif response.status_code == 429:
586+
raise ExdLmtError(message=response.text)
587+
elif response.status_code == 500 or response.status_code == 503:
588+
raise ServerError(message=response.text)
589+
elif response.status_code > 400:
590+
raise GeneralError(message=response.text)
591+
576592
def _validate_arguments(self, endpoint: str, parameter_name: str, parameter_argument: List[str]) -> bool:
577593
"""Validates arguments for an API call.
578594

td/exceptions.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
class TknExpError(Exception):
2+
"""Raise exception when refresh or access token is expired.
3+
4+
Args:
5+
Exception (Exception): The base python exception class
6+
"""
7+
def __init__(self, message):
8+
"""Print out message for this exception.
9+
10+
Args:
11+
message (str): Pass in the message returned by the server.
12+
"""
13+
self.message = message
14+
super().__init__(self.message)
15+
16+
class ExdLmtError(Exception):
17+
"""Raise exception when exceeding query limit of the server.
18+
19+
Args:
20+
Exception (Exception): The base python exception class
21+
"""
22+
def __init__(self, message):
23+
"""Print out message for this exception.
24+
25+
Args:
26+
message (str): Pass in the message returned by the server.
27+
"""
28+
self.message = message
29+
super().__init__(self.message)
30+
31+
class NotNulError(Exception):
32+
"""Raise exception when a null value is passed into non-null field.
33+
34+
Args:
35+
Exception (Exception): The base python exception class
36+
"""
37+
def __init__(self, message):
38+
"""Print out message for this exception.
39+
40+
Args:
41+
message (str): Pass in the message returned by the server.
42+
"""
43+
self.message = message
44+
super().__init__(self.message)
45+
46+
class ForbidError(Exception):
47+
"""Raise forbidden exception. This usually occurs when the app does
48+
not have access to the account.
49+
50+
Args:
51+
Exception (Exception): The base python exception class
52+
"""
53+
def __init__(self, message):
54+
"""Print out message for this exception.
55+
56+
Args:
57+
message (str): Pass in the message returned by the server.
58+
"""
59+
self.message = message
60+
super().__init__(self.message)
61+
62+
class NotFndError(Exception):
63+
"""Raise exception when criteria is not found.
64+
65+
Args:
66+
Exception (Exception): The base python exception class
67+
"""
68+
def __init__(self, message):
69+
"""Print out message for this exception.
70+
71+
Args:
72+
message (str): Pass in the message returned by the server.
73+
"""
74+
self.message = message
75+
super().__init__(self.message)
76+
77+
class ServerError(Exception):
78+
"""Raise exception when there is an error with the service or the server
79+
cannot provide response.
80+
81+
Args:
82+
Exception (Exception): The base python exception class
83+
"""
84+
def __init__(self, message):
85+
"""Print out message for this exception.
86+
87+
Args:
88+
message (str): Pass in the message returned by the server.
89+
"""
90+
self.message = message
91+
super().__init__(self.message)
92+
93+
class GeneralError(Exception):
94+
"""Raise exception for all other status code >400 errors which are not
95+
defined above.
96+
97+
Args:
98+
Exception (Exception): The base python exception class
99+
"""
100+
def __init__(self, message):
101+
"""Print out message for this exception.
102+
103+
Args:
104+
message (str): Pass in the message returned by the server.
105+
"""
106+
self.message = message
107+
super().__init__(self.message)

0 commit comments

Comments
 (0)