-
Notifications
You must be signed in to change notification settings - Fork 4
/
body_fat.go
78 lines (65 loc) · 2.02 KB
/
body_fat.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
package fitbit
import (
"encoding/json"
"fmt"
)
// BodyFat contains one or multiple records, similar to BodyFat but without weight
type BodyFat struct {
Fat []struct {
Date string `json:"date"`
Fat float64 `json:"fat"`
LogID int64 `json:"logId"`
Source string `json:"source"`
Time string `json:"time"`
} `json:"fat"`
}
// BodyFatLogByDay returns the fat log by a given date
// date must be in the format yyyy-MM-dd
func (m *Session) BodyFatLogByDay(day string) (BodyFat, error) {
contents, err := m.makeRequest(fmt.Sprintf("https://api.fitbit.com/1/user/-/body/log/fat/date/%s.json", day))
if err != nil {
return BodyFat{}, err
}
fat := BodyFat{}
if err := json.Unmarshal(contents, &fat); err != nil {
return BodyFat{}, err
}
return fat, nil
}
// BodyFatLogByDateRange returns the weight log of a given time range by date
// date must be in the format yyyy-MM-dd
func (m *Session) BodyFatLogByDateRange(startDay string, endDay string) (BodyFat, error) {
contents, err := m.makeRequest(fmt.Sprintf("https://api.fitbit.com/1/user/-/body/log/fat/date/%s/%s.json", startDay, endDay))
if err != nil {
return BodyFat{}, err
}
fat := BodyFat{}
if err := json.Unmarshal(contents, &fat); err != nil {
return BodyFat{}, err
}
return fat, nil
}
// AddBodyFat adds a new body weight record
// date must be in the format yyyy-MM-dd
func (m *Session) AddBodyFat(day string, fat float64) (BodyFat, error) {
contents, err := m.makePOSTRequest("https://api.fitbit.com/1/user/-/body/log/fat.json", map[string]string{
"date": day,
"fat": fmt.Sprintf("%f", fat),
})
if err != nil {
return BodyFat{}, err
}
fatResponse := BodyFat{}
if err := json.Unmarshal(contents, &fatResponse); err != nil {
return BodyFat{}, err
}
return fatResponse, nil
}
// RemoveBodyFat removes a existing record by it's log ID
func (m *Session) RemoveBodyFat(logID int64) error {
_, err := m.makeDELETERequest(fmt.Sprintf("https://api.fitbit.com/1/user/-/body/log/fat/%d.json", logID))
if err != nil {
return err
}
return nil
}