From 0f248ee291570cbca58fc5bae6bbcbd6db36c0a2 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 | 14 ++++++++++---- 3 files changed, 12 insertions(+), 5 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 25977d60..ed5a3e58 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"], diff --git a/watttime/api.py b/watttime/api.py index 8be1113e..0a208de6 100644 --- a/watttime/api.py +++ b/watttime/api.py @@ -1,19 +1,22 @@ import os -import time import threading import time -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 import pandas as pd import requests from dateutil.parser import parse from pytz import UTC +VERSION = open(".VERSION").read().strip() + class WattTimeBase: url_base = os.getenv("WATTTIME_API_URL", "https://api.watttime.org") @@ -106,7 +109,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: