Skip to content

Commit

Permalink
Handle value error on relogin, improved error messages, removed point…
Browse files Browse the repository at this point in the history
…less logging.
  • Loading branch information
RenierM26 committed Mar 11, 2021
1 parent e0a7027 commit 742c028
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 44 deletions.
66 changes: 23 additions & 43 deletions pyezviz/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ def _login(self):
req.raise_for_status()

except requests.HTTPError as err:
raise PyEzvizError("Can not login to API") from err
raise requests.HTTPError(err)

try:
json_result = req.json()

except ValueError as err:
raise PyEzvizError("Can't login response") from err
raise PyEzvizError("Can't decode login response") from err

if self._session.cookies.get("REDIRECTCOOKIE", domain=API_BASE_TLD):
print("Your region is incorrect!")
Expand Down Expand Up @@ -121,7 +121,7 @@ def _login(self):
)

except requests.HTTPError as err:
raise PyEzvizError("Can not login to API") from err
raise requests.HTTPError(err)

return True

Expand Down Expand Up @@ -150,9 +150,7 @@ def _login_api(self):
req.raise_for_status()

except requests.HTTPError as err:
raise PyEzvizError(
"Login error: Please check your username/password"
) from err
raise requests.HTTPError(err)

json_result = req.json()

Expand Down Expand Up @@ -184,10 +182,9 @@ def _api_get_pagelist(self, page_filter=None, json_key=None, max_retries=0):
if err.response.status_code == 401 or 504:
# session is wrong, need to relogin
self.login()
logging.info("Got %s, relogging", str(req.status_code))
return self._api_get_pagelist(page_filter, json_key, max_retries + 1)

raise PyEzvizError("Could not access Ezviz' API: " + str(err)) from err
raise requests.HTTPError(err)

if not req.text:
raise PyEzvizError("No data")
Expand Down Expand Up @@ -251,12 +248,9 @@ def get_alarminfo(self, serial, max_retries=0):
if err.response.status_code == 401 or 504:
# session is wrong, need to relogin
self.login()
logging.info(
"Got 302 or 401, relogging (max retries: %s)", str(max_retries)
)
return self.get_alarminfo(serial, max_retries + 1)

raise PyEzvizError("Could not access Ezviz' API: " + str(err)) from err
raise requests.HTTPError(err)

if req.text == "":
raise PyEzvizError("No data")
Expand All @@ -276,6 +270,8 @@ def get_alarminfo(self, serial, max_retries=0):

def _switch_status(self, serial, status_type, enable, max_retries=0):
"""Switch status on a device."""
if max_retries > MAX_RETRIES:
raise PyEzvizError("Can't gather proper data. Max retries exceeded.")

