-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiHealth.py
133 lines (117 loc) · 5.93 KB
/
iHealth.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
__author__ = 'Alexandr Kurlov'
from flask import request, redirect
import requests
import json
import config as cfg
class iHealth():
""" A basic class of iHealth API handler """
def __init__(self, client_id, client_secret, redirect_uri):
self.access_token = ''
self.refresh_token = ''
self.user_id = ''
self.client_id = client_id
self.client_secret = client_secret
self.redirect_uri = redirect_uri
self.auth_url = cfg.AUTH_URL
self.user_url = cfg.USER_DATA_URL
self.app_url = cfg.ALL_DATA_URL
self.response_type = 'code' # default value for response_type
self.APIName = 'OpenApiBP OpenApiWeight OpenApiActivity OpenApiBG OpenApiSleep OpenApiSpO2 OpenApiUserInfo' # an array of target API
self.RequiredAPIName = 'OpenApiBP OpenApiWeight OpenApiActivity OpenApiBG OpenApiSleep OpenApiSpO2 OpenApiUserInfo' # it is must be selected for the authentication page
self.IsNew = 'true' # the system will be auto redirected to the sign up page
def authorize(self):
payload = {'client_id': self.client_id, 'response_type': self.response_type,
'redirect_uri': self.redirect_uri, 'APIName': self.APIName,
'RequiredAPIName': self.RequiredAPIName, 'IsNew': self.IsNew}
r = requests.get(self.auth_url, params=payload)
return r
def callback(self):
code = self.get_code()
grant_type = 'authorization_code' # is currently the only supported value
payload = {'code': code, 'client_id': self.client_id, 'grant_type': grant_type,
'client_secret': self.client_secret, 'redirect_uri': self.redirect_uri}
r = requests.get(self.auth_url, params=payload)
self.access_token, self.refresh_token = self.get_tokens(r.text)
self.user_id = self.get_user_id(r.text)
return r.text
def get_code(self):
if 'code' not in request.args:
return None
return request.args['code']
def get_tokens(self, data):
resp = json.loads(data)
if resp['AccessToken'] and resp['RefreshToken']:
return resp['AccessToken'], resp['RefreshToken']
else:
return None, None # this should be handled as an error
def get_user_id(self, data):
resp = json.loads(data)
if resp['UserID']:
return resp['UserID']
else:
return None
def get_blood_pressure(self):
base_url = self.user_url+str(self.user_id)+'/bp/'
BP = cfg.DATA_TYPES['OpenApiBP']
payload = {'client_id': self.client_id, 'client_secret': self.client_secret,
'access_token': self.access_token, 'redirect_uri': self.redirect_uri,
'sc': BP['sc'], 'sv': BP['sv']}
r = requests.get(base_url, params=payload)
return r.text
def get_weight(self):
base_url = self.user_url+str(self.user_id)+'/weight/'
Weight = cfg.DATA_TYPES['OpenApiWeight']
payload = {'client_id': self.client_id, 'client_secret': self.client_secret,
'access_token': self.access_token, 'redirect_uri': self.redirect_uri,
'sc': Weight['sc'], 'sv': Weight['sv']}
r = requests.get(base_url, params=payload)
return r.text
def get_bg(self):
base_url = self.user_url+str(self.user_id)+'/glucose/'
BG = cfg.DATA_TYPES['OpenApiBG']
payload = {'client_id': self.client_id, 'client_secret': self.client_secret,
'access_token': self.access_token, 'redirect_uri': self.redirect_uri,
'sc': BG['sc'], 'sv': BG['sv']}
r = requests.get(base_url, params=payload)
return r.text
def get_blood_oxygen(self):
base_url = self.user_url+str(self.user_id)+'/spo2/'
spO2 = cfg.DATA_TYPES['OpenApiSpO2']
payload = {'client_id': self.client_id, 'client_secret': self.client_secret,
'access_token': self.access_token, 'redirect_uri': self.redirect_uri,
'sc': spO2['sc'], 'sv': spO2['sv']}
r = requests.get(base_url, params=payload)
return r.text
def get_activity_report(self):
base_url = self.user_url+str(self.user_id)+'/activity/'
activity = cfg.DATA_TYPES['OpenApiActivity']
payload = {'client_id': self.client_id, 'client_secret': self.client_secret,
'access_token': self.access_token, 'redirect_uri': self.redirect_uri,
'sc': activity['sc'], 'sv': activity['sv']}
r = requests.get(base_url, params=payload)
return r.text
def get_sleep_report(self):
base_url = self.user_url+str(self.user_id)+'/sleep/'
sleep = cfg.DATA_TYPES['OpenApiSleep']
payload = {'client_id': self.client_id, 'client_secret': self.client_secret,
'access_token': self.access_token, 'redirect_uri': self.redirect_uri,
'sc': sleep['sc'], 'sv': sleep['sv']}
r = requests.get(base_url, params=payload)
return r.text
def get_food(self):
#seems like it is UserInfo. It does not covered by iHealth doc.
base_url = self.user_url+str(self.user_id)+'/food/'
food = cfg.DATA_TYPES['OpenApiUserInfo']
payload = {'client_id': self.client_id, 'client_secret': self.client_secret,
'access_token': self.access_token, 'redirect_uri': self.redirect_uri,
'sc': food['sc'], 'sv': food['sv']}
r = requests.get(base_url, params=payload)
return r.text
def get_sport_report(self):
base_url = self.user_url+str(self.user_id)+'/sport/'
sport = cfg.DATA_TYPES['OpenApiActivity']
payload = {'client_id': self.client_id, 'client_secret': self.client_secret,
'access_token': self.access_token, 'redirect_uri': self.redirect_uri,
'sc': sport['sc'], 'sv': sport['sv']}
r = requests.get(base_url, params=payload)
return r.text