-
Notifications
You must be signed in to change notification settings - Fork 4
/
activity_log.go
227 lines (204 loc) · 7.66 KB
/
activity_log.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
package fitbit
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strconv"
"time"
)
// ActivityLog returns the activity log for the given configuration
func (m *Session) ActivityLog(params LogListParameters) (ActivitiesLogList, error) {
parameterList := url.Values{}
//nolint:gocritic
if params.BeforeDate != "" {
parameterList.Add("beforeDate", params.BeforeDate)
parameterList.Add("sort", "desc")
} else if params.AfterDate != "" {
parameterList.Add("afterDate", params.BeforeDate)
parameterList.Add("sort", "asc")
} else {
return ActivitiesLogList{}, errors.New("beforeDate or afterDate must be given")
}
if params.Limit > 0 {
if params.Limit > 20 {
return ActivitiesLogList{}, errors.New("limit must be 20 or less")
}
parameterList.Add("limit", strconv.Itoa(params.Limit))
}
parameterList.Add("offset", strconv.Itoa(params.Offset))
contents, err := m.makeRequest("https://api.fitbit.com/1/user/-/activities/list.json?" + parameterList.Encode())
if err != nil {
return ActivitiesLogList{}, err
}
activityResponse := ActivitiesLogList{}
if err := json.Unmarshal(contents, &activityResponse); err != nil {
return ActivitiesLogList{}, err
}
return activityResponse, nil
}
// LogListParameters contains parameters for the activity log list
type LogListParameters struct {
BeforeDate string
AfterDate string
Limit int
Offset int
}
// ActivitiesLogList contains the activity log list
type ActivitiesLogList struct {
Activities []struct {
ActiveDuration int `json:"activeDuration"`
ActiveZoneMinutes struct {
MinutesInHeartRateZones []struct {
MinuteMultiplier int `json:"minuteMultiplier"`
Minutes int `json:"minutes"`
Order int `json:"order"`
Type string `json:"type"`
ZoneName string `json:"zoneName"`
} `json:"minutesInHeartRateZones"`
} `json:"activeZoneMinutes"`
ActivityLevel []struct {
Minutes int `json:"minutes"`
Name string `json:"name"`
} `json:"activityLevel"`
ActivityName string `json:"activityName"`
ActivityTypeID int `json:"activityTypeId"`
Calories int `json:"calories"`
CaloriesLink string `json:"caloriesLink"`
Distance float64 `json:"distance"`
DistanceUnit string `json:"distanceUnit"`
Duration int `json:"duration"`
ElevationGain float64 `json:"elevationGain"`
HasActiveZoneMinutes bool `json:"hasActiveZoneMinutes"`
LastModified time.Time `json:"lastModified"`
LogID int64 `json:"logId"`
LogType string `json:"logType"`
ManualValuesSpecified struct {
Calories bool `json:"calories"`
Distance bool `json:"distance"`
Steps bool `json:"steps"`
} `json:"manualValuesSpecified"`
OriginalDuration int `json:"originalDuration"`
OriginalStartTime time.Time `json:"originalStartTime"`
PoolLength int `json:"poolLength,omitempty"`
PoolLengthUnit string `json:"poolLengthUnit,omitempty"`
Source struct {
ID string `json:"id"`
Name string `json:"name"`
TrackerFeatures []string `json:"trackerFeatures"`
Type string `json:"type"`
URL string `json:"url"`
} `json:"source"`
Speed float64 `json:"speed"`
StartTime time.Time `json:"startTime"`
SwimLengths int `json:"swimLengths,omitempty"`
AverageHeartRate int `json:"averageHeartRate,omitempty"`
DetailsLink string `json:"detailsLink,omitempty"`
HeartRateLink string `json:"heartRateLink,omitempty"`
HeartRateZones []struct {
CaloriesOut float64 `json:"caloriesOut"`
Max int `json:"max"`
Min int `json:"min"`
Minutes int `json:"minutes"`
Name string `json:"name"`
} `json:"heartRateZones,omitempty"`
Pace float64 `json:"pace,omitempty"`
Steps int `json:"steps,omitempty"`
TcxLink string `json:"tcxLink,omitempty"`
} `json:"activities"`
Pagination struct {
BeforeDate string `json:"beforeDate"`
Limit int `json:"limit"`
Next string `json:"next"`
Offset int `json:"offset"`
Previous string `json:"previous"`
Sort string `json:"sort"`
} `json:"pagination"`
}
// LogActivity logs a new activity
// date must be in the format yyyy-MM-dd
// TODO: TESTME
func (m *Session) LogActivity(activity NewActivity) (NewActivityResponse, error) {
postData := make(map[string]string)
//nolint:gocritic
if activity.ActivityID != 0 {
postData["activityId"] = strconv.Itoa(activity.ActivityID)
} else if activity.ActivityName != "" {
postData["activityName"] = activity.ActivityName
if activity.ManualCalories != 0 {
postData["manualCalories"] = strconv.Itoa(activity.ManualCalories)
} else {
return NewActivityResponse{}, errors.New("manualCalories must be given if activityName is used")
}
} else {
return NewActivityResponse{}, errors.New("activityId or activityName must be given")
}
if activity.StartTime != "" {
postData["startTime"] = activity.StartTime
} else {
return NewActivityResponse{}, errors.New("startTime must be given")
}
if activity.DurationMillis > 0 {
postData["durationMillis"] = strconv.FormatInt(activity.DurationMillis, 10)
} else {
return NewActivityResponse{}, errors.New("durationMillis must be given")
}
if activity.Date != "" {
postData["date"] = activity.Date
} else {
return NewActivityResponse{}, errors.New("date must be given")
}
if activity.Distance > 0 {
postData["distance"] = strconv.FormatFloat(activity.Distance, 'f', 3, 64)
}
if activity.DistanceUnit != "" {
postData["distanceUnit"] = activity.DistanceUnit
}
contents, err := m.makePOSTRequest("https://api.fitbit.com/1/user/-/activities.json", postData)
if err != nil {
return NewActivityResponse{}, err
}
activityResponse := NewActivityResponse{}
if err := json.Unmarshal(contents, &activityResponse); err != nil {
return NewActivityResponse{}, err
}
return activityResponse, nil
}
// NewActivity contains the data for a new activity
//
//nolint:lll
type NewActivity struct {
ActivityID int `json:"activityId,omitempty"`
ActivityName string `json:"activityName,omitempty"`
ManualCalories int `json:"manualCalories,omitempty"`
StartTime string `json:"startTime"` // HH:mm
DurationMillis int64 `json:"durationMillis"` // milliseconds
Date string `json:"date"` // yyyy-MM-dd
Distance float64 `json:"distance,omitempty"` // x.xx
DistanceUnit string `json:"distanceUnit,omitempty"` // Steps units are available only for "Walking" (activityId=90013) and "Running" (activityId=90009) directory activities and their intensity levels.
}
// NewActivityResponse contains the response from a new activity request
type NewActivityResponse struct {
ActivityLog struct {
ActivityID int `json:"activityId"`
ActivityParentID int `json:"activityParentId"`
Calories int `json:"calories"`
Description string `json:"description"`
Distance float64 `json:"distance"`
Duration int `json:"duration"`
IsFavorite bool `json:"isFavorite"`
LogID int64 `json:"logId"`
Name string `json:"name"`
StartTime string `json:"startTime"`
Steps int `json:"steps"`
} `json:"activityLog"`
}
// RemoveActivity deletes an existing activity by activity log id
// TODO: TESTME
func (m *Session) RemoveActivity(id int) error {
_, err := m.makeDELETERequest(fmt.Sprintf("https://api.fitbit.com/1/user/-/activities/%d.json", id))
if err != nil {
return err
}
return nil
}