/
rides.go
211 lines (192 loc) · 5.5 KB
/
rides.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
package cyclinganalytics
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"math"
"mime/multipart"
"net/http"
"net/url"
"strings"
"github.com/bzimmer/gravl/pkg/providers/activity"
)
// RidesService manages rides for a user
type RidesService service
// RideOptions specify additional detail to return for a queried ride
type RideOptions struct {
// Streams is a list of valid data streams
Streams []string
// Curves specifies the difference curves to return
Curves struct {
// AveragePower will be returned if true
AveragePower bool
// EffectivePower will be returned if true
EffectivePower bool
}
}
const (
meupload = "me/upload"
)
func (r *RideOptions) values() (*url.Values, error) {
v := &url.Values{}
if r.Streams != nil {
if err := validateStreams(r.Streams); err != nil {
return nil, err
}
v.Set("streams", strings.Join(r.Streams, ","))
}
if r.Curves.AveragePower && r.Curves.EffectivePower {
v.Set("curves", "true")
} else {
v.Set("power_curve", fmt.Sprintf("%t", r.Curves.AveragePower))
v.Set("epower_curve", fmt.Sprintf("%t", r.Curves.EffectivePower))
}
return v, nil
}
// Ride returns a single ride with available options
func (s *RidesService) Ride(ctx context.Context, rideID int64, opts RideOptions) (*Ride, error) {
uri := fmt.Sprintf("ride/%d", rideID)
params, err := opts.values()
if err != nil {
return nil, err
}
req, err := s.client.newAPIRequest(ctx, http.MethodGet, uri, params, nil)
if err != nil {
return nil, err
}
ride := &Ride{}
err = s.client.do(req, ride)
if err != nil {
return nil, err
}
return ride, nil
}
// Rides returns a slice of rides for the user
func (s *RidesService) Rides(ctx context.Context, userID UserID, spec activity.Pagination) ([]*Ride, error) {
uri := "me/rides"
if userID != Me {
uri = fmt.Sprintf("%d/rides", userID)
}
req, err := s.client.newAPIRequest(ctx, http.MethodGet, uri, nil, nil)
if err != nil {
return nil, err
}
type rides struct {
Rides []*Ride `json:"rides"`
}
res := &rides{}
err = s.client.do(req, res)
if err != nil {
return nil, err
}
if spec.Total > 0 {
n := math.Min(float64(len(res.Rides)), float64(spec.Total))
res.Rides = res.Rides[:int(n)]
}
return res.Rides, nil
}
// Upload the file for the authenticated user
func (s *RidesService) Upload(ctx context.Context, file *activity.File) (*Upload, error) {
return s.UploadWithUser(ctx, Me, file)
}
// Upload the file for the user
func (s *RidesService) UploadWithUser(ctx context.Context, userID UserID, file *activity.File) (*Upload, error) {
if file == nil {
return nil, errors.New("missing upload file")
}
uri := meupload
if userID != Me {
uri = fmt.Sprintf("user/%d/upload", userID)
}
var b bytes.Buffer
w := multipart.NewWriter(&b)
if err := w.WriteField("filename", file.Name); err != nil {
return nil, err
}
fw, err := w.CreateFormFile("data", file.Name)
if err != nil {
return nil, err
}
if _, err = io.Copy(fw, file); err != nil {
return nil, err
}
if err = w.Close(); err != nil {
return nil, err
}
req, err := s.client.newAPIRequest(ctx, http.MethodPost, uri, nil, &b)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", w.FormDataContentType())
res := &Upload{}
err = s.client.do(req, res)
if err != nil {
return nil, err
}
return res, nil
}
// Status returns the status of an upload request
func (s *RidesService) Status(ctx context.Context, uploadID int64) (*Upload, error) {
return s.StatusWithUser(ctx, Me, uploadID)
}
// Status returns the status of an upload request for the user
func (s *RidesService) StatusWithUser(ctx context.Context, userID UserID, uploadID int64) (*Upload, error) {
uri := meupload
if userID != Me {
uri = fmt.Sprintf("user/%d/upload", userID)
}
uri = fmt.Sprintf("%s/%d", uri, uploadID)
req, err := s.client.newAPIRequest(ctx, http.MethodGet, uri, nil, nil)
if err != nil {
return nil, err
}
res := &Upload{}
err = s.client.do(req, res)
if err != nil {
return nil, err
}
return res, nil
}
// AvailableStreams returns the list of valid stream names
func (s *RidesService) StreamSets() map[string]string {
q := make(map[string]string)
for k, v := range streamsets {
q[k] = v
}
return q
}
func validateStreams(streams []string) error {
for i := range streams {
_, ok := streamsets[streams[i]]
if !ok {
return fmt.Errorf("invalid stream '%s'", streams[i])
}
}
return nil
}
// https://www.cyclinganalytics.com/developer/api#/ride/ride_id
var streamsets = map[string]string{
"cadence": "",
"distance": "The sequence of distance values for this stream, in kilometers [float]",
"elevation": "The sequence of elevation values for this stream, in meters [float]",
"gears": "",
"gradient": "The sequence of grade values for this stream, as percents of a grade [float]",
"heart_rate_variability": "",
"heartrate": "The sequence of heart rate values for this stream, in beats per minute [integer]",
"latitude": "",
"longitude": "",
"lrbalance": "",
"pedal_smoothness": "",
"platform_center_offset": "",
"power_direction": "",
"power_phase": "",
"power": "",
"respiration_rate": "",
"smo2": "",
"speed": "The sequence of speed values for this stream, in meters per second [float]",
"temperature": "The sequence of temperature values for this stream, in celsius degrees [float]",
"thb": "",
"torque_effectiveness": "",
}