Skip to content

Instantly share code, notes, and snippets.

@exterm
Created August 19, 2024 00:55
Show Gist options
  • Save exterm/97d624193b6b556d259c6eb7b027d6ab to your computer and use it in GitHub Desktop.
Save exterm/97d624193b6b556d259c6eb7b027d6ab to your computer and use it in GitHub Desktop.
Hydro Ottawa Private API Client
import requests
from pycognito import Cognito
# AWS Cognito configuration
client_id = '7scfcis6ecucktmp4aqi1jk6cb'
user_pool_id = 'ca-central-1_VYnwOhMBK'
username = 'your_email'
password = 'your_password'
# Step 1: Authenticate and get tokens using pycognito
u = Cognito(user_pool_id, client_id, username=username)
u.authenticate(password=password)
# Extract the necessary tokens
id_token = u.id_token
access_token = u.access_token
# Step 2: Use the tokens to get the loggedInAppToken from the response header
app_token_url = 'https://api-myaccount.hydroottawa.com/app-token'
# Prepare the headers with x-id and x-access tokens
headers = {
'x-id': id_token, # Corresponds to the id_token
'x-access': access_token, # Corresponds to the access_token
'Accept': 'application/json',
}
# Step 3: Make the request to get the loggedInAppToken from the response headers
response = requests.get(app_token_url, headers=headers)
if response.status_code == 200:
loggedInAppToken = response.headers.get('x-amzn-remapped-authorization')
if loggedInAppToken:
print(f"loggedInAppToken: {loggedInAppToken}")
else:
print("loggedInAppToken not found in response headers.")
else:
print(f"Failed to obtain loggedInAppToken: {response.status_code}")
print(response.text)
exit(1)
# Step 4: Use the loggedInAppToken to make authenticated requests to the Hydro Ottawa API
headers = {
'x-id': id_token, # Corresponds to the id_token
'x-access': access_token, # Corresponds to the access_token
'Authorization': loggedInAppToken, # Corresponds to the loggedInAppToken
'Accept': 'application/json',
}
# first, get current billing period
billing_period_url = 'https://api-myaccount.hydroottawa.com/usage/billing-period-list'
response = requests.get(billing_period_url, headers=headers)
if response.status_code == 200:
billing_periods = response.json()
current_billing_period = billing_periods[0]
start_date = current_billing_period['startDate']
end_date = current_billing_period['endDate']
print(f"Current billing period: {start_date} - {end_date}")
else:
print(f"Failed to obtain billing periods: {response.status_code}")
print(response.text)
exit(1)
# second, get usage data for the current billing period
usage_url = 'https://api-myaccount.hydroottawa.com/usage/consumption/billing-period'
request_body = {
"startDate": start_date,
"endDate": end_date
}
response = requests.post(usage_url, headers=headers, json=request_body)
if response.status_code == 200:
usage_data = response.json()
total_usage = usage_data['summary']['totalUsage']
print(f"Total usage for the current billing period: {total_usage}")
else:
print(f"Failed to obtain usage data: {response.status_code}")
print(response.text)
exit(1)
@coryjog
Copy link

coryjog commented Sep 30, 2024

Hey there,
We just had a residential solar system setup today and I found this really helpful to compare our usage data to our output. Thanks for posting this!

@exterm
Copy link
Author

exterm commented Oct 1, 2024

Thanks for commenting, nice to hear!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment