Skip to content

Commit

Permalink
added more logs
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrErohin committed Dec 14, 2023
1 parent ccee9d2 commit 5fce011
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
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="tplinkrouterc6u",
version="1.0.0",
version="1.1.0",
author="Alex Erohin",
author_email="[email protected]",
description="TP-Link Router API",
Expand Down
44 changes: 24 additions & 20 deletions tplinkrouterc6u/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ def __init__(self, host: str, password: str, username: str = 'admin', logger: Lo

self._encryption = EncryptionWrapper()

def test_connect(self) -> None:
def test_connect(self) -> bool:
try:
self.authorize()
return self.authorize()
except Exception as error:
if self._logger:
self._logger.error(error)
self._logger.error('TplinkRouter Integration Exception - {}'.format(error))
finally:
self.clear()

Expand Down Expand Up @@ -94,13 +94,16 @@ def authorize(self) -> bool:
try:
jsonData = response.json()

if 'data' not in jsonData or not jsonData['data']:
raise Exception('No data in response: ' + response.text)

encryptedResponseData = jsonData['data']
responseData = self._encryption.aes_decrypt(encryptedResponseData)

responseDict = json.loads(responseData)

if not responseDict['success']:
raise Exception(responseDict['errorcode'])
if 'success' not in responseDict or not responseDict['success']:
raise Exception('No data in response: ' + responseData)

self._stok = responseDict['data']['stok']
regex_result = re.search(
Expand All @@ -110,7 +113,7 @@ def authorize(self) -> bool:
return True
except (ValueError, KeyError, AttributeError) as e:
if self._logger:
self._logger.error("Couldn't fetch auth tokens! Response was: %s", response.text)
self._logger.error("TplinkRouter Integration Exception - Couldn't fetch auth tokens! Response was: %s", response.text)

return False

Expand Down Expand Up @@ -178,8 +181,8 @@ def _request_pwd(self, referer: str) -> None:

jsonData = response.json()

if not jsonData['success']:
raise Exception('Unkown error: ' + jsonData)
if 'success' not in jsonData or not jsonData['success']:
raise Exception('Unkown error for pwd: ' + response.text)

args = jsonData['data']['password']

Expand All @@ -200,8 +203,8 @@ def _request_seq(self, referer: str) -> None:

jsonData = response.json()

if not jsonData['success']:
raise Exception('Unkown error: ' + jsonData)
if 'success' not in jsonData or not jsonData['success']:
raise Exception('Unkown error for seq: ' + response.text)

self._seq = jsonData['data']['seq']
args = jsonData['data']['key']
Expand Down Expand Up @@ -246,7 +249,7 @@ def _request(self, callback: Callable):
self._seq = ''
self._pwdNN = ''
if self._logger:
self._logger.error(error)
self._logger.error('TplinkRouter Integration Exception - {}'.format(error))
finally:
self.clear()

Expand All @@ -265,29 +268,30 @@ def _get_data(self, path: str) -> dict | None:
verify=self._verify_ssl,
)

data = response.text
try:
json_response = response.json()

data = json_response['data']
data = self._encryption.aes_decrypt(data)
if 'data' not in json_response:
raise Exception("Router didn't respond with JSON - " + data)
data = self._encryption.aes_decrypt(json_response['data'])

json_response = json.loads(data)

if json_response['success']:
if 'success' in json_response and json_response['success']:
return json_response['data']
else:
if json_response['errorcode'] == 'timeout':
if 'errorcode' in json_response and json_response['errorcode'] == 'timeout':
if self._logger:
self._logger.info("Token timed out. Relogging on next scan")
self._logger.info("TplinkRouter Integration Exception - Token timed out. Relogging on next scan")
self._stok = ''
self._sysauth = ''
elif self._logger:
self._logger.error("An unknown error happened while fetching data")
self._logger.error("TplinkRouter Integration Exception - An unknown error happened while fetching data %s", data)
except ValueError:
if self._logger:
self._logger.error("Router didn't respond with JSON. Check if credentials are correct")
self._logger.error("TplinkRouter Integration Exception - Router didn't respond with JSON. Check if credentials are correct")

return None
raise Exception('An unknown response - ' + data)

def _send_data(self, path: str, data: str) -> None:
if self._logged is False:
Expand Down

0 comments on commit 5fce011

Please sign in to comment.