diff --git a/README.md b/README.md index 3f119c6..1cc1ac2 100644 --- a/README.md +++ b/README.md @@ -9,10 +9,20 @@ Python lib for WEG solar energy platform, [SunWEG.net](https://sunweg.net/) ## Usage +### Retrieve token +You need to use devtools from your browser to retrieve the token. In most of them, you can open devtools by pressing F12. +Inside the network tab, you need to check the preserve logs and then do a login. + +You can find the token in the request header of various XHR requests, for example: +![Devtools with a request with a token](img/image.png) + +It will be available in the X-Auth-Token-Update header. + +### Code sample ``` python from sunweg.api import APIHelper -api = APIHelper('username','password') +api = APIHelper(token='your token here') plants = api.listPlants() for plant in plants: print(plant) diff --git a/img/image.png b/img/image.png new file mode 100644 index 0000000..d4851c1 Binary files /dev/null and b/img/image.png differ diff --git a/sunweg/api.py b/sunweg/api.py index 0331188..55a90e2 100644 --- a/sunweg/api.py +++ b/sunweg/api.py @@ -83,20 +83,36 @@ class APIHelper: SERVER_URI = SUNWEG_URL - def __init__(self, username: str, password: str) -> None: + def __init__( + self, + username: str | None = None, + password: str | None = None, + token: str | None = None, + ) -> None: """ Initialize APIHelper for SunWEG platform. :param username: username for authentication :param password: password for authentication + :param token: token for authentication :type username: str :type password: str + :type token: str """ - self._token = None + self._token = token self._username = username self._password = password self.session = session() + def set_token(self, token: str) -> None: + """ + Set token. + + :param token: token for authentication + :type token: str + """ + self._token = token + def _set_username(self, username: str) -> None: """ Set username. @@ -126,6 +142,9 @@ def authenticate(self) -> bool: :return: True on authentication success :rtype: bool """ + if self._username is None or self._password is None: + return False + user_data = json.dumps( {"usuario": self._username, "senha": self._password, "rememberMe": True}, default=lambda o: o.__dict__, diff --git a/tests/test_api.py b/tests/test_api.py index d5aca0c..8032f9c 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -97,6 +97,17 @@ def test_error500(self) -> None: api.authenticate() assert e_info.value.__str__() == "Request failed: " + def test_initialize_token(self) -> None: + """Test initialize token.""" + api = APIHelper(token="token") + assert api._token == "token" + + def test_set_token(self) -> None: + """Test set token.""" + api = APIHelper(token="token") + api.set_token("new_token") + assert api._token == "new_token" + def test_authenticate_success(self) -> None: """Test authentication success.""" with patch( @@ -106,6 +117,11 @@ def test_authenticate_success(self) -> None: api = APIHelper("user@acme.com", "password") assert api.authenticate() + def test_authenticate_fail_empty_credentials(self) -> None: + """Test authentication failed.""" + api = APIHelper(None, None) + assert not api.authenticate() + def test_authenticate_failed(self) -> None: """Test authentication failed.""" with patch(