1111
1212
1313class APIClient :
14- def __init__ (self ):
14+ def __init__ (self , initials ):
15+ self .initials = initials
1516 self .account_numbers = None
16- self .config = APIConfig
17+ self .config = APIConfig ( self . initials )
1718 self .session = requests .Session ()
1819 self .setup_logging ()
1920 self .token_info = self .load_token ()
@@ -23,7 +24,7 @@ def __init__(self):
2324 self .manual_authorization_flow ()
2425
2526 def setup_logging (self ):
26- logging .basicConfig (** APIConfig .LOGGING_CONFIG )
27+ logging .basicConfig (** self . config .LOGGING_CONFIG )
2728 self .logger = logging .getLogger (__name__ )
2829
2930 def ensure_valid_token (self ):
@@ -42,7 +43,7 @@ def ensure_valid_token(self):
4243 def manual_authorization_flow (self ):
4344 """ Handle the manual steps required to get the authorization code from the user. """
4445 self .logger .info ("Starting manual authorization flow." )
45- auth_url = f"{ APIConfig . API_BASE_URL } /v1/oauth/authorize?client_id={ APIConfig . APP_KEY } &redirect_uri={ APIConfig .CALLBACK_URL } &response_type=code"
46+ auth_url = f"{ self . config . API_BASE_URL } /v1/oauth/authorize?client_id={ self . config . APP_KEY } &redirect_uri={ self . config .CALLBACK_URL } &response_type=code"
4647 webbrowser .open (auth_url )
4748 self .logger .info (f"Please authorize the application by visiting: { auth_url } " )
4849 response_url = ColorPrint .input (
@@ -91,26 +92,26 @@ def refresh_access_token(self):
9192 def save_token (self , token_data ):
9293 """ Save token data securely. """
9394 token_data ['expires_at' ] = (datetime .now () + timedelta (seconds = token_data ['expires_in' ])).isoformat ()
94- with open ('token_data .json' , 'w' ) as f :
95+ with open (f'schwab_token_data_ { self . initials } .json' , 'w' ) as f :
9596 json .dump (token_data , f )
9697 self .logger .info ("Token data saved successfully." )
9798
9899 def load_token (self ):
99100 """ Load token data. """
100101 try :
101- with open ('token_data .json' , 'r' ) as f :
102+ with open (f'schwab_token_data_ { self . initials } .json' , 'r' ) as f :
102103 token_data = json .load (f )
103104 return token_data
104105 except Exception as e :
105106 self .logger .warning (f"Loading token failed: { e } " )
106107 return None
107108
108109 def validate_token (self , force = False ):
109- """ Validate the current token's validity . """
110- print (self .token_info ['expires_at' ])
111- print (datetime .now ())
112- print (datetime .fromisoformat (self .token_info ['expires_at' ]))
113- print (datetime .now () < datetime .fromisoformat (self .token_info ['expires_at' ]))
110+ """ Validate the current token. """
111+ # print(self.token_info['expires_at'])
112+ # print(datetime.now())
113+ # print(datetime.fromisoformat(self.token_info['expires_at']))
114+ # print(datetime.now() < datetime.fromisoformat(self.token_info['expires_at']))
114115 if self .token_info and datetime .now () < datetime .fromisoformat (self .token_info ['expires_at' ]):
115116 print (f"Token expires in { datetime .fromisoformat (self .token_info ['expires_at' ]) - datetime .now ()} seconds" )
116117 return True
0 commit comments