From 2ac075ff41b15a24b5f06cf11ff92ea2cdbcad76 Mon Sep 17 00:00:00 2001 From: matthew mcginn Date: Fri, 6 Mar 2026 10:19:07 -0500 Subject: [PATCH] add a user-agent with the version to our requests Signed-off-by: matthew mcginn --- .VERSION | 1 + setup.py | 2 +- watttime/api.py | 24 ++++++++++++++++-------- 3 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 .VERSION diff --git a/.VERSION b/.VERSION new file mode 100644 index 00000000..ea7786a3 --- /dev/null +++ b/.VERSION @@ -0,0 +1 @@ +v1.3.3 diff --git a/setup.py b/setup.py index bbe2c581..497be852 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ description="An officially maintained python client for WattTime's API providing access to electricity grid emissions data.", long_description=open("README.md").read(), long_description_content_type="text/markdown", - version="v1.3.3", + version=open(".VERSION").read().strip(), packages=["watttime"], python_requires=">=3.8", install_requires=["requests", "pandas>1.0.0", "holidays", "python-dateutil", "pytz"], diff --git a/watttime/api.py b/watttime/api.py index fadcad97..e804b937 100644 --- a/watttime/api.py +++ b/watttime/api.py @@ -1,21 +1,24 @@ +import logging import os -import time import threading import time -import logging -from datetime import date, datetime, timedelta, time as dt_time from collections import defaultdict +from concurrent.futures import ThreadPoolExecutor, as_completed +from datetime import date, datetime +from datetime import time as dt_time +from datetime import timedelta from functools import cache from pathlib import Path from typing import Any, Dict, List, Literal, Optional, Tuple, Union -from concurrent.futures import ThreadPoolExecutor, as_completed -from urllib3.util.retry import Retry -from requests.adapters import HTTPAdapter import pandas as pd import requests from dateutil.parser import parse from pytz import UTC +from requests.adapters import HTTPAdapter +from urllib3.util.retry import Retry + +VERSION = open(".VERSION").read().strip() class WattTimeAPIWarning: @@ -124,7 +127,9 @@ def _login(self): url = f"{self.url_base}/login" rsp = self.session.get( url, - auth=requests.auth.HTTPBasicAuth(os.getenv("WATTTIME_USER"), os.getenv("WATTTIME_PASSWORD")), + auth=requests.auth.HTTPBasicAuth( + os.getenv("WATTTIME_USER"), os.getenv("WATTTIME_PASSWORD") + ), timeout=(10, 60), ) rsp.raise_for_status() @@ -132,7 +137,10 @@ def _login(self): self.token_valid_until = datetime.now() + timedelta(minutes=30) if not self.token: raise Exception("failed to log in, double check your credentials") - self.headers = {"Authorization": "Bearer " + self.token} + self.headers = { + "Authorization": "Bearer " + self.token, + "User-Agent": f"watttime-python-sdk-{VERSION}", + } def _is_token_valid(self) -> bool: if not self.token_valid_until: