-
Notifications
You must be signed in to change notification settings - Fork 0
/
carelink_client.py
386 lines (311 loc) · 13.6 KB
/
carelink_client.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
###############################################################################
#
# Carelink Client library
#
# Description:
#
# This library implements a client for the Medtronic Carelink API.
# It is a port of the original Java client by Bence Szász:
# https://github.com/benceszasz/CareLinkJavaClient
#
# Author:
#
# Ondrej Wisniewski (ondrej.wisniewski *at* gmail.com)
#
# Changelog:
#
# 09/05/2021 - Initial public release
# 06/06/2021 - Add check for expired token
# 19/09/2022 - Check for general BLE device family to support 770G
# 09/05/2023 - Fix connection issues by removing common http headers
# 24/05/2023 - Add handling of patient Id in data request
# 29/06/2023 - Get login parameters from response to connection request
#
# Copyright 2021-2023, Ondrej Wisniewski
#
###############################################################################
import json
import requests
from datetime import datetime, timedelta
from urllib.parse import urlparse, parse_qsl
import time
# Version string
VERSION = "0.6"
# Constants
CARELINK_CONNECT_SERVER_EU = "carelink.minimed.eu"
CARELINK_CONNECT_SERVER_US = "carelink.minimed.com"
CARELINK_LANGUAGE_EN = "en"
CARELINK_LOCALE_EN = "en"
CARELINK_AUTH_TOKEN_COOKIE_NAME = "auth_tmp_token"
CARELINK_TOKEN_VALIDTO_COOKIE_NAME = "c_token_valid_to"
AUTH_EXPIRE_DEADLINE_MINUTES = 1
DEBUG = False
def printdbg(msg):
if DEBUG:
print(msg)
class CareLinkClient(object):
def __init__(self, carelinkUsername, carelinkPassword, carelinkCountry, carelinkPatient):
# User info
self.__carelinkUsername = carelinkUsername
self.__carelinkPassword = carelinkPassword
self.__carelinkCountry = carelinkCountry.lower()
self.__carelinkPatient = carelinkPatient
# Session info
self.__sessionUser = None
self.__sessionProfile = None
self.__sessionCountrySettings = None
self.__sessionMonitorData = None
# State info
self.__loginInProcess = False
self.__loggedIn = False
self.__lastDataSuccess = False
self.__lastResponseCode = None
self.__lastErrorMessage = None
self.__commonHeaders = {}
'''
self.__commonHeaders = {
# Common browser headers
"Accept-Language":"en;q=0.9, *;q=0.8",
"Connection":"keep-alive",
"sec-ch-ua":"\"Google Chrome\";v=\"87\", \" Not;A Brand\";v=\"99\", \"Chromium\";v=\"87\"",
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36",
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
}
'''
# Create main http client session with CookieJar
self.__httpClient = requests.Session()
def getLastDataSuccess(self):
return self.__lastDataSuccess
def getLastResponseCode(self):
return self.__lastResponseCode
def getLastErrorMessage(self):
return self.__lastErrorMessage
# Get server URL
def __careLinkServer(self):
return CARELINK_CONNECT_SERVER_US if self.__carelinkCountry == "us" else CARELINK_CONNECT_SERVER_EU
def __extractResponseData(self, responseBody, begstr, endstr):
beg = responseBody.find(begstr) + len(begstr)
end = responseBody.find(endstr,beg)
return responseBody[beg:end].strip("\"")
def __getLoginSession(self):
url = "https://" + self.__careLinkServer() + "/patient/sso/login"
payload = {"country":self.__carelinkCountry, "lang":CARELINK_LANGUAGE_EN}
try:
response = self.__httpClient.get(url, headers = self.__commonHeaders, params = payload)
if not response.ok:
raise ValueError("session response is not OK")
except Exception as e:
printdbg(e)
printdbg("__getLoginSession() failed")
else:
printdbg("__getLoginSession() success")
return response
def __doLogin(self, loginSessionResponse):
queryParameters = dict(parse_qsl(urlparse(loginSessionResponse.url).query))
p = urlparse(loginSessionResponse.url)
url = p.scheme + "://" + p.netloc + p.path
payload = { "country":queryParameters["countrycode"],
"locale":queryParameters["locale"]
}
form = { "sessionID":queryParameters["sessionID"],
"sessionData":queryParameters["sessionData"],
"locale":queryParameters["locale"],
"action":"login",
"username":self.__carelinkUsername,
"password":self.__carelinkPassword,
"actionButton":"Log in"
}
try:
response = self.__httpClient.post(url, headers = self.__commonHeaders, params = payload, data = form)
if not response.ok:
raise ValueError("session response is not OK")
except Exception as e:
printdbg(e)
printdbg("__doLogin() failed")
else:
printdbg("__doLogin() success")
return response
def __doConsent(self, doLoginResponse):
# Extract data for consent
doLoginRespBody = doLoginResponse.text
url = self.__extractResponseData(doLoginRespBody, "<form action=", " ")
sessionID = self.__extractResponseData(doLoginRespBody, "<input type=\"hidden\" name=\"sessionID\" value=", ">")
sessionData = self.__extractResponseData(doLoginRespBody, "<input type=\"hidden\" name=\"sessionData\" value=", ">")
# Send consent
form = { "action":"consent",
"sessionID":sessionID,
"sessionData":sessionData,
"response_type":"code",
"response_mode":"query"
}
# Add header
consentHeaders = self.__commonHeaders
consentHeaders["Content-Type"] = "application/x-www-form-urlencoded"
try:
response = self.__httpClient.post(url, headers = consentHeaders, data = form)
if not response.ok:
raise ValueError("session response is not OK")
except Exception as e:
printdbg(e)
printdbg("__doConsent() failed")
else:
printdbg("__doConsent() success")
return response
def __getData(self, host, path, queryParams, requestBody):
printdbg("__getData()")
self.__lastDataSuccess = False;
if host==None:
url = path
else:
url = "https://" + host + "/" + path
payload = queryParams
data = requestBody
jsondata = None
# Get auth token
authToken = self.__getAuthorizationToken()
if (authToken != None):
try:
# Add header
headers = self.__commonHeaders
headers["Authorization"] = authToken
if data == None:
headers["Accept"] = "application/json, text/plain, */*"
headers["Content-Type"] = "application/json; charset=utf-8"
response = self.__httpClient.get(url, headers = headers, params = payload)
self.__lastResponseCode = response.status_code
if not response.ok:
raise ValueError("session get response is not OK")
else:
headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
headers["Content-Type"] = "application/x-www-form-urlencoded"
response = self.__httpClient.post(url, headers = headers, data = data)
self.__lastResponseCode = response.status_code
if not response.ok:
printdbg(response.status_code)
raise ValueError("session post response is not OK")
except Exception as e:
printdbg(e)
printdbg("__getData() failed")
else:
jsondata = json.loads(response.text)
self.__lastDataSuccess = True
return jsondata
def __getMyUser(self):
printdbg("__getMyUser()")
return self.__getData(self.__careLinkServer(), "patient/users/me", None, None)
def __getMyProfile(self):
printdbg("__getMyProfile()")
return self.__getData(self.__careLinkServer(), "patient/users/me/profile", None, None)
def __getCountrySettings(self, country, language):
printdbg("__getCountrySettings()")
queryParams = { "countryCode":country,
"language":language
}
return self.__getData(self.__careLinkServer(), "patient/countries/settings", queryParams, None)
def __getMonitorData(self):
printdbg("__getMonitorData()")
return self.__getData(self.__careLinkServer(), "patient/monitor/data", None, None,)
# Old last24hours webapp data
def __getLast24Hours(self):
printdbg("__getLast24Hours")
queryParams = { "cpSerialNumber":"NONE",
"msgType":"last24hours",
"requestTime":str(int(time.time()*1000))
}
return self.__getData(self.__careLinkServer(), "patient/connect/data", queryParams, None)
# Periodic data from CareLink Cloud
def __getConnectDisplayMessage(self, username, role, patient, endpointUrl):
printdbg("__getConnectDisplayMessage()")
# Build user json for request
userJson = { "username":username,
"role":role,
"patientId":patient
}
requestBody = json.dumps(userJson)
recentData = self.__getData(None, endpointUrl, None, requestBody)
if recentData != None:
self.__correctTimeInRecentData(recentData)
return recentData
def __correctTimeInRecentData(self,recentData):
# TODO
pass
def __executeLoginProcedure(self):
lastLoginSuccess = False
self.__loginInProcess = True
self.__lastErrorMessage = None
try:
# Clear cookies
self.__httpClient.cookies.clear_session_cookies()
# Clear basic infos
self.__sessionUser = None
self.__sessionProfile = None
self.__sessionCountrySettings = None
self.__sessionMonitorData = None
# Open login (get SessionId and SessionData)
loginSessionResponse = self.__getLoginSession()
self.__lastResponseCode = loginSessionResponse.status_code
# Login
doLoginResponse = self.__doLogin(loginSessionResponse)
self.__lastResponseCode = doLoginResponse.status_code
#setLastResponseBody(loginSessionResponse)
loginSessionResponse.close()
# Consent
consentResponse = self.__doConsent(doLoginResponse)
self.__lastResponseCode = consentResponse.status_code
#setLastResponseBody(consentResponse);
doLoginResponse.close()
consentResponse.close()
# Get sessions infos if required
if self.__sessionUser == None:
self.__sessionUser = self.__getMyUser()
if self.__sessionProfile == None:
self.__sessionProfile = self.__getMyProfile()
if self.__sessionCountrySettings == None:
self.__sessionCountrySettings = self.__getCountrySettings(self.__carelinkCountry, CARELINK_LANGUAGE_EN)
if self.__sessionMonitorData == None:
self.__sessionMonitorData = self.__getMonitorData()
# Set login success if everything was ok:
if self.__sessionUser != None and self.__sessionProfile != None and self.__sessionCountrySettings != None and self.__sessionMonitorData != None:
lastLoginSuccess = True
except Exception as e:
printdbg(e)
self.__lastErrorMessage = e
self.__loginInProcess = False
self.__loggedIn = lastLoginSuccess
return lastLoginSuccess
def __getAuthorizationToken(self):
auth_token = self.__httpClient.cookies.get(CARELINK_AUTH_TOKEN_COOKIE_NAME)
auth_token_validto = self.__httpClient.cookies.get(CARELINK_TOKEN_VALIDTO_COOKIE_NAME)
# New token is needed:
# a) no token or about to expire => execute authentication
# b) last response 401
if auth_token == None or auth_token_validto == None or \
(datetime.strptime(auth_token_validto, '%a %b %d %H:%M:%S UTC %Y') - datetime.utcnow()) < timedelta(seconds=300) or \
self.__lastResponseCode in [401,403]:
# execute new login process | null, if error OR already doing login
if self.__loginInProcess:
printdbg("loginInProcess")
return None
if not self.__executeLoginProcedure():
printdbg("__executeLoginProcedure failed")
return None
printdbg("auth_token_validto = " + self.__httpClient.cookies.get(CARELINK_TOKEN_VALIDTO_COOKIE_NAME))
# there can be only one
return "Bearer " + self.__httpClient.cookies.get(CARELINK_AUTH_TOKEN_COOKIE_NAME)
# Wrapper for data retrival methods
def getRecentData(self):
# Force login to get basic info
if self.__getAuthorizationToken() != None:
if self.__carelinkCountry == "us" or "BLE" in self.__sessionMonitorData["deviceFamily"]:
role = "carepartner" if self.__sessionUser["role"] in ["CARE_PARTNER","CARE_PARTNER_OUS"] else "patient"
patient = self.__carelinkPatient
return self.__getConnectDisplayMessage(self.__sessionProfile["username"], role, patient, self.__sessionCountrySettings["blePereodicDataEndpoint"])
else:
return self.__getLast24Hours()
else:
return None
# Authentication methods
def login(self):
if not self.__loggedIn:
self.__executeLoginProcedure()
return self.__loggedIn