-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser.go
84 lines (69 loc) · 2.11 KB
/
user.go
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
package dexcomClient
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strings"
)
const (
applicationId = "d89443d2-327c-4a6f-89e5-496bbb0317db"
agent = "Dexcom Share/3.0.2.11 CFNetwork/711.2.23 Darwin/14.0.0"
loginUrl = "https://share1.dexcom.com/ShareWebServices/Services/General/LoginPublisherAccountByName"
latestGlucoseUrl = "https://share1.dexcom.com/ShareWebServices/Services/Publisher/ReadPublisherLatestGlucoseValues"
)
type RealTimeData struct {
DeviceTime string `json:"DT"`
ServerTime string `json:"ST"`
Trend int
Value float64
}
func (client *DexcomClient) getLatestGlucoseUrl() string {
return latestGlucoseUrl + "?sessionID=" + client.DexcomToken + "&minutes=1440&maxCount=1"
}
func (client *DexcomClient) GetSessionID(username, password string) error {
payload := map[string]string{
"accountName": username,
"password": password,
"applicationId": applicationId,
}
payloadBytes, _ := json.Marshal(&payload)
payloadReader := bytes.NewReader(payloadBytes)
req, _ := http.NewRequest("POST", loginUrl, payloadReader)
req.Header.Add("user-agent", agent)
req.Header.Add("content-type", "application/json")
req.Header.Add("accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
sessionId, _ := ioutil.ReadAll(resp.Body)
var id []byte
// Strip quotations from the response
for _, b := range sessionId {
if b != 34 {
id = append(id, b)
}
}
client.DexcomToken = string(id)
return nil
}
func (client *DexcomClient) GetRealTimeData() (*RealTimeData, error) {
url := client.getLatestGlucoseUrl()
req, _ := http.NewRequest("POST", url, strings.NewReader(""))
req.Header.Add("user-agent", agent)
req.Header.Add("content-type", "application/json")
req.Header.Add("accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
body, _ := ioutil.ReadAll(resp.Body)
var realTimeData []RealTimeData
json.Unmarshal(body, &realTimeData)
if len(realTimeData) == 0 {
return nil, errors.New("no real time data returned")
}
return &realTimeData[0], nil
}