Skip to content

Commit

Permalink
feat: use token instead of username and password (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
rokam authored Oct 2, 2024
1 parent 94c9357 commit 571b9ae
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Binary file added img/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 21 additions & 2 deletions sunweg/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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__,
Expand Down
16 changes: 16 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ def test_error500(self) -> None:
api.authenticate()
assert e_info.value.__str__() == "Request failed: <Response [500]>"

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(
Expand All @@ -106,6 +117,11 @@ def test_authenticate_success(self) -> None:
api = APIHelper("[email protected]", "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(
Expand Down

0 comments on commit 571b9ae

Please sign in to comment.