-
Notifications
You must be signed in to change notification settings - Fork 29
/
Activity.go
229 lines (185 loc) · 6.2 KB
/
Activity.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
package connect
import (
"archive/zip"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"strings"
)
// Activity describes a Garmin Connect activity.
type Activity struct {
ID int `json:"activityId"`
ActivityName string `json:"activityName"`
Description string `json:"description"`
StartLocal Time `json:"startTimeLocal"`
StartGMT Time `json:"startTimeGMT"`
ActivityType ActivityType `json:"activityType"`
Distance float64 `json:"distance"` // meter
Duration float64 `json:"duration"`
ElapsedDuration float64 `json:"elapsedDuration"`
MovingDuration float64 `json:"movingDuration"`
AverageSpeed float64 `json:"averageSpeed"`
MaxSpeed float64 `json:"maxSpeed"`
OwnerID int `json:"ownerId"`
Calories float64 `json:"calories"`
AverageHeartRate float64 `json:"averageHR"`
MaxHeartRate float64 `json:"maxHR"`
DeviceID int `json:"deviceId"`
}
// ActivityType describes the type of activity.
type ActivityType struct {
TypeID int `json:"typeId"`
TypeKey string `json:"typeKey"`
ParentTypeID int `json:"parentTypeId"`
SortOrder int `json:"sortOrder"`
}
// Activity will retrieve details about an activity.
func (c *Client) Activity(activityID int) (*Activity, error) {
URL := fmt.Sprintf("https://connect.garmin.com/modern/proxy/activity-service/activity/%d",
activityID,
)
activity := new(Activity)
err := c.getJSON(URL, &activity)
if err != nil {
return nil, err
}
return activity, nil
}
// Activities will list activities for displayName. If displayName is empty,
// the authenticated user will be used.
func (c *Client) Activities(displayName string, start int, limit int) ([]Activity, error) {
URL := fmt.Sprintf("https://connect.garmin.com/modern/proxy/activitylist-service/activities/%s?start=%d&limit=%d", displayName, start, limit)
if !c.authenticated() && displayName == "" {
return nil, ErrNotAuthenticated
}
var proxy struct {
List []Activity `json:"activityList"`
}
err := c.getJSON(URL, &proxy)
if err != nil {
return nil, err
}
return proxy.List, nil
}
// RenameActivity can be used to rename an activity.
func (c *Client) RenameActivity(activityID int, newName string) error {
URL := fmt.Sprintf("https://connect.garmin.com/modern/proxy/activity-service/activity/%d", activityID)
payload := struct {
ID int `json:"activityId"`
Name string `json:"activityName"`
}{activityID, newName}
return c.write("PUT", URL, payload, 204)
}
// ExportActivity will export an activity from Connect. The activity will be written til w.
func (c *Client) ExportActivity(id int, w io.Writer, format ActivityFormat) error {
formatTable := [activityFormatMax]string{
"https://connect.garmin.com/modern/proxy/download-service/files/activity/%d",
"https://connect.garmin.com/modern/proxy/download-service/export/tcx/activity/%d",
"https://connect.garmin.com/modern/proxy/download-service/export/gpx/activity/%d",
"https://connect.garmin.com/modern/proxy/download-service/export/kml/activity/%d",
"https://connect.garmin.com/modern/proxy/download-service/export/csv/activity/%d",
}
if format >= activityFormatMax || format < ActivityFormatFIT {
return errors.New("invalid format")
}
URL := fmt.Sprintf(formatTable[format], id)
// To unzip FIT files on-the-fly, we treat them specially.
if format == ActivityFormatFIT {
buffer := bytes.NewBuffer(nil)
err := c.Download(URL, buffer)
if err != nil {
return err
}
z, err := zip.NewReader(bytes.NewReader(buffer.Bytes()), int64(buffer.Len()))
if err != nil {
return err
}
if len(z.File) != 1 {
return fmt.Errorf("%d files found in FIT archive, 1 expected", len(z.File))
}
src, err := z.File[0].Open()
if err != nil {
return err
}
defer src.Close()
_, err = io.Copy(w, src)
return err
}
return c.Download(URL, w)
}
// ImportActivity will import an activity into Garmin Connect. The activity
// will be read from file.
func (c *Client) ImportActivity(file io.Reader, format ActivityFormat) (int, error) {
URL := "https://connect.garmin.com/modern/proxy/upload-service/upload/." + format.Extension()
switch format {
case ActivityFormatFIT, ActivityFormatTCX, ActivityFormatGPX:
// These are ok.
default:
return 0, fmt.Errorf("%s is not supported for import", format.Extension())
}
formData := bytes.Buffer{}
writer := multipart.NewWriter(&formData)
defer writer.Close()
activity, err := writer.CreateFormFile("file", "activity."+format.Extension())
if err != nil {
return 0, err
}
_, err = io.Copy(activity, file)
if err != nil {
return 0, err
}
writer.Close()
req, err := c.newRequest("POST", URL, &formData)
if err != nil {
return 0, err
}
req.Header.Add("content-type", writer.FormDataContentType())
resp, err := c.do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
// Implement enough of the response to satisfy our needs.
var response struct {
ImportResult struct {
Successes []struct {
InternalID int `json:"internalId"`
} `json:"successes"`
Failures []struct {
Messages []struct {
Content string `json:"content"`
} `json:"messages"`
} `json:"failures"`
} `json:"detailedImportResult"`
}
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return 0, err
}
// This is ugly.
if len(response.ImportResult.Failures) > 0 {
messages := make([]string, 0, 10)
for _, f := range response.ImportResult.Failures {
for _, m := range f.Messages {
messages = append(messages, m.Content)
}
}
return 0, errors.New(strings.Join(messages, "; "))
}
if resp.StatusCode != 201 {
return 0, fmt.Errorf("%d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
if len(response.ImportResult.Successes) != 1 {
return 0, Error("cannot parse response, no failures and no successes..?")
}
return response.ImportResult.Successes[0].InternalID, nil
}
// DeleteActivity will permanently delete an activity.
func (c *Client) DeleteActivity(id int) error {
URL := fmt.Sprintf("https://connect.garmin.com/modern/proxy/activity-service/activity/%d", id)
return c.write("DELETE", URL, nil, 0)
}