Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
areed1192 committed Jul 20, 2020
2 parents c0528db + fce3be3 commit ac7bdb1
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,4 @@ pypirc
*State.json
tests/parse_level_two.py
certs/
td/td_state.json
28 changes: 22 additions & 6 deletions td/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
from td.app.auth import FlaskTDAuth
from td.oauth import run
from td.oauth import shutdown

from td.exceptions import TknExpError, ExdLmtError, NotNulError, \
ForbidError, NotFndError, ServerError, GeneralError

class TDClient():

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

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

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

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

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

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

return token_exp

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

if response.status_code == 400:
raise NotNulError(message=response.text)
elif response.status_code == 401:
raise TknExpError(message=response.text)
elif response.status_code == 403:
raise ForbidError(message=response.text)
elif response.status_code == 404:
raise NotFndError(message=response.text)
elif response.status_code == 429:
raise ExdLmtError(message=response.text)
elif response.status_code == 500 or response.status_code == 503:
raise ServerError(message=response.text)
elif response.status_code > 400:
raise GeneralError(message=response.text)

def _validate_arguments(self, endpoint: str, parameter_name: str, parameter_argument: List[str]) -> bool:
"""Validates arguments for an API call.
Expand Down
107 changes: 107 additions & 0 deletions td/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
class TknExpError(Exception):
"""Raise exception when refresh or access token is expired.
Args:
Exception (Exception): The base python exception class
"""
def __init__(self, message):
"""Print out message for this exception.
Args:
message (str): Pass in the message returned by the server.
"""
self.message = message
super().__init__(self.message)

class ExdLmtError(Exception):
"""Raise exception when exceeding query limit of the server.
Args:
Exception (Exception): The base python exception class
"""
def __init__(self, message):
"""Print out message for this exception.
Args:
message (str): Pass in the message returned by the server.
"""
self.message = message
super().__init__(self.message)

class NotNulError(Exception):
"""Raise exception when a null value is passed into non-null field.
Args:
Exception (Exception): The base python exception class
"""
def __init__(self, message):
"""Print out message for this exception.
Args:
message (str): Pass in the message returned by the server.
"""
self.message = message
super().__init__(self.message)

class ForbidError(Exception):
"""Raise forbidden exception. This usually occurs when the app does
not have access to the account.
Args:
Exception (Exception): The base python exception class
"""
def __init__(self, message):
"""Print out message for this exception.
Args:
message (str): Pass in the message returned by the server.
"""
self.message = message
super().__init__(self.message)

class NotFndError(Exception):
"""Raise exception when criteria is not found.
Args:
Exception (Exception): The base python exception class
"""
def __init__(self, message):
"""Print out message for this exception.
Args:
message (str): Pass in the message returned by the server.
"""
self.message = message
super().__init__(self.message)

class ServerError(Exception):
"""Raise exception when there is an error with the service or the server
cannot provide response.
Args:
Exception (Exception): The base python exception class
"""
def __init__(self, message):
"""Print out message for this exception.
Args:
message (str): Pass in the message returned by the server.
"""
self.message = message
super().__init__(self.message)

class GeneralError(Exception):
"""Raise exception for all other status code >400 errors which are not
defined above.
Args:
Exception (Exception): The base python exception class
"""
def __init__(self, message):
"""Print out message for this exception.
Args:
message (str): Pass in the message returned by the server.
"""
self.message = message
super().__init__(self.message)

0 comments on commit ac7bdb1

Please sign in to comment.