Added timeout parameter to Reqeusts calls, making it user adjustable. Added some more exception handling. Bumping version to 0.0.5

get_list_of_videos
Mark Cotton 2023-07-24 10:24:44 -06:00
parent b7b68d71e0
commit 0e150331b4
1 changed files with 209 additions and 44 deletions

View File

@ -1,5 +1,5 @@
""" Python client for Eagle Eye Networks APIv3 """ """ Python client for Eagle Eye Networks APIv3 """
__version__ = "0.0.4" __version__ = "0.0.5"
import json import json
@ -21,7 +21,7 @@ class EagleEyev3():
Class representing the EagleEyev3 client. Class representing the EagleEyev3 client.
""" """
__version__ = "0.0.4" __version__ = "0.0.5"
__all__ = ['EagleEyev3', 'Device', 'Camera'] __all__ = ['EagleEyev3', 'Device', 'Camera']
def __init__(self, config=None): def __init__(self, config=None):
@ -61,6 +61,18 @@ class EagleEyev3():
except FileNotFoundError as e: except FileNotFoundError as e:
logging.warn("self.lazy_login is set to {self.lazy_login} but could not find .lazy_login file to load") logging.warn("self.lazy_login is set to {self.lazy_login} but could not find .lazy_login file to load")
# for use with request calls, can be an int, tuple, or None
# preferred would be a tuple with the first value is time to connect, second is time to first byte
# https://requests.readthedocs.io/en/latest/user/advanced/#timeouts
# called by self._get_timeout_values
self.timeout_values = {
'default': (3,5),
'login': (5, 30),
'logout': (5, 15),
'list_of_events': (6, 20),
'live_preview': (3, 5)
}
def _load_vars_from_settings(self, config={}): def _load_vars_from_settings(self, config={}):
""" """
Load variables from the settings module. Load variables from the settings module.
@ -77,7 +89,6 @@ class EagleEyev3():
self.redirect_uri = f"{self.server_protocol}://{self.server_host}:{self.server_port}/{self.server_path}" self.redirect_uri = f"{self.server_protocol}://{self.server_host}:{self.server_port}/{self.server_path}"
def _save_access_token(self): def _save_access_token(self):
with open(".lazy_login", "w") as json_file: with open(".lazy_login", "w") as json_file:
json.dump({ json.dump({
@ -96,11 +107,9 @@ class EagleEyev3():
self.get_base_url(cascade=True) self.get_base_url(cascade=True)
def time_now(self): def time_now(self):
return datetime.now(tz=self.user_tz_obj).isoformat(timespec='milliseconds') return datetime.now(tz=self.user_tz_obj).isoformat(timespec='milliseconds')
def time_before(self, ts=None, hours=6): def time_before(self, ts=None, hours=6):
if ts == None: if ts == None:
ts = datetime.now(tz=self.user_tz_obj) ts = datetime.now(tz=self.user_tz_obj)
@ -110,8 +119,6 @@ class EagleEyev3():
return (ts - timedelta(hours=hours)).isoformat(timespec='milliseconds') return (ts - timedelta(hours=hours)).isoformat(timespec='milliseconds')
def login_tokens(self, code=None, cascade=True): def login_tokens(self, code=None, cascade=True):
""" """
Obtains login tokens using the authorization code. Obtains login tokens using the authorization code.
@ -128,7 +135,7 @@ class EagleEyev3():
url = baseUrl + pathUrl url = baseUrl + pathUrl
# Send a POST request to obtain login tokens # Send a POST request to obtain login tokens
response = requests.post(url, auth=(self.client_id, self.client_secret)) response = requests.post(url, auth=(self.client_id, self.client_secret), timeout=self._get_timeout_values('login'))
response_json = response.json() response_json = response.json()
logging.info(f"{response.status_code} in login_tokens") logging.info(f"{response.status_code} in login_tokens")
@ -174,7 +181,7 @@ class EagleEyev3():
} }
# Send a POST request to obtain the base URL # Send a POST request to obtain the base URL
response = requests.post(url, json=payload, headers=headers) response = requests.post(url, json=payload, headers=headers, timeout=self._get_timeout_values('logout'))
response_json = response.json() response_json = response.json()
logging.info(f"{response.status_code} in logout") logging.info(f"{response.status_code} in logout")
@ -210,12 +217,29 @@ class EagleEyev3():
"Accept": "application/json" "Accept": "application/json"
} }
try:
# Send a GET request to obtain the base URL # Send a GET request to obtain the base URL
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers, timeout=self._get_timeout_values('base_url'))
response_json = response.json() response_json = response.json()
logging.info(f"{response.status_code} in get_base_url") logging.info(f"{response.status_code} in get_base_url")
except requests.exceptions.Timeout:
logging.warn(f"timeout expired for {self.id} get_base_url()")
return {
"success": False,
"response_http_status": 0,
"data": None
}
except requests.exceptions.RequestException as e:
logging.warn(e)
return {
"success": False,
"response_http_status": 0,
"data": None
}
if response.status_code == 200: if response.status_code == 200:
success = True success = True
if 'httpsBaseUrl' in response_json and 'hostname' in response_json['httpsBaseUrl']: if 'httpsBaseUrl' in response_json and 'hostname' in response_json['httpsBaseUrl']:
@ -244,12 +268,29 @@ class EagleEyev3():
"Accept": "application/json" "Accept": "application/json"
} }
try:
# Send a GET request to obtain the current user information # Send a GET request to obtain the current user information
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers, timeout=self._get_timeout_values('curent_user'))
response_json = response.json() response_json = response.json()
logging.info(f"{response.status_code} in get_current_user") logging.info(f"{response.status_code} in get_current_user")
except requests.exceptions.Timeout:
logging.warn(f"timeout expired for {self.id} get_current_user()")
return {
"success": False,
"response_http_status": 0,
"data": None
}
except requests.exceptions.RequestException as e:
logging.warn(e)
return {
"success": False,
"response_http_status": 0,
"data": None
}
if response.status_code == 200: if response.status_code == 200:
success = True success = True
self.current_user = response_json self.current_user = response_json
@ -276,11 +317,28 @@ class EagleEyev3():
"Accept": "application/json" "Accept": "application/json"
} }
response = requests.get(url, headers=headers) try:
response = requests.get(url, headers=headers, timeout=self._get_timeout_values('list_of_users'))
response_json = response.json() response_json = response.json()
logging.info(f"{response.status_code} in get_list_of_users") logging.info(f"{response.status_code} in get_list_of_users")
except requests.exceptions.Timeout:
logging.warn(f"timeout expired for {self.id} get_list_of_users()")
return {
"success": False,
"response_http_status": 0,
"data": None
}
except requests.exceptions.RequestException as e:
logging.warn(e)
return {
"success": False,
"response_http_status": 0,
"data": None
}
if response.status_code == 200: if response.status_code == 200:
success = True success = True
self.users = [i for i in response_json['results']] self.users = [i for i in response_json['results']]
@ -306,11 +364,28 @@ class EagleEyev3():
"Accept": "application/json" "Accept": "application/json"
} }
response = requests.get(url, headers=headers) try:
response = requests.get(url, headers=headers, timeout=self._get_timeout_values('list_of_cameras'))
response_json = response.json() response_json = response.json()
logging.info(f"{response.status_code} in get_list_of_cameras") logging.info(f"{response.status_code} in get_list_of_cameras")
except requests.exceptions.Timeout:
logging.warn(f"timeout expired for {self.id} get_list_of_cameras()")
return {
"success": False,
"response_http_status": 0,
"data": None
}
except requests.exceptions.RequestException as e:
logging.warn(e)
return {
"success": False,
"response_http_status": 0,
"data": None
}
if response.status_code == 200: if response.status_code == 200:
success = True success = True
self.cameras = [ self.cameras = [
@ -346,11 +421,28 @@ class EagleEyev3():
"Accept": "application/json" "Accept": "application/json"
} }
response = requests.get(url, headers=headers) try:
response = requests.get(url, headers=headers, timeout=self._get_timeout_values('list_of_bridges'))
response_json = response.json() response_json = response.json()
logging.info(f"{response.status_code} in get_list_of_bridges") logging.info(f"{response.status_code} in get_list_of_bridges")
except requests.exceptions.Timeout:
logging.warn(f"timeout expired for {self.id} get_list_of_bridges()")
return {
"success": False,
"response_http_status": 0,
"data": None
}
except requests.exceptions.RequestException as e:
logging.warn(e)
return {
"success": False,
"response_http_status": 0,
"data": None
}
if response.status_code == 200: if response.status_code == 200:
success = True success = True
self.bridges = [i for i in response_json['results']] self.bridges = [i for i in response_json['results']]
@ -376,11 +468,28 @@ class EagleEyev3():
"Accept": "application/json" "Accept": "application/json"
} }
response = requests.get(url, headers=headers) try:
response = requests.get(url, headers=headers, timeout=self._get_timeout_values('list_of_switches'))
response_json = response.json() response_json = response.json()
logging.info(f"{response.status_code} in get_list_of_switches") logging.info(f"{response.status_code} in get_list_of_switches")
except requests.exceptions.Timeout:
logging.warn(f"timeout expired for {self.id} get_list_of_switches()")
return {
"success": False,
"response_http_status": 0,
"data": None
}
except requests.exceptions.RequestException as e:
logging.warn(e)
return {
"success": False,
"response_http_status": 0,
"data": None
}
if response.status_code == 200: if response.status_code == 200:
success = True success = True
self.switches = [i for i in response_json['results']] self.switches = [i for i in response_json['results']]
@ -406,11 +515,28 @@ class EagleEyev3():
"Accept": "application/json" "Accept": "application/json"
} }
response = requests.get(url, headers=headers) try:
response = requests.get(url, headers=headers, timeout=self._get_timeout_values('list_of_available_devices'))
response_json = response.json() response_json = response.json()
logging.info(f"{response.status_code} in get_list_of_available_devices") logging.info(f"{response.status_code} in get_list_of_available_devices")
except requests.exceptions.Timeout:
logging.warn(f"timeout expired for {self.id} get_list_of_available_devices()")
return {
"success": False,
"response_http_status": 0,
"data": None
}
except requests.exceptions.RequestException as e:
logging.warn(e)
return {
"success": False,
"response_http_status": 0,
"data": None
}
if response.status_code == 200: if response.status_code == 200:
success = True success = True
else: else:
@ -435,11 +561,28 @@ class EagleEyev3():
"Accept": "application/json" "Accept": "application/json"
} }
response = requests.get(url, headers=headers) try:
response = requests.get(url, headers=headers, timeout=self._get_timeout_values('list_of_multi_cameras'))
response_json = response.json() response_json = response.json()
logging.info(f"{response.status_code} in get_list_of_multi_cameras") logging.info(f"{response.status_code} in get_list_of_multi_cameras")
except requests.exceptions.Timeout:
logging.warn(f"timeout expired for {self.id} get_list_of_multi_cameras()")
return {
"success": False,
"response_http_status": 0,
"data": None
}
except requests.exceptions.RequestException as e:
logging.warn(e)
return {
"success": False,
"response_http_status": 0,
"data": None
}
if response.status_code == 200: if response.status_code == 200:
success = True success = True
else: else:
@ -464,11 +607,28 @@ class EagleEyev3():
"Accept": "application/json" "Accept": "application/json"
} }
response = requests.get(url, headers=headers) try:
response = requests.get(url, headers=headers, timeout=self._get_timeout_values('list_of_feeds'))
response_json = response.json() response_json = response.json()
logging.info(f"{response.status_code} in get_list_of_feeds") logging.info(f"{response.status_code} in get_list_of_feeds")
except requests.exceptions.Timeout:
logging.warn(f"timeout expired for {self.id} get_list_of_feeds()")
return {
"success": False,
"response_http_status": 0,
"data": None
}
except requests.exceptions.RequestException as e:
logging.warn(e)
return {
"success": False,
"response_http_status": 0,
"data": None
}
if response.status_code == 200: if response.status_code == 200:
success = True success = True
else: else:
@ -480,7 +640,6 @@ class EagleEyev3():
"data": response_json "data": response_json
} }
def get_camera_by_id(self, esn): def get_camera_by_id(self, esn):
found_camera = None found_camera = None
for camera in self.cameras: for camera in self.cameras:
@ -494,12 +653,17 @@ class EagleEyev3():
logging.debug(f"returning camera {camera} for search query {esn}") logging.debug(f"returning camera {camera} for search query {esn}")
return camera return camera
def _get_timeout_values(self, name='default'):
if name in self.timeout_values:
return self.timeout_values[name]
else:
return self.timeout_values['default']
class Device(): class Device():
def __init__(self, id=None, name=None, status=None, account_id=None, user_base_url=None, een_instance=None): def __init__(self, id=None, name=None, status=dict(), account_id=None, user_base_url=None, een_instance=None):
self.id = id self.id = id
self.name = name self.name = name
@ -541,7 +705,7 @@ class Device():
class Camera(Device): class Camera(Device):
def __init__(self, id=None, name=None, status=None, account_id=None, bridge_id=None, user_base_url=None, een_instance=None): def __init__(self, id=None, name=None, status=dict(), account_id=None, bridge_id=None, user_base_url=None, een_instance=None):
super().__init__(id=id, name=name, status=status, account_id=account_id, user_base_url=user_base_url, een_instance=een_instance) super().__init__(id=id, name=name, status=status, account_id=account_id, user_base_url=user_base_url, een_instance=een_instance)
self.bridge_id = bridge_id self.bridge_id = bridge_id
self.previews = [] self.previews = []
@ -576,7 +740,7 @@ class Camera(Device):
} }
try: try:
response = requests.get(url, headers=headers, timeout=(3, 5)) response = requests.get(url, headers=headers, timeout=self.een_instance._get_timeout_values('list_of_events'))
response_json = response.json() response_json = response.json()
logging.debug(f"{response.status_code} returned from {url} with {headers} and {response.text}") logging.debug(f"{response.status_code} returned from {url} with {headers} and {response.text}")
@ -634,12 +798,13 @@ class Camera(Device):
} }
try: try:
response = requests.get(url, headers=headers, timeout=(3, 5)) response = requests.get(url, headers=headers, timeout=self.een_instance._get_timeout_values('live_preview'))
logging.info(f"{response.status_code} in get_live_preview") logging.info(f"{response.status_code} in get_live_preview")
except requests.exceptions.Timeout: except requests.exceptions.Timeout:
logging.warn(f"timeout expired for {self.id} get_live_preview()") logging.warn(f"timeout expired for {self.id} get_live_preview()")
response = None response = None
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
logging.warn(e) logging.warn(e)
response = None response = None