66import requests
77import urllib .parse
88
9- from . import defaults
10-
119from typing import Any
1210from typing import Dict
1311from typing import List
1412from typing import Union
1513from typing import Optional
1614
15+ from td .utils import StatePath
16+ from td .utils import TDUtilities
17+
1718from td .orders import Order
1819from td .orders import OrderLeg
19- from td .defaults import StatePath
2020from td .stream import TDStreamerClient
2121from td .option_chain import OptionChain
22- from td .fields import VALID_CHART_VALUES
23- from td .fields import ENDPOINT_ARGUMENTS
24- from td .app .auth import FlaskTDAuth
22+
23+ from td .enums import VALID_CHART_VALUES
24+ from td .enums import ENDPOINT_ARGUMENTS
25+
2526from td .oauth import run
2627from td .oauth import shutdown
27- from td .exceptions import TknExpError , ExdLmtError , NotNulError , ForbidError , NotFndError , ServerError , GeneralError
28+ from td .app .auth import FlaskTDAuth
29+
30+ from td .exceptions import TknExpError
31+ from td .exceptions import ExdLmtError
32+ from td .exceptions import NotNulError
33+ from td .exceptions import ForbidError
34+ from td .exceptions import NotFndError
35+ from td .exceptions import ServerError
36+ from td .exceptions import GeneralError
2837
2938class TDClient ():
3039
@@ -105,6 +114,7 @@ def __init__(self, client_id: str, redirect_uri: str, account_number: str = None
105114 self .account_number = account_number
106115 self .credentials_path = StatePath (credentials_file = credentials_path )
107116 self ._redirect_code = None
117+ self ._td_utilities = TDUtilities ()
108118
109119 if self .auth_flow == 'flask' :
110120 self ._flask_app = FlaskTDAuth (
@@ -546,8 +556,8 @@ def _make_request(self, method: str, endpoint: str, mode: str = None, params: di
546556 else :
547557 order_id = ''
548558
559+ # If it's okay and we need details, then add them.
549560 if response .ok and order_details :
550-
551561 response_dict = {
552562 'order_id' :order_id ,
553563 'headers' :response_headers ,
@@ -559,19 +569,11 @@ def _make_request(self, method: str, endpoint: str, mode: str = None, params: di
559569
560570 return response_dict
561571
572+ # If it's okay and no details.
562573 elif response .ok :
563-
564574 return response .json ()
565575
566576 else :
567-
568- print ('=' * 80 )
569- print ("RESPONSE STATUS CODE: {status_code}" .format (status_code = status_code ))
570- print ("RESPONSE URL: {url}" .format (url = response .url ))
571- print ("RESPONSE HEADERS: {headers}" .format (headers = response .headers ))
572- print ("RESPONSE PARAMS: {params}" .format (params = response .links ))
573- print ("RESPONSE TEXT: {text}" .format (text = response .text ))
574- print ('-' * 80 )
575577
576578 if response .status_code == 400 :
577579 raise NotNulError (message = response .text )
@@ -745,17 +747,13 @@ def get_price_history(self, symbol: str, period_type:str = None, period: str = N
745747 elif (not start_date and not end_date and period ):
746748
747749 # Attempt to grab the key, if it fails we know there is an error.
748- try :
750+ # check if the period is valid.
751+ if int (period ) in VALID_CHART_VALUES [frequency_type ][period_type ]:
752+ True
753+ else :
754+ raise IndexError ('Invalid Period.' )
749755
750- # check if the period is valid.
751- if period in VALID_CHART_VALUES [frequency_type ][int (period_type )]:
752- True
753- else :
754- raise IndexError ('Invalid Period.' )
755- except :
756- raise KeyError ('Invalid Frequency Type or Period Type you passed through is not valid' )
757-
758- if frequency_type == 'minute' and frequency not in ['1' , '5' , '10' , '15' , '30' ]:
756+ if frequency_type == 'minute' and int (frequency ) not in [1 , 5 , 10 , 15 , 30 ]:
759757 raise ValueError ('Invalid Minute Frequency, must be 1,5,10,15,30' )
760758
761759 # build the params dictionary
@@ -2027,7 +2025,8 @@ def create_streaming_session(self) -> TDStreamerClient:
20272025
20282026 # Grab the Streamer Info.
20292027 userPrincipalsResponse = self .get_user_principals (
2030- fields = ['streamerConnectionInfo' ,'streamerSubscriptionKeys' ,'preferences' ,'surrogateIds' ])
2028+ fields = ['streamerConnectionInfo' ,'streamerSubscriptionKeys' ,'preferences' ,'surrogateIds' ]
2029+ )
20312030
20322031
20332032 # Grab the timestampe.
@@ -2038,7 +2037,8 @@ def create_streaming_session(self) -> TDStreamerClient:
20382037
20392038 # Parse the token timestamp.
20402039 tokenTimeStampAsMs = self ._create_token_timestamp (
2041- token_timestamp = tokenTimeStamp )
2040+ token_timestamp = tokenTimeStamp
2041+ )
20422042
20432043 # Define our Credentials Dictionary used for authentication.
20442044 credentials = {
0 commit comments