-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwahoo_api.py
135 lines (120 loc) · 4.4 KB
/
wahoo_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""
All Wahoo modules to talk to the Wahoo API
Infomation at: https://developers.wahooligan.com/cloud
"""
import json
import logging
import os
import sys
import webbrowser
import requests
from dotenv import load_dotenv
load_dotenv()
# Wahoo API access
wahoo_client_id = os.getenv("wahoo_client_id")
wahoo_secret = os.getenv("wahoo_secret")
wahoo_redirect_uri = os.getenv("wahoo_redirect_uri")
wahoo_api = "https://api.wahooligan.com"
wahoo_cfg = "wahoo.json"
wahoo_scopes = "user_write+email+workouts_read+workouts_write+power_zones_read\
+power_zones_write+offline_data+user_read"
def wahoo_authenticate():
"""Setup Authentication with Wahoo API"""
url = f"{wahoo_api}/oauth/authorize?client_id={wahoo_client_id}&redirect_uri={wahoo_redirect_uri}&response_type=code&scope={wahoo_scopes}"
print(
f"No token found, webbrowser will open, authorize the application \
and copy paste the code section or open URL manually {url}"
)
logging.info(
"No token found, webbrowser will open, authorize the \
application and copy paste the code section"
)
webbrowser.open(url, new=2)
print(f"If browser didn't open, copy this link into your browser: {url}")
wahoo_code = input("Insert the code from the URL after authorizing: ")
paramdata = {
"action": "requesttoken",
"code": wahoo_code,
"client_id": wahoo_client_id,
"client_secret": wahoo_secret,
"grant_type": "authorization_code",
"redirect_uri": wahoo_redirect_uri,
}
res = requests.post(f"{wahoo_api}/oauth/token", params=paramdata, timeout=10)
out = res.json()
if res.status_code == 200:
with open(wahoo_cfg, "w", encoding="utf8") as file:
json.dump(out, file)
file.close()
return out["access_token"]
if res.status_code != 200:
print("Wahoo authentication failed:")
print(out)
logging.info("Wahoo authentication failed:")
logging.info(out)
sys.exit()
def wahoo_refresh(token):
"""refresh current token
this makes sure we won't have to reauthorize again."""
url = f"{wahoo_api}/oauth/token"
res = requests.post(
url,
params={
"client_id": wahoo_client_id,
"client_secret": wahoo_secret,
"action": "requesttoken",
"grant_type": "refresh_token",
"refresh_token": token["refresh_token"],
},
timeout=10,
)
out = res.json()
if res.status_code == 200:
with open(wahoo_cfg, "w", encoding="utf8") as file:
json.dump(out, file)
file.close()
return out["access_token"]
if res.status_code != 200:
logging.info("Wahoo token refresh failed")
logging.info(out)
sys.exit()
def get_wahoo_user(token):
"""Read user information from Wahoo"""
url = f"{wahoo_api}/v1/user"
res = requests.get(url, headers={"Authorization": f"Bearer {token}"}, timeout=10)
return res.json()
def set_wahoo_user_weight(token, weight):
"""Write the weight to the Wahoo user settings"""
# TODO: Which function should be used? write_weight_wahoo
# or set_wahoo_user_weight
url = f"{wahoo_api}/v1/user"
headers = {"Authorization": f"Bearer {token}"}
data = {"user[weight]": f"{weight}"}
res = requests.put(url, headers=headers, data=data, timeout=10)
if res.status_code != 200:
print("There was an error writing to Wahoo API:")
print(res.json())
logging.info("There was an error writing to Wahoo API:")
logging.info(res.json())
return False
else:
print(f"Succesful writing weight {weight} to Wahoo API")
logging.info("Succesful writing weight %s to Wahoo API", weight)
return True
def write_weight_wahoo(user_weight):
"""Write the weight to the Wahoo user settings"""
# TODO: Which function should be used? write_weight_wahoo
# or set_wahoo_user_weight
wahoo_access_token = (
wahoo_refresh(json.load(open(wahoo_cfg, encoding="utf8")))
if os.path.isfile(wahoo_cfg)
else wahoo_authenticate()
)
wahoo_user_info = get_wahoo_user(wahoo_access_token)
logging.info(
"Retreived Wahoo userid %s for %s %s",
wahoo_user_info["id"],
wahoo_user_info["first"],
wahoo_user_info["last"],
)
return set_wahoo_user_weight(wahoo_access_token, user_weight)