Skip to content

Commit

Permalink
Throw when making a request without properly logging in.
Browse files Browse the repository at this point in the history
  • Loading branch information
magico13 committed Jul 30, 2024
1 parent f22574f commit 2012d7a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
4 changes: 4 additions & 0 deletions pyemvue/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(
self.initial_retry_delay = max(initial_retry_delay, 0.5)
self.max_retry_delay = max(max_retry_delay, 0)
self.pool_wellknown_jwks = None
self.tokens = {}

self._password = None

Expand Down Expand Up @@ -82,6 +83,9 @@ def get_username(self) -> str:

def request(self, method: str, path: str, **kwargs) -> requests.Response:
"""Make a request."""
if not self.tokens or not self.tokens["access_token"]:
raise ValueError("Not authenticated. Incorrect username or password?")

dec_access_token = self._decode_token(self.tokens["access_token"])

attempts = 0
Expand Down
8 changes: 6 additions & 2 deletions tools/charger_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
password = getpass.getpass("Enter your password: ")

# Log in to PyEmVue
vue.login(username=email, password=password, token_storage_file='keys.json')
logged_in = vue.login(username=email, password=password, token_storage_file='keys.json')
else:
# If keys.json exists, log in using the keys in the file
vue.login(token_storage_file='keys.json')
logged_in = vue.login(token_storage_file='keys.json')

print("Logged in?", logged_in)
if not logged_in:
raise Exception("Login failed")

# Get the list of chargers
chargers = vue.get_chargers()
Expand Down
7 changes: 5 additions & 2 deletions tools/outlet_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from pyemvue.pyemvue import PyEmVue

vue = PyEmVue()
vue.login(token_storage_file='keys.json')
print('Logged in.')
logged_in = vue.login(token_storage_file='keys.json')
print('Logged in?', logged_in)
print()

if not logged_in:
raise Exception('Login failed')

devices = vue.get_devices()
outlets, chargers = vue.get_devices_status()
for device in devices:
Expand Down

0 comments on commit 2012d7a

Please sign in to comment.