diff options
author | Jan <sentrycraft123@gmail.com> | 2023-07-24 20:07:40 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-24 20:07:40 +0200 |
commit | 1f9d84484b5f4698b3d789bccfda18fdd682be99 (patch) | |
tree | d3efbbe6d79588b450d3f50c3143f50db36bf94a | |
parent | e4f6edc19bc45835c19ffdbaaffc8d33143f0fb4 (diff) | |
download | ShellyPy-1f9d84484b5f4698b3d789bccfda18fdd682be99.tar.gz ShellyPy-1f9d84484b5f4698b3d789bccfda18fdd682be99.zip |
Implement Gen2 digest auth (#13)
* Implement Gen2 digest auth
* setup payload_id ahead of constructor
-rw-r--r-- | ShellyPy/base.py | 4 | ||||
-rw-r--r-- | ShellyPy/gen1.py | 7 | ||||
-rw-r--r-- | ShellyPy/gen2.py | 11 |
3 files changed, 15 insertions, 7 deletions
diff --git a/ShellyPy/base.py b/ShellyPy/base.py index 505d796..6c34be0 100644 --- a/ShellyPy/base.py +++ b/ShellyPy/base.py @@ -5,8 +5,6 @@ if version_info.major == 3: else: JSONDecodeError = ValueError -from requests.auth import HTTPBasicAuth - class ShellyBase: def __init__(self, ip, port = "80", *args, **kwargs): @@ -37,7 +35,7 @@ class ShellyBase: self.__timeout__ = kwargs.get("timeout", 5) - self.__credentials__ = HTTPBasicAuth( + self.__credentials__ = ( login.get("username", ""), login.get("password", "") ) diff --git a/ShellyPy/gen1.py b/ShellyPy/gen1.py index e3df549..dea4650 100644 --- a/ShellyPy/gen1.py +++ b/ShellyPy/gen1.py @@ -7,6 +7,7 @@ else: from requests import post +from requests.auth import HTTPBasicAuth from .error import BadLogin, NotFound, BadResponse @@ -77,9 +78,11 @@ class ShellyGen1(ShellyBase): print("Target Adress: {}\n" "Authentication: {}\n" "Timeout: {}" - "".format(url, any(self.__credentials__.username + self.__credentials__.password), self.__timeout__)) + "".format(url, any(self.__credentials__), self.__timeout__)) - response = post(url, auth=self.__credentials__, + credentials = HTTPBasicAuth(*self.__credentials__) + + response = post(url, auth=credentials, timeout=self.__timeout__) if response.status_code == 401: diff --git a/ShellyPy/gen2.py b/ShellyPy/gen2.py index 96747c9..199fb84 100644 --- a/ShellyPy/gen2.py +++ b/ShellyPy/gen2.py @@ -6,6 +6,7 @@ else: JSONDecodeError = ValueError from requests import post +from requests.auth import HTTPDigestAuth from .error import BadLogin, NotFound, BadResponse @@ -23,9 +24,9 @@ class ShellyGen2(ShellyBase): @param init calls the update method on init """ + self.payload_id = 1 super().__init__(ip, port, *args, **kwargs) self.__generation__ = 2 - self.payload_id = 1 def update(self): status = self.settings() @@ -50,7 +51,13 @@ class ShellyGen2(ShellyBase): if values: payload["params"] = values - response = post(url, auth=self.__credentials__, + credentials = None + try: + credentials = auth=HTTPDigestAuth('admin', self.__credentials__[1]) + except IndexError: + pass + + response = post(url, auth=credentials, json=payload, timeout=self.__timeout__) |