-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
347 lines (282 loc) · 8.23 KB
/
client.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
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
package main
import (
"bytes"
"context"
"encoding/csv"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"os"
"sort"
"strconv"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
// variables used to embed build information in the binary
var (
BuildSHA string
BuildTime string
Version string
)
const (
APIHost = "snoo-api.happiestbaby.com"
CurrentPath = "/ss/v2/sessions/last"
DataPath = "/ss/v2/sessions/aggregated"
LoginPath = "/us/login"
UserAgent = "SNOO/351 CFNetwork/1121.2 Darwin/19.2.0"
)
// CustomTime handles occasional non-RFC3339 dates of the 2020-08-08 00:00:00.000
type CustomTime struct {
time.Time
}
func (ct *CustomTime) UnmarshalJSON(b []byte) (err error) {
// Get rid of the quotes "" around the value.
s := strings.Trim(string(b), "\"")
if s == "null" {
ct.Time = time.Time{}
return
}
t, err := time.Parse(time.RFC3339, s)
if err != nil {
t, err = time.Parse("2006-01-02 15:04:05.000", s)
}
ct.Time = t
return
}
var nilTime = (time.Time{}).UnixNano()
func (ct *CustomTime) IsSet() bool {
return ct.UnixNano() != nilTime
}
// Day represents aggregated data for a day
type Day struct {
DaySleep int `json:"daySleep"`
Levels []Level `json:"levels"`
LongestSleep int `json:"longestSleep"`
Naps int `json:"naps"`
NightSleep int `json:"nightSleep"`
NightWakings int `json:"nightWakings"`
Timezone string `json:"timezone"`
TotalSleep int `json:"totalSleep"`
}
// Level represents a SNOO session level
type Level struct {
IsActive bool `json:"isActive"`
SessionID string `json:"sessionId"`
StartTime CustomTime `json:"startTime"`
StateDuration int `json:"stateDuration"`
Type string `json:"type"` // soothing, asleep
}
// tokenSession represents a client API session
type tokenSession struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"` // seconds
RefreshToken string `json:"refresh_token"`
}
// Client represents the SNOO client object.
type Client struct {
BaseURL *url.URL
UserAgent string
Version string
username string
password string
tokenExpire time.Time
tokenSession tokenSession
httpClient *http.Client
}
// NewClient creates a new client object.
func NewClient(username, password string) *Client {
return &Client{
BaseURL: &url.URL{
Scheme: "https",
Host: APIHost,
},
UserAgent: UserAgent,
Version: Version,
tokenExpire: time.Time{}, // initialize to min time
username: username,
password: password,
httpClient: http.DefaultClient,
}
}
// newRequestWithContext creates a new request with signed params.
func (c *Client) newRequestWithContext(ctx context.Context, method, path string, params url.Values, payload []byte) (*http.Request, error) {
rel := &url.URL{Path: path}
absoluteURL := c.BaseURL.ResolveReference(rel)
if params != nil {
absoluteURL.RawQuery = params.Encode()
}
req, err := http.NewRequestWithContext(ctx, method, absoluteURL.String(), bytes.NewBuffer(payload))
if err != nil {
return nil, err
}
req.Header.Add("User-Agent", c.UserAgent)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
return req, nil
}
// do executes request and decodes response body.
func (c *Client) do(req *http.Request, v interface{}) error {
resp, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// https://stackoverflow.com/a/46948073/690430
bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyString := string(bodyBytes)
log.Debug(bodyString)
body := ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
if resp.StatusCode >= 400 {
return errors.New(resp.Status)
}
return json.NewDecoder(body).Decode(v)
}
// MakeRequest requests and decodes json result.
func (c *Client) MakeRequest(ctx context.Context, method, path string, params url.Values, payload []byte, v interface{}) error {
log.Debug(path)
req, err := c.newRequestWithContext(ctx, method, path, params, payload)
if err != nil {
return err
}
return c.do(req, &v)
}
// MakeRequestWithToken gets token, requests and decodes json result.
func (c *Client) MakeRequestWithToken(ctx context.Context, method, path string, params url.Values, payload []byte, v interface{}) error {
log.Debug(path)
req, err := c.newRequestWithContext(ctx, method, path, params, payload)
if err != nil {
return err
}
req.Header.Add("Authorization", "Bearer "+c.Token(ctx))
return c.do(req, &v)
}
func (c *Client) HasValidToken() bool {
return time.Now().UTC().Before(c.tokenExpire)
}
// Token requests a token session if it's not expired.
func (c *Client) Token(ctx context.Context) string {
if !c.HasValidToken() {
log.Debug("Getting new token")
values := map[string]string{"username": c.username, "password": c.password}
payload, err := json.Marshal(values)
if err != nil {
log.Fatal(err)
}
// get the current time before making the request
now := time.Now().UTC()
var result tokenSession
err = c.MakeRequest(ctx, http.MethodPost, LoginPath, nil, payload, &result)
if err != nil {
log.Fatal(err)
}
c.tokenSession = result
c.tokenExpire = now.Add(time.Second * time.Duration(result.ExpiresIn))
}
return c.tokenSession.AccessToken
}
// GetHistory returns the session history of the SNOO
func (c *Client) GetHistory(startTime, endTime time.Time) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
w := csv.NewWriter(os.Stdout)
header := []string{
"date",
"naps",
"longest_sleep",
"total_sleep",
"day_sleep",
"night_sleep",
"night_wakings",
"timezone",
}
if err := w.Write(header); err != nil {
log.Fatalln("Error writing header to csv:", err)
}
for d := startTime; d.After(endTime) == false; d = d.AddDate(0, 0, 1) {
var result Day
params := url.Values{}
params.Set("startTime", d.Format("01/02/2006 15:04:05"))
err := c.MakeRequestWithToken(ctx, http.MethodGet, DataPath, params, nil, &result)
if err != nil {
log.Fatal(err)
}
record := []string{
d.String(),
strconv.Itoa(result.Naps),
strconv.Itoa(result.LongestSleep),
strconv.Itoa(result.TotalSleep),
strconv.Itoa(result.DaySleep),
strconv.Itoa(result.NightSleep),
strconv.Itoa(result.NightWakings),
result.Timezone,
}
if err := w.Write(record); err != nil {
log.Fatalln("Error writing record to csv:", err)
}
}
// Write any buffered data to the underlying writer (standard output).
w.Flush()
if err := w.Error(); err != nil {
log.Fatal(err)
}
}
// GetSessions returns the session history of the SNOO
func (c *Client) GetSessions(startTime, endTime time.Time) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Write to standard out
w := csv.NewWriter(os.Stdout)
header := []string{
"session_id",
"start_time",
"end_time",
"duration",
"asleep_duration",
"soothing_duration",
}
if err := w.Write(header); err != nil {
log.Fatalln("Error writing header to csv:\n", err)
}
sessionLevels := make(map[string][]Level)
for d := startTime; d.After(endTime) == false; d = d.AddDate(0, 0, 1) {
var result Day
params := url.Values{}
params.Set("startTime", d.Format("01/02/2006 15:04:05"))
err := c.MakeRequestWithToken(ctx, http.MethodGet, DataPath, params, nil, &result)
if err != nil {
log.Fatal("Error making request:", err)
}
for _, level := range result.Levels {
sessionLevels[level.SessionID] = append(sessionLevels[level.SessionID], level)
}
}
sessions := make([]Session, 0)
for sessionID, levels := range sessionLevels {
sessions = append(sessions, NewSession(sessionID, levels))
}
// Sort session by StartTime
sort.Slice(sessions, func(i, j int) bool {
return sessions[i].StartTime.Before(sessions[j].StartTime)
})
for _, session := range sessions {
record := []string{
session.ID,
session.StartTime.Format("2006-01-02 15:04:05"),
session.EndTime.Format("2006-01-02 15:04:05"),
strconv.Itoa(session.TotalDuration()),
strconv.Itoa(session.AsleepDuration),
strconv.Itoa(session.SoothingDuration),
}
if err := w.Write(record); err != nil {
log.Fatalln("Error writing record to csv:", err)
}
}
// Write any buffered data to the underlying writer (standard output).
w.Flush()
if err := w.Error(); err != nil {
log.Fatal(err)
}
}