/
trips.go
188 lines (167 loc) · 4.76 KB
/
trips.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
package rwgps
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"strconv"
"github.com/bzimmer/gravl/pkg/providers/activity"
)
// pageSize default for querying bulk entities (eg trips, routes)
const pageSize = 100
// TripsService provides access to Trips and Routes via the RWGPS API
type TripsService service
type tripsPaginator struct {
service TripsService
userID UserID
trips []*Trip
kind string
}
func (p *tripsPaginator) PageSize() int {
return pageSize
}
func (p *tripsPaginator) Count() int {
return len(p.trips)
}
func (p *tripsPaginator) Do(ctx context.Context, spec activity.Pagination) (int, error) {
uri := fmt.Sprintf("users/%d/%s.json", p.userID, p.kind)
params := map[string]string{
// pagination uses the concept of page (based on strava), rwgps uses an offset by row
// since pagination starts with page 1 (again, strava), subtract one from `start`
"offset": strconv.FormatInt(int64((spec.Start-1)*p.PageSize()), 10),
"limit": strconv.FormatInt(int64(spec.Count), 10),
}
req, err := p.service.client.newAPIRequest(ctx, uri, params)
if err != nil {
return 0, err
}
type TripsResponse struct {
Results []*Trip `json:"results"`
ResultsCount int `json:"results_count"`
}
res := &TripsResponse{}
err = p.service.client.do(req, res)
if err != nil {
return 0, err
}
if spec.Total > 0 && len(p.trips)+len(res.Results) > spec.Total {
res.Results = res.Results[:spec.Total-len(p.trips)]
}
p.trips = append(p.trips, res.Results...)
return len(res.Results), nil
}
// Trips returns a slice of trips
func (s *TripsService) Trips(ctx context.Context, userID UserID, spec activity.Pagination) ([]*Trip, error) {
p := &tripsPaginator{service: *s, userID: userID, kind: "trips", trips: make([]*Trip, 0)}
err := activity.Paginate(ctx, p, spec)
if err != nil {
return nil, err
}
return p.trips, nil
}
// Routes returns a slice of routes
func (s *TripsService) Routes(ctx context.Context, userID UserID, spec activity.Pagination) ([]*Trip, error) {
p := &tripsPaginator{service: *s, userID: userID, kind: "routes", trips: make([]*Trip, 0)}
err := activity.Paginate(ctx, p, spec)
if err != nil {
return nil, err
}
return p.trips, nil
}
// Trip returns a trip for the `tripID`
func (s *TripsService) Trip(ctx context.Context, tripID int64) (*Trip, error) {
return s.trip(ctx, TypeTrip, fmt.Sprintf("trips/%d.json", tripID))
}
// Route returns a trip for the `routeID`
func (s *TripsService) Route(ctx context.Context, routeID int64) (*Trip, error) {
return s.trip(ctx, TypeRoute, fmt.Sprintf("routes/%d.json", routeID))
}
func (s *TripsService) trip(ctx context.Context, entity Type, uri string) (*Trip, error) {
req, err := s.client.newAPIRequest(ctx, uri, nil)
if err != nil {
return nil, err
}
type TripResponse struct {
Type string `json:"type"`
Trip *Trip `json:"trip"`
Route *Trip `json:"route"`
}
res := &TripResponse{}
err = s.client.do(req, res)
if err != nil {
return nil, err
}
var t *Trip
switch entity {
case TypeTrip:
t = res.Trip
case TypeRoute:
t = res.Route
}
t.Type = entity.String()
return t, nil
}
// Upload the file for the user
func (s *TripsService) Upload(ctx context.Context, file *activity.File) (*Upload, error) {
if file == nil || file.Name == "" || file.Format == activity.Original {
return nil, errors.New("missing upload file, name, or format")
}
var b bytes.Buffer
w := multipart.NewWriter(&b)
for k, v := range map[string]string{
"filename": file.Name,
"trip[name]": "",
"trip[description]": "",
"trip[bad_elevations]": "false",
"version": apiVersion,
"apikey": s.client.config.ClientID,
"auth_token": s.client.token.AccessToken,
} {
if err := w.WriteField(k, v); err != nil {
return nil, err
}
}
fw, err := w.CreateFormFile("file", 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
}
uri := fmt.Sprintf("%s/trips.json", baseURL)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, &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 *TripsService) Status(ctx context.Context, uploadID int64) (*Upload, error) {
uri := "queued_tasks/status.json"
id := strconv.FormatInt(uploadID, 10)
req, err := s.client.newAPIRequest(ctx, uri, map[string]string{
"ids": id,
"include_objects": "false",
})
if err != nil {
return nil, err
}
res := &Upload{}
err = s.client.do(req, res)
if err != nil {
return nil, err
}
return res, nil
}