try:
req = self._session.put(
Expand All @@ -302,12 +298,9 @@ def _switch_status(self, serial, status_type, enable, max_retries=0):
if err.response.status_code == 401 or 504:
# session is wrong, need to relogin
self.login()
logging.info(
"Got 504 or 401, relogging (max retries: %s)", str(max_retries)
)
return self._switch_status(serial, status_type, enable, max_retries + 1)

raise PyEzvizError("Could not access Ezviz' API: " + str(err)) from err
raise requests.HTTPError(err)

try:
json_output = req.json()
Expand All @@ -329,6 +322,8 @@ def _switch_status(self, serial, status_type, enable, max_retries=0):

def sound_alarm(self, serial, enable=1, max_retries=0):
"""Sound alarm on a device."""
if max_retries > MAX_RETRIES:
raise PyEzvizError("Can't gather proper data. Max retries exceeded.")

try:
req = self._session.put(
Expand All @@ -351,12 +346,9 @@ def sound_alarm(self, serial, enable=1, max_retries=0):
if err.response.status_code == 401 or 504:
# session is wrong, need to relogin
self.login()
logging.info(
"Got 504 or 401, relogging (max retries: %s)", str(max_retries)
)
return self.sound_alarm(serial, enable, max_retries + 1)

raise PyEzvizError("Could not access Ezviz' API: " + str(err)) from err
raise requests.HTTPError(err)

try:
json_output = req.json()
Expand Down Expand Up @@ -533,7 +525,7 @@ def ptz_control(self, command, serial, action, speed=5):
req.raise_for_status()

except requests.HTTPError as err:
raise PyEzvizError("Could not access Ezviz' API: " + str(err)) from err
raise requests.HTTPError(err)

return req.text

Expand All @@ -553,15 +545,13 @@ def login(self):
req.raise_for_status()

except requests.HTTPError as err:
raise PyEzvizError(
"Could not access Ezviz login check API: " + str(err)
) from err
raise requests.HTTPError(err)

try:
response_json = req.json()

except ValueError:
self._login()
return self._login()

if response_json["success"] != "success":
self._login()
Expand Down Expand Up @@ -591,12 +581,9 @@ def data_report(self, serial, enable=1, max_retries=0):
if err.response.status_code == 401 or 504:
# session is wrong, need to relogin
self.login()
logging.info(
"Got 504 or 401, relogging (max retries: %s)", str(max_retries)
)
return self.data_report(serial, enable, max_retries + 1)

raise PyEzvizError("Could not access Ezviz' API: " + str(err)) from err
raise requests.HTTPError(err)

try:
json_output = req.json()
Expand Down Expand Up @@ -648,12 +635,11 @@ def api_set_defence_schedule(self, serial, schedule, enable, max_retries=0):
if err.response.status_code == 401 or 504:
# session is wrong, need to relogin
self.login()
logging.info("Error, got status code: %s", req.status_code)
return self.api_set_defence_schedule(
serial, schedule, enable, max_retries + 1
)

raise PyEzvizError("Could not access Ezviz' API: " + str(err)) from err
raise requests.HTTPError(err)

try:
json_output = req.json()
Expand Down Expand Up @@ -697,10 +683,9 @@ def api_set_defence_mode(self, mode: DefenseModeType, max_retries=0):
if err.response.status_code == 401 or 504:
# session is wrong, need to relogin
self.login()
logging.info("Error, got status code: %s", req.status_code)
return self.api_set_defence_mode(mode, max_retries + 1)
return self.api_set_defence_mode(DefenseModeType, max_retries + 1)

raise PyEzvizError("Could not access Ezviz' API: " + str(err)) from err
raise requests.HTTPError(err)

try:
json_output = req.json()
Expand Down Expand Up @@ -751,12 +736,9 @@ def detection_sensibility(self, serial, sensibility=3, type_value=3, max_retries
if err.response.status_code == 401 or 504:
# session is wrong, need to re-log-in
self.login()
logging.info("Got 401, relogging (max retries: %s)", str(max_retries))
return self.detection_sensibility(
serial, sensibility, type_value, max_retries + 1
)
return self.detection_sensibility(serial, sensibility, type_value, max_retries + 1)

raise PyEzvizError("Could not access Ezviz' API: " + str(err)) from err
raise requests.HTTPError(err)

try:
response_json = req.json()
Expand Down Expand Up @@ -792,12 +774,11 @@ def get_detection_sensibility(self, serial, type_value="0", max_retries=0):
if err.response.status_code == 401 or 504:
# session is wrong, need to re-log-in
self.login()
logging.info("Got 401, relogging (max retries: %s)", str(max_retries))
return self.get_detection_sensibility(
serial, type_value, max_retries + 1
)

raise PyEzvizError("Could not access Ezviz' API: " + str(err)) from err
raise requests.HTTPError(err)

try:
response_json = req.json()
Expand Down Expand Up @@ -849,10 +830,9 @@ def alarm_sound(self, serial, sound_type, enable=1, max_retries=0):
if err.response.status_code == 401 or 504:
# session is wrong, need to re-log-in
self.login()
logging.info("Got 401, relogging (max retries: %s)", str(max_retries))
return self.alarm_sound(serial, sound_type, enable, max_retries + 1)

raise PyEzvizError("Could not access Ezviz' API: " + str(err)) from err
raise requests.HTTPError(err)

return True

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name='pyezviz',
version="0.1.7.8",
version="0.1.7.9",
license='Apache Software License 2.0',
author='Pierre Ourdouille',
author_email='[email protected]',
Expand Down

0 comments on commit 742c028

Please sign in to comment.