Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions watttime/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ def __init__(
worker_count (int): The number of worker threads to use for multithreading. Default is min(10, (os.cpu_count() or 1) * 2).

"""
self.username = username or os.getenv("WATTTIME_USER")
self.password = password or os.getenv("WATTTIME_PASSWORD")

if username:
os.environ["WATTTIME_USER"] = username
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the intent of this was to allow users to pass in these parameters, but not save them as attributes that could be pickled.

setting an env var using os.environ is only persistent for the current session.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR's is intended to preserve the signature, i.e. users can still pass these parameters. But the class handles them differently: it stores them in os.environ, then retrieves them each time they are needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I misunderstood the history here. This change is already on future-release, and is being picked over to main?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No this is a new approach. The change on future-release stored the password as a class property whereas this stores it in the sessions' environment.

Copy link
Contributor Author

@jcofield jcofield Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setting an env var using os.environ is only persistent for the current session.

It's good to check that we can re-login, but I think we are robust to this still. I will attempt to write a test to verify re-login on an expired token works as expected.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the approach on future-release does not store anything in the class property as far as I can tell. I believe it's the same approach as this with additional logging.

password = os.getenv("WATTTIME_PASSWORD")

I think that logging can be helpful, but if you'd rather move ahead with out it thats ok by me.

if password:
os.environ["WATTTIME_PASSWORD"] = password

self.token = None
self.headers = None
self.token_valid_until = None
Expand Down Expand Up @@ -67,7 +71,7 @@ def _login(self):
url = f"{self.url_base}/login"
rsp = self.session.get(
url,
auth=requests.auth.HTTPBasicAuth(self.username, self.password),
auth=requests.auth.HTTPBasicAuth(os.getenv("WATTTIME_USER"), os.getenv("WATTTIME_PASSWORD")),
timeout=(10, 60),
)
rsp.raise_for_status()
Expand Down Expand Up @@ -150,16 +154,16 @@ def register(self, email: str, organization: Optional[str] = None) -> None:

url = f"{self.url_base}/register"
params = {
"username": self.username,
"password": self.password,
"username": os.getenv("WATTTIME_USER"),
"password": os.getenv("WATTTIME_PASSWORD"),
"email": email,
"org": organization,
}

rsp = self.session.post(url, json=params, timeout=(10, 60))
rsp.raise_for_status()
print(
f"Successfully registered {self.username}, please check {email} for a verification email"
f"Successfully registered {os.getenv('WATTTIME_USER')}, please check {email} for a verification email"
)

@cache
Expand Down