-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgoals.go
More file actions
263 lines (223 loc) · 7.02 KB
/
goals.go
File metadata and controls
263 lines (223 loc) · 7.02 KB
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
package fitbit
import (
"encoding/json"
"errors"
"strconv"
"time"
)
// GetWeightGoal is a object that is used to accept the return data
type GetWeightGoal struct {
Goal struct {
StartDate string `json:"startDate"`
StartWeight uint64 `json:"startWeight"`
Weight uint64 `json:"weight"`
} `json:"goal"`
}
// GetBodyWeightGoal is used to get the body weight goal for the given user
// The object GetWeightGoal is returned or an error if one occours
func (c *Client) GetBodyWeightGoals() (*GetWeightGoal, error) {
//Build and GET requestURL
responseBody, err := c.getData("user/-/body/log/weight/goal.json")
if err != nil {
return nil, err
}
//Parse data
weightData := &GetWeightGoal{}
err = json.NewDecoder(responseBody).Decode(weightData)
if err != nil {
return nil, err
}
return weightData, nil
}
// UpdateBodyWeightGoal is used to update the body weight goal for the given user
// The object GetWeightGoal is returned or an error if one occours
func (c *Client) UpdateBodyWeightGoal(date time.Time, startWeight, weight float64) (*GetWeightGoal, error) {
//Build arguments map
dataArguments := map[string]string{
"startDate": date.Format("2006-01-02"),
"startWeight": strconv.FormatFloat(startWeight, 'f', 2, 32),
}
//Check for missing parameters
if startWeight == 0 && weight == 0 {
return nil, errors.New("missing parameters")
}
//Add weight
if weight > 0 {
dataArguments["weight"] = strconv.FormatFloat(weight, 'f', 2, 32)
}
//Buid and POST requestURL
responseBody, err := c.postData("user/-/body/log/weight/goal.json", dataArguments)
if err != nil {
return nil, err
}
//Parse data
weightData := &GetWeightGoal{}
err = json.NewDecoder(responseBody).Decode(weightData)
if err != nil {
return nil, err
}
return weightData, nil
}
// GetFatGoal is a container used to receive the response from the server
type GetFatGoal struct {
Goal struct {
BodyFat uint64 `json:"bodyFat"`
} `json:"goal"`
}
// GetBodyFatGoal is used to get the body fat goal for the given user
// The object GetFatGoal is returned or an error if one occours
func (c *Client) GetBodyFatGoals() (*GetFatGoal, error) {
//Build and GET requestURL
responseBody, err := c.getData("user/-/body/log/fat/goal.json")
if err != nil {
return nil, err
}
//Parse data
fatData := &GetFatGoal{}
err = json.NewDecoder(responseBody).Decode(fatData)
if err != nil {
return nil, err
}
return fatData, nil
}
// UpdateBodyFatGoal is used to update the body fat goal for the given user
// The object GetFatGoal is returned or an error if one occours
func (c *Client) UpdateBodyFatGoal(fat float64) (*GetFatGoal, error) {
//Build arguments map
dataArguments := map[string]string{
"fat": strconv.FormatFloat(fat, 'f', 2, 32),
}
//Check for missing parameters
if fat == 0 {
return nil, errors.New("missing parameters")
}
//Buid and POST requestURL
responseBody, err := c.postData("user/-/body/log/fat/goal.json", dataArguments)
if err != nil {
return nil, err
}
//Parse data
fatData := &GetFatGoal{}
err = json.NewDecoder(responseBody).Decode(fatData)
if err != nil {
return nil, err
}
return fatData, nil
}
// GetActivityDailyGoal is a container used to receive the response from the server
type GetActivityGoal struct {
Goals struct {
CaloriesOut uint64 `json:"caloriesOut"`
Distance float64 `json:"distance"`
ActiveMinutes uint64 `json:"activeMinutes"`
Floors uint64 `json:"floors"`
Steps uint64 `json:"steps"`
} `json:"goals"`
}
// GetActivityDailyGoals is used to get the daily activities goal for the given user
// The object GetActivityGoal is returned or an error if one occours
func (c *Client) GetActivityDailyGoals() (*GetActivityGoal, error) {
//Build and GET requestURL
responseBody, err := c.getData("user/-/activities/goals/daily.json")
if err != nil {
return nil, err
}
//Parse data
activityData := &GetActivityGoal{}
err = json.NewDecoder(responseBody).Decode(activityData)
if err != nil {
return nil, err
}
return activityData, nil
}
// UpdateActivityDailyGoals is used to update the body fat goal for the given user
// The object GetActivityGoal is returned or an error if one occours
func (c *Client) UpdateActivityDailyGoals(caloriesOut, activeMinutes, floors, steps uint64, distance float64) (*GetActivityGoal, error) {
//Build arguments map
dataArguments := map[string]string{
"caloriesOut": strconv.FormatUint(caloriesOut, 10),
"activeMinutes": strconv.FormatUint(activeMinutes, 10),
"floors": strconv.FormatUint(floors, 10),
"steps": strconv.FormatUint(steps, 10),
"distance": strconv.FormatFloat(distance, 'f', 2, 32),
}
//Check for missing parameters
if caloriesOut == 0 || activeMinutes == 0 || floors == 0 || steps == 0 || distance == 0 {
return nil, errors.New("missing parameters")
}
//Buid and POST requestURL
responseBody, err := c.postData("user/-/activities/goals/daily.json", dataArguments)
if err != nil {
return nil, err
}
//Parse data
activityData := &GetActivityGoal{}
err = json.NewDecoder(responseBody).Decode(activityData)
if err != nil {
return nil, err
}
return activityData, nil
}
// GetActivityWeeklyGoals is used to get the daily activities goal for the given user
// The object GetActivityGoal is returned or an error if one occours
func (c *Client) GetActivityWeeklyGoals() (*GetActivityGoal, error) {
//Build and GET requestURL
responseBody, err := c.getData("user/-/activities/goals/weekly.json")
if err != nil {
return nil, err
}
//Parse data
activityData := &GetActivityGoal{}
err = json.NewDecoder(responseBody).Decode(activityData)
if err != nil {
return nil, err
}
return activityData, nil
}
// GetFoodGoal is a object that is used to receive the server resposne
type GetFoodGoal struct {
Goals struct {
Calories uint64 `json:"calories"`
} `json:"calories"`
FoodPlan struct {
Intensity string `json:"intensity"`
EstimatedDate string `json:"estimatedDate"`
Personalized bool `json:"personalized"`
} `json:"foodPlan"`
}
// GetFoodGoals is used to get the food goal for the given user
// The object GetFoodGoal is returned or an error if one occours
func (c *Client) GetFoodGoals() (*GetFoodGoal, error) {
//Build and GET requestURL
responseBody, err := c.getData("user/-/foods/log/goal.json")
if err != nil {
return nil, err
}
//Parse data
foodData := &GetFoodGoal{}
err = json.NewDecoder(responseBody).Decode(foodData)
if err != nil {
return nil, err
}
return foodData, nil
}
// GetWaterGoal is a object that is used to receive the server resposne
type GetWaterGoal struct {
Goal uint64 `json:"water"`
}
// GetWaterGoals is used to get the water goal for the given user
// The object GetWaterGoal is returned or an error if one occours
func (c *Client) GetWaterGoals() (*GetWaterGoal, error) {
//Build and GET requestURL
responseBody, err := c.getData("user/-/foods/log/water/goal.json")
if err != nil {
return nil, err
}
//Parse data
waterData := &GetWaterGoal{}
err = json.NewDecoder(responseBody).Decode(waterData)
if err != nil {
return nil, err
}
return waterData, nil
}