-
Notifications
You must be signed in to change notification settings - Fork 6
/
body.go
262 lines (223 loc) · 6.7 KB
/
body.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
package fitbit
import (
"encoding/json"
"fmt"
"strconv"
"time"
)
type GetBody struct {
body *Body `json:"body"`
goals *BodyGoals `json:"goals"`
}
type Body struct {
Bicep uint64 `json:"bicep"`
Bmi float64 `json:"bmi"`
Calf float64 `json:"calf"`
Chest uint64 `json:"chest"`
Fat uint64 `json:"fat"`
Forearm float64 `json:"forearm"`
Hips uint64 `json:"hips"`
Neck uint64 `json:"neck"`
Thigh uint64 `json:"thigh"`
Waist uint64 `json:"waist"`
Weight float64 `json:"weight"`
}
type BodyGoals struct {
Weight float64 `json:"weight"`
}
// GetBody gets all the details of the body for the given user
// It returns an object Body or an error if one occours
func (c *Client) GetBody(date time.Time) (*GetBody, error) {
//Build and GET requestURL
requestURL := fmt.Sprintf("user/-/body/date/%s.json", date.Format("2006-01-02"))
responseBody, err := c.getData(requestURL)
if err != nil {
return nil, err
}
//Parse data
bodyData := &GetBody{}
err = json.NewDecoder(responseBody).Decode(bodyData)
if err != nil {
return nil, err
}
return bodyData, nil
}
// LogBody contains all the details of body measurements
type LogBody struct {
Body *Body `json:"body"`
}
// GetBodyMeasurements gets all the body measurements for the given user
// It returns an error if one occours
func (c *Client) GetBodyMeasurements(date time.Time) (*LogBody, error) {
//Build and get request-URL
requestURL := fmt.Sprintf("user/-/body/date/%s.json", date.Format("2006-01-02"))
responseBody, err := c.getData(requestURL)
if err != nil {
return nil, err
}
//Parse data
bodyData := &LogBody{}
err = json.NewDecoder(responseBody).Decode(bodyData)
if err != nil {
return nil, err
}
return bodyData, nil
}
// LogBody adds all the body measurements for the given user
// It returns an error if one occours
func (c *Client) LogBodyMeasurements(date time.Time, bicep, calf, chest, fat, forearm, hips, neck, thigh, waist, weight float64) (*LogBody, error) {
//Build dataArguments
dataArguments := map[string]string{
"bicep": strconv.FormatFloat(bicep, 'f', 2, 32),
"calf": strconv.FormatFloat(calf, 'f', 2, 32),
"chest": strconv.FormatFloat(chest, 'f', 2, 32),
"fat": strconv.FormatFloat(fat, 'f', 2, 32),
"forearm": strconv.FormatFloat(forearm, 'f', 2, 32),
"hips": strconv.FormatFloat(hips, 'f', 2, 32),
"neck": strconv.FormatFloat(neck, 'f', 2, 32),
"thigh": strconv.FormatFloat(thigh, 'f', 2, 32),
"waist": strconv.FormatFloat(waist, 'f', 2, 32),
"weight": strconv.FormatFloat(weight, 'f', 2, 32),
"date": date.Format("2006-01-02"),
}
//Build and POST requestURL
responseBody, err := c.postData("user/-/body.json", dataArguments)
if err != nil {
return nil, err
}
//Parse data
logBodyData := &LogBody{}
err = json.NewDecoder(responseBody).Decode(logBodyData)
if err != nil {
return nil, err
}
return logBodyData, nil
}
// Weighting contains the details of the user's weight
type Weight struct {
Bmi float64 `json:"bmi"`
Date string `json:"date"`
LogID uint64 `json:"logId"`
Time string `json:"time"`
Weight float64 `json:"weight"`
BodyFat float64 `json:"fat"`
}
type LogWeight struct {
WeightLog *Weight `json:"weightLog"`
}
// GetBodyWeight gets the body weight for the given user on a specific date
// It returns an error if one occours
func (c *Client) GetBodyWeight(date time.Time) (*LogWeight, error) {
//Build and get request-URL
requestURL := fmt.Sprintf("user/-/body/log/weight/date/%s.json", date.Format("2006-01-02"))
responseBody, err := c.getData(requestURL)
if err != nil {
return nil, err
}
//Parse data
bodyData := &LogWeight{}
err = json.NewDecoder(responseBody).Decode(bodyData)
if err != nil {
return nil, err
}
return bodyData, nil
}
// LogWeight logs user's weight
// It returns an object Weight or an error if one occours
func (c *Client) LogBodyWeight(date time.Time, weight float64) (*LogWeight, error) {
//Build dataArguments
dataArguments := map[string]string{
"weight": strconv.FormatFloat(weight, 'f', 2, 32),
"date": date.Format("2006-01-02"),
"time": date.Format("15:04:05"),
}
//Buid and POST requestURL
responseBody, err := c.postData("user/-/body/log/weight.json", dataArguments)
if err != nil {
return nil, err
}
//Parse data
weightingData := &LogWeight{}
err = json.NewDecoder(responseBody).Decode(weightingData)
if err != nil {
return nil, err
}
return weightingData, nil
}
// DeleteBodyWeight removes an body weight measurement
// It returns an error if on occours
func (c *Client) DeleteBodyWeight(weightID uint64) error {
//Build requestURL and DELETE data
requestURL := fmt.Sprintf("user/-/body/log/weight/%d.json", weightID)
_, err := c.deleteData(requestURL, nil)
if err != nil {
return err
}
return nil
}
// Weighting contains the details of the user's weight
type Fat struct {
BMI float64 `json:"bmi"`
Date string `json:"date"`
Fat float64 `json:"fat"`
LogID uint64 `json:"logId"`
Time string `json:"time"`
}
// GetFat struct that contains all the measurements of body fat
type GetFat struct {
GetFat []*Fat `json:"fat"`
}
// LogFat is a object that is returned by the server when a fat measurement is logged
type LogFat struct {
FatLog *Fat `json:"fatLog"`
}
// GetBodyFat gets the body fat measurements for the given user on a specific date
// It returns an error if one occours
func (c *Client) GetBodyFat(date time.Time) (*GetFat, error) {
//Build and get request-URL
requestURL := fmt.Sprintf("user/-/body/log/fat/date/%s.json", date.Format("2006-01-02"))
responseBody, err := c.getData(requestURL)
if err != nil {
return nil, err
}
//Parse data
bodyData := &GetFat{}
err = json.NewDecoder(responseBody).Decode(bodyData)
if err != nil {
return nil, err
}
return bodyData, nil
}
// LogWeight logs user's weight
// It returns an object Weight or an error if one occours
func (c *Client) LogBodyFat(date time.Time, fat float64) (*LogFat, error) {
//Build dataArguments
dataArguments := map[string]string{
"fat": strconv.FormatFloat(fat, 'f', 2, 32),
"date": date.Format("2006-01-02"),
"time": date.Format("15:04:05"),
}
//Buid and POST requestURL
responseBody, err := c.postData("user/-/body/log/fat.json", dataArguments)
if err != nil {
return nil, err
}
//Parse data
weightingData := &LogFat{}
err = json.NewDecoder(responseBody).Decode(weightingData)
if err != nil {
return nil, err
}
return weightingData, nil
}
// DeleteBodyFat removes an body fat measurement
// It returns an error if on occours
func (c *Client) DeleteBodyFat(fatID uint64) error {
//Build requestURL and DELETE data
requestURL := fmt.Sprintf("user/-/body/log/fat/%d.json", fatID)
_, err := c.deleteData(requestURL, nil)
if err != nil {
return err
}
return nil
}