1- import json
2- from datetime import datetime
1+ import requests
32import logging
3+ import json
4+ from datetime import datetime , timedelta
45from config import APIConfig
56
67class APIClient :
78 def __init__ (self ):
8- self .config = APIConfig
99 self .session = requests .Session ()
10- self .logger = logging .getLogger (__name__ )
11- logging .basicConfig (** self .config .LOGGING_CONFIG )
10+ self .setup_logging ()
11+
12+ if not self .validate_credentials ():
13+ logging .error ("Invalid or missing credentials. Please check your configuration." )
14+ exit (1 )
15+
1216 self .token_info = self .load_token () or self .authenticate ()
1317
18+ def setup_logging (self ):
19+ logging .basicConfig (** APIConfig .LOGGING_CONFIG )
20+ self .logger = logging .getLogger (__name__ )
21+
22+ def validate_credentials (self ):
23+ return all ([APIConfig .APP_KEY , APIConfig .APP_SECRET , APIConfig .CALLBACK_URL ])
24+
1425 def authenticate (self ):
15- """ Authenticates with the API and stores the new token information. """
16- self .logger .info ("Authenticating with API to retrieve new tokens." )
26+ """Authenticate with the API and store the new token information."""
1727 data = {
1828 'grant_type' : 'client_credentials' ,
19- 'client_id' : self . config .APP_KEY ,
20- 'client_secret' : self . config .APP_SECRET
29+ 'client_id' : APIConfig .APP_KEY ,
30+ 'client_secret' : APIConfig .APP_SECRET
2131 }
22- response = self .session .post (f"{ self . config .API_BASE_URL } /v1/oauth/token" , data = data )
32+ response = self .session .post (f"{ APIConfig .API_BASE_URL } /v1/oauth/token" , data = data )
2333 response .raise_for_status ()
2434 token_data = response .json ()
2535 self .save_token (token_data )
2636 return token_data
2737
2838 def save_token (self , token_data ):
29- """ Saves the token data securely to a file. """
39+ """Saves the token data securely to a file."""
3040 token_data ['expires_at' ] = (datetime .now () + timedelta (seconds = token_data ['expires_in' ])).isoformat ()
3141 with open ('token_data.json' , 'w' ) as f :
3242 json .dump (token_data , f )
3343 self .logger .info ("Token data saved successfully." )
3444
3545 def load_token (self ):
36- """ Loads the token data from a file if it is still valid. """
46+ """Loads the token data from a file if it is still valid."""
3747 try :
3848 with open ('token_data.json' , 'r' ) as f :
3949 token_data = json .load (f )
@@ -45,8 +55,8 @@ def load_token(self):
4555 return None
4656
4757 def make_request (self , method , endpoint , ** kwargs ):
48- """ Makes an HTTP request using the authenticated session. """
49- url = f"{ self . config .API_BASE_URL } { endpoint } "
58+ """Makes an HTTP request using the authenticated session."""
59+ url = f"{ APIConfig .API_BASE_URL } { endpoint } "
5060 response = self .session .request (method , url , ** kwargs )
5161 if response .status_code == 401 : # Token expired
5262 self .logger .warning ("Token expired. Refreshing token..." )
0 commit comments