Skip to content

Commit

Permalink
Add credentials path check
Browse files Browse the repository at this point in the history
  • Loading branch information
areed1192 committed Dec 9, 2020
1 parent 2ace9a8 commit de96af7
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions td/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ def __init__(self, client_id: str, redirect_uri: str, account_number: str = None
self.client_id = client_id
self.redirect_uri = redirect_uri
self.account_number = account_number

self.credentials_path = pathlib.Path(credentials_path)
self._redirect_code = None
self._td_utilities = TDUtilities()

if self.auth_flow == 'flask':
Expand Down Expand Up @@ -236,8 +236,8 @@ def login(self) -> bool:
{bool} -- Specifies whether it was successful or not.
"""

# if caching is enabled then attempt silent authentication.
if self._silent_sso():
# Only attempt silent SSO if the credential file exists.
if self.credentials_path.exists() and self._silent_sso():
self.authstate = True
return True
else:
Expand Down Expand Up @@ -382,7 +382,7 @@ def exchange_code_for_token(self, code: str, return_refresh_token: bool) -> dict
"""

# Parse the URL
url_dict = urllib.parse.parse_qs(self._redirect_code)
url_dict = urllib.parse.parse_qs(self.code)

# Grab the Code.
url_code = list(url_dict.values())[0][0]
Expand All @@ -391,11 +391,13 @@ def exchange_code_for_token(self, code: str, return_refresh_token: bool) -> dict
data = {
'grant_type': 'authorization_code',
'client_id': self.client_id + '@AMER.OAUTHAP',
'access_type': 'offline',
'code': url_code,
'redirect_uri': self.redirect_uri
}

if return_refresh_token:
data['access_type'] = 'offline'

# Make the request.
response = requests.post(
url="https://api.tdameritrade.com/v1/oauth2/token",
Expand Down Expand Up @@ -459,7 +461,7 @@ def validate_token(self) -> bool:
"message": "The credential file does not contain expiration times for your tokens, please go through the oAuth process."
}
)

return False

def _silent_sso(self) -> bool:
Expand Down Expand Up @@ -699,7 +701,9 @@ def _prepare_arguments_list(self, parameter_list: List) -> str:
### Usage:
----
>>> td_client._prepare_arguments_list(parameter_list = ['MSFT', 'SQ'])
>>> td_client._prepare_arguments_list(
parameter_list=['MSFT', 'SQ']
)
"""

return ','.join(parameter_list)
Expand Down Expand Up @@ -909,7 +913,6 @@ def get_instruments(self, cusip: str) -> Dict:
>>> td_client.get_instruments(
cusip='SomeCUSIPNumber'
)
"""

# build the params dictionary
Expand Down Expand Up @@ -946,9 +949,8 @@ def get_market_hours(self, markets: List[str], date: str) -> Dict:
### Usage:
----
>>> td_client.get_market_hours(markets = ['EQUITY'], date = '2019-10-19')
>>> td_client.get_market_hours(markets = ['EQUITY','FOREX'], date = '2019-10-19')
>>> td_client.get_market_hours(markets=['EQUITY'], date='2019-10-19')
>>> td_client.get_market_hours(markets=['EQUITY','FOREX'], date='2019-10-19')
"""

# validate argument
Expand Down

0 comments on commit de96af7

Please sign in to comment.