-
Notifications
You must be signed in to change notification settings - Fork 0
/
egvs.go
63 lines (53 loc) · 1.66 KB
/
egvs.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
package dexcom
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
)
type getEGVsResponse struct {
Unit string `json:"unit"`
rateUnit string `json:"rateUnit"`
EGVs []EGV `json:"egvs"`
}
type EGV struct {
SystemTime Time `json:"systemTime"`
DisplayTime Time `json:"displayTime"`
Value int `json:"value"`
RealtimeValue int `json:"realtimeValue"`
SmoothedValue int `json:"smoothedValue"`
Status string `json:"status"`
Trend string `json:"trend"`
TrendRate float64 `json:"trendRate"`
}
// GetEGVs will return all of the EGVs in the given time range or an error describing the failed network call
func (c *Client) GetEGVs(start, end time.Time) ([]EGV, error) {
uri, err := url.Parse(fmt.Sprintf("%s/v2/users/self/egvs", c.Endpoint))
if err != nil {
return nil, err
}
vals := uri.Query()
vals.Add("startDate", start.Format("2006-01-02T15:04:05"))
vals.Add("endDate", end.Format("2006-01-02T15:04:05"))
uri.RawQuery = vals.Encode()
req, err := http.NewRequest(http.MethodGet, uri.String(), nil)
if err != nil {
c.Logger.Log("level", "error", "msg", "could make new request", "err", err.Error())
return nil, err
}
resp, err := c.c.Do(req)
if err != nil {
c.Logger.Log("level", "error", "msg", "could not do request", "err", err.Error())
return nil, err
}
defer resp.Body.Close()
var egvs getEGVsResponse
err = json.NewDecoder(resp.Body).Decode(&egvs)
if err != nil {
c.Logger.Log("level", "error", "msg", "could not json decode response", "err", err.Error())
return nil, err
}
c.Logger.Log("level", "info", "msg", "received EGVs", "n", len(egvs.EGVs))
return egvs.EGVs, nil
}