99class KDOneAPI :
1010 BASE_URL : str = "https://ilps.naviensmartcontrol.com:3000"
1111
12- def __init__ (self , username : str , password : str , base_url : str = BASE_URL ) -> None :
12+ def __init__ (self , username : str , password : str , base_url : str = BASE_URL , verbose = True ) -> None :
1313 self .username : str = username
1414 self .password : str = password
1515 self .complex_id : Optional [str ] = None
1616 self .base_url : str = base_url
1717 self .app_certify : Optional [str ] = None
1818 self .access_token : Optional [str ] = None
1919 self .refresh_token : Optional [str ] = None
20+ self .verbose = verbose
2021
2122 def _send_request (self , endpoint : str , data : Dict [str , Any ]) -> Dict [str , Any ]:
2223 request_key : str = random_key ()
2324 payload : str = encrypt_payload (request_key , json .dumps (data ))
2425
25- print (payload )
26+ if self .verbose :
27+ print (f"Sending request to { endpoint } with payload: { payload } and key: { request_key } " )
28+
2629 headers : Dict [str , str ] = {
2730 "Content-Type" : "text/plain" ,
2831 "User-Agent" : "homenet/10 CFNetwork/1568.200.51 Darwin/24.1.0" ,
@@ -32,21 +35,29 @@ def _send_request(self, endpoint: str, data: Dict[str, Any]) -> Dict[str, Any]:
3235 f"{ self .base_url } { endpoint } " , headers = headers , data = payload
3336 )
3437
38+ if self .verbose :
39+ print (f"Received response: { response .text } " )
3540 if response .status_code == 200 :
36- response_key : Optional [ str ] = response . headers . get ( "x-message-id" )
37- decrypted_data : str = decrypt_payload ( response_key , response .text )
38- response_data : Dict [ str , Any ] = json . loads ( decrypted_data )
41+ try :
42+ response_key : Optional [ str ] = response .headers . get ( "x-message-id" )
43+ decrypted_data : str = decrypt_payload ( response_key , response . text )
3944
40- error_code : str = response_data .get ("Error_Cd" , "" )
41- error_name : str = response_data .get ("Error_Nm" , "" )
45+ if self .verbose :
46+ print (f"Decrypted response: { decrypted_data } " )
47+ response_data : Dict [str , Any ] = json .loads (decrypted_data )
4248
43- if error_code != "0000" or error_name != "성공" :
44- raise ValueError ( f"Error { error_code } : { error_name } " )
49+ error_code : str = response_data . get ( "Error_Cd" , "" )
50+ error_name : str = response_data . get ( "Error_Nm" , " " )
4551
46- return response_data
52+ if error_code != "0000" or error_name != "성공" :
53+ raise ValueError (f"Error { error_code } : { error_name } " )
54+ except Exception as e :
55+ raise ValueError (f"An error occurred during decryption: { e } " )
4756 else :
4857 response .raise_for_status ()
4958
59+ return response_data
60+
5061 @staticmethod
5162 def get_complexes () -> List [Complex ]:
5263 response = requests .post (f"{ KDOneAPI .BASE_URL } /info/complex-list" )
@@ -74,9 +85,16 @@ def login(self, complex_id: str) -> Dict[str, Any]:
7485 "User_Pw" : self .password ,
7586 "App_Type" : "1" ,
7687 }
77- return self ._send_request ("/users/login2" , data )
88+ try :
89+ response_data = self ._send_request ("/users/login2" , data )
90+ return response_data
91+ except requests .exceptions .HTTPError as http_error :
92+ if http_error .response .status_code == 404 :
93+ self ._get_certification_code ()
94+ else :
95+ raise
7896
79- def get_certification_code (self ) -> Dict [str , Any ]:
97+ def _get_certification_code (self ) -> Dict [str , Any ]:
8098 if not self .complex_id :
8199 raise ValueError (
82100 "Complex ID not set. Call login() first or pass complex_id."
@@ -96,7 +114,7 @@ def certify(self, certify_number: str) -> Dict[str, Any]:
96114 "Complex" : self .complex_id ,
97115 "Certify_Number" : certify_number ,
98116 }
99- response_data = self ._send_request ("/users/certify " , data )
117+ response_data = self ._send_request ("/users/certification " , data )
100118
101119 # Save the App_Certify value after certification
102120 self .app_certify = certify_number
0 commit comments