Skip to content

Commit 2012d7a

Browse files
committed
Throw when making a request without properly logging in.
1 parent f22574f commit 2012d7a

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

pyemvue/auth.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def __init__(
3434
self.initial_retry_delay = max(initial_retry_delay, 0.5)
3535
self.max_retry_delay = max(max_retry_delay, 0)
3636
self.pool_wellknown_jwks = None
37+
self.tokens = {}
3738

3839
self._password = None
3940

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

8384
def request(self, method: str, path: str, **kwargs) -> requests.Response:
8485
"""Make a request."""
86+
if not self.tokens or not self.tokens["access_token"]:
87+
raise ValueError("Not authenticated. Incorrect username or password?")
88+
8589
dec_access_token = self._decode_token(self.tokens["access_token"])
8690

8791
attempts = 0

tools/charger_test.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
password = getpass.getpass("Enter your password: ")
1414

1515
# Log in to PyEmVue
16-
vue.login(username=email, password=password, token_storage_file='keys.json')
16+
logged_in = vue.login(username=email, password=password, token_storage_file='keys.json')
1717
else:
1818
# If keys.json exists, log in using the keys in the file
19-
vue.login(token_storage_file='keys.json')
19+
logged_in = vue.login(token_storage_file='keys.json')
20+
21+
print("Logged in?", logged_in)
22+
if not logged_in:
23+
raise Exception("Login failed")
2024

2125
# Get the list of chargers
2226
chargers = vue.get_chargers()

tools/outlet_test.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
from pyemvue.pyemvue import PyEmVue
22

33
vue = PyEmVue()
4-
vue.login(token_storage_file='keys.json')
5-
print('Logged in.')
4+
logged_in = vue.login(token_storage_file='keys.json')
5+
print('Logged in?', logged_in)
66
print()
77

8+
if not logged_in:
9+
raise Exception('Login failed')
10+
811
devices = vue.get_devices()
912
outlets, chargers = vue.get_devices_status()
1013
for device in devices:

0 commit comments

Comments
 (0)