Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request to add custom SSL certificate verification #23

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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: 9 additions & 7 deletions watttime/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self, username: Optional[str] = None, password: Optional[str] = Non
"""
self.username = username or os.getenv("WATTTIME_USER")
self.password = password or os.getenv("WATTTIME_PASSWORD")
self.certificate_location = os.getenv("PEM_CERTIFICATE")
self.token = None
self.token_valid_until = None

Expand All @@ -39,6 +40,7 @@ def _login(self):
rsp = requests.get(
url,
auth=requests.auth.HTTPBasicAuth(self.username, self.password),
verify=self.certificate_location,
timeout=20,
)
rsp.raise_for_status()
Expand Down Expand Up @@ -126,7 +128,7 @@ def register(self, email: str, organization: Optional[str] = None) -> None:
"org": organization,
}

rsp = requests.post(url, json=params, timeout=20)
rsp = requests.post(url, json=params, verify=self.certificate_location, timeout=20)
rsp.raise_for_status()
print(
f"Successfully registered {self.username}, please check {email} for a verification email"
Expand Down Expand Up @@ -163,7 +165,7 @@ def region_from_loc(
"longitude": str(longitude),
"signal_type": signal_type,
}
rsp = requests.get(url, headers=headers, params=params)
rsp = requests.get(url, headers=headers, params=params, verify=self.certificate_location)
if not rsp.ok:
if rsp.status_code == 404:
# here we specifically cannot find a location that was provided
Expand Down Expand Up @@ -219,7 +221,7 @@ def get_historical_jsons(

for c in chunks:
params["start"], params["end"] = c
rsp = requests.get(url, headers=headers, params=params)
rsp = requests.get(url, headers=headers, params=params, verify=self.certificate_location)
try:
rsp.raise_for_status()
j = rsp.json()
Expand Down Expand Up @@ -325,7 +327,7 @@ def get_access_json(self) -> Dict:
self._login()
url = "{}/v3/my-access".format(self.url_base)
headers = {"Authorization": "Bearer " + self.token}
rsp = requests.get(url, headers=headers)
rsp = requests.get(url, headers=headers, verify=self.certificate_location)
rsp.raise_for_status()
return rsp.json()

Expand Down Expand Up @@ -409,7 +411,7 @@ def get_forecast_json(

url = "{}/v3/forecast".format(self.url_base)
headers = {"Authorization": "Bearer " + self.token}
rsp = requests.get(url, headers=headers, params=params)
rsp = requests.get(url, headers=headers, params=params, verify=self.certificate_location)
rsp.raise_for_status()
return rsp.json()

Expand Down Expand Up @@ -488,7 +490,7 @@ def get_historical_forecast_json(

for c in chunks:
params["start"], params["end"] = c
rsp = requests.get(url, headers=headers, params=params)
rsp = requests.get(url, headers=headers, params=params, verify=self.certificate_location)
try:
rsp.raise_for_status()
j = rsp.json()
Expand Down Expand Up @@ -565,6 +567,6 @@ def get_maps_json(
url = "{}/v3/maps".format(self.url_base)
headers = {"Authorization": "Bearer " + self.token}
params = {"signal_type": signal_type}
rsp = requests.get(url, headers=headers, params=params)
rsp = requests.get(url, headers=headers, params=params, verify=self.certificate_location)
rsp.raise_for_status()
return rsp.json()