From 1684f3629a0a5beba74390dff7028c8fe704e9dc Mon Sep 17 00:00:00 2001 From: Tim Van Laer Date: Thu, 18 Jul 2024 11:31:45 +0200 Subject: [PATCH] Fix linting issues --- huawei_lte_api/Session.py | 4 ---- huawei_lte_api/api/App.py | 14 ++++++++------ huawei_lte_api/api/User.py | 24 ++++++++++++------------ 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/huawei_lte_api/Session.py b/huawei_lte_api/Session.py index ec892f8..3fd5306 100644 --- a/huawei_lte_api/Session.py +++ b/huawei_lte_api/Session.py @@ -6,7 +6,6 @@ from types import TracebackType from typing import Any, Callable, Dict, List, Optional, Tuple, TypeVar, Union, Type, cast from urllib.parse import urlparse, urlunparse -from time import sleep import requests import xmltodict @@ -244,9 +243,6 @@ def _post(self, else: headers['__RequestVerificationToken'] = self.request_verification_tokens[0] - if is_json is True: - headers["_ResponseFormat"] = "JSON" - if data: data_encoded = json.dumps(data).encode() if is_json else self._create_request_xml(data) else: diff --git a/huawei_lte_api/api/App.py b/huawei_lte_api/api/App.py index a41d40a..c6db3a1 100644 --- a/huawei_lte_api/api/App.py +++ b/huawei_lte_api/api/App.py @@ -1,8 +1,6 @@ -import json - from huawei_lte_api.ApiGroup import ApiGroup from huawei_lte_api.Session import GetResponseType, SetResponseType -from huawei_lte_api.exceptions import RequestFormatException +from huawei_lte_api.exceptions import ResponseErrorException class App(ApiGroup): @@ -21,7 +19,11 @@ def accept_privacypolicy(self, approve: bool = False) -> SetResponseType: } }, is_json=True) - if response["errcode"] == 0: + error_code = response["errcode"] + if error_code == 0: return "OK" - else: - raise RequestFormatException("Unexpected response: " + response) + + raise ResponseErrorException( + message="Unexpected response: " + str(response), + code=int(error_code) + ) diff --git a/huawei_lte_api/api/User.py b/huawei_lte_api/api/User.py index e721c0c..d68ce48 100644 --- a/huawei_lte_api/api/User.py +++ b/huawei_lte_api/api/User.py @@ -62,22 +62,22 @@ def state_login_with_retry(self) -> GetResponseType: if i == tries - 1: raise time.sleep((i + 1) / 10) - except ResponseErrorNotSupportedException: - raise + + raise ResponseErrorException(message="Tries exhausted", code=0) def _encode_password(self, username: str, password: Optional[str], password_type: PasswordTypeEnum = PasswordTypeEnum.BASE_64): if not password: return b'' - else: - if password_type == PasswordTypeEnum.SHA256: - concentrated = b''.join([ - username.encode('UTF-8'), - base64.b64encode(hashlib.sha256(password.encode('UTF-8')).hexdigest().encode('ascii')), - self._session.request_verification_tokens[0].encode('UTF-8') - ]) - return base64.b64encode(hashlib.sha256(concentrated).hexdigest().encode('ascii')) - else: - return base64.b64encode(password.encode('UTF-8')) + + if password_type == PasswordTypeEnum.SHA256: + concentrated = b''.join([ + username.encode('UTF-8'), + base64.b64encode(hashlib.sha256(password.encode('UTF-8')).hexdigest().encode('ascii')), + self._session.request_verification_tokens[0].encode('UTF-8') + ]) + return base64.b64encode(hashlib.sha256(concentrated).hexdigest().encode('ascii')) + + return base64.b64encode(password.encode('UTF-8')) def _login(self, username: str, password: Optional[str], password_type: PasswordTypeEnum = PasswordTypeEnum.BASE_64) -> bool: password_encoded = self._encode_password(username, password, password_type)