1+ from typing import (
2+ Dict ,
3+ List ,
4+ Union
5+ )
16import requests
7+ from loguru import logger
8+ from reolink_api .alarm import AlarmAPIMixin
9+ from reolink_api .device import DeviceAPIMixin
10+ from reolink_api .display import DisplayAPIMixin
11+ from reolink_api .download import DownloadAPIMixin
12+ from reolink_api .image import ImageAPIMixin
13+ from reolink_api .motion import MotionAPIMixin
14+ from reolink_api .network import NetworkAPIMixin
15+ from reolink_api .ptz import PtzAPIMixin
16+ from reolink_api .recording import RecordingAPIMixin
217from reolink_api .resthandle import Request
3- from .alarm import AlarmAPIMixin
4- from .device import DeviceAPIMixin
5- from .display import DisplayAPIMixin
6- from .download import DownloadAPIMixin
7- from .image import ImageAPIMixin
8- from .motion import MotionAPIMixin
9- from .network import NetworkAPIMixin
10- from .ptz import PtzAPIMixin
11- from .recording import RecordingAPIMixin
12- from .system import SystemAPIMixin
13- from .user import UserAPIMixin
14- from .zoom import ZoomAPIMixin
18+ from reolink_api .system import SystemAPIMixin
19+ from reolink_api .user import UserAPIMixin
20+ from reolink_api .zoom import ZoomAPIMixin
1521
1622
1723class APIHandler (AlarmAPIMixin ,
@@ -35,7 +41,7 @@ class APIHandler(AlarmAPIMixin,
3541 All Code will try to follow the PEP 8 standard as described here: https://www.python.org/dev/peps/pep-0008/
3642 """
3743
38- def __init__ (self , ip : str , username : str , password : str , https = False , ** kwargs ):
44+ def __init__ (self , ip : str , username : str , password : str , https : bool = False , ** kwargs ):
3945 """
4046 Initialise the Camera API Handler (maps api calls into python)
4147 :param ip:
@@ -63,21 +69,22 @@ def login(self) -> bool:
6369 body = [{"cmd" : "Login" , "action" : 0 ,
6470 "param" : {"User" : {"userName" : self .username , "password" : self .password }}}]
6571 param = {"cmd" : "Login" , "token" : "null" }
66- response = Request .post (self .url , data = body , params = param )
72+ response = Request .post (self .url , data = body , params = param ) # type: requests.Response
6773 if response is not None :
6874 data = response .json ()[0 ]
6975 code = data ["code" ]
7076 if int (code ) == 0 :
7177 self .token = data ["value" ]["Token" ]["name" ]
72- print ("Login success" )
78+ logger . debug ("Login success" )
7379 return True
74- print (self .token )
80+ logger . debug (self .token )
7581 return False
7682 else :
77- print ("Failed to login\n Status Code:" , response .status_code )
83+ # Response object was NoneType (empty)
84+ logger .warning ("Failed to login\n Error in request." )
7885 return False
7986 except Exception as e :
80- print ("Error Login\n " , e )
87+ logger . error ("Error Login\n " , e )
8188 raise
8289
8390 def logout (self ) -> bool :
@@ -88,13 +95,12 @@ def logout(self) -> bool:
8895 try :
8996 data = [{"cmd" : "Logout" , "action" : 0 }]
9097 self ._execute_command ('Logout' , data )
91- # print(ret)
9298 return True
9399 except Exception as e :
94- print ("Error Logout\n " , e )
100+ logger . error ("Error Logout\n " , e )
95101 return False
96102
97- def _execute_command (self , command , data , multi = False ):
103+ def _execute_command (self , command : str , data : List [ Dict ] , multi : bool = False ) -> Union [ bool , Dict ] :
98104 """
99105 Send a POST request to the IP camera with given data.
100106 :param command: name of the command to send
@@ -122,12 +128,12 @@ def _execute_command(self, command, data, multi=False):
122128 f .write (req .content )
123129 return True
124130 else :
125- print (f'Error received: { req .status_code } ' )
131+ logger . error (f'Error received: { req .status_code } ' )
126132 return False
127133
128134 else :
129135 response = Request .post (self .url , data = data , params = params )
130- return response .json ()
136+ return response .json ()
131137 except Exception as e :
132- print (f"Command { command } failed: { e } " )
138+ logger . error (f"Command { command } failed: { e } " )
133139 raise
0 commit comments