-
Notifications
You must be signed in to change notification settings - Fork 0
/
lap.go
50 lines (41 loc) · 1.26 KB
/
lap.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
package strava
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
// AveragePace calculates the average pace for the lap
func (l Lap) AveragePace() time.Duration {
return ConvertSpeedToPace(l.AverageSpeed)
}
// MaxPace calculates the maximum pace for the lap
func (l Lap) MaxPace() time.Duration {
return ConvertSpeedToPace(l.MaxSpeed)
}
// MovingDuration returns the moving time of the lap as a time.Duration
func (l Lap) MovingDuration() time.Duration {
return time.Duration(l.MovingTime) * time.Second
}
// ElapsedDuration returns the elapsed time of the lap as a time.Duration
func (l Lap) ElapsedDuration() time.Duration {
return time.Duration(l.ElapsedTime) * time.Second
}
// GetActivityLaps retrieves the laps of an activity
func (c *Client) GetActivityLaps(ctx context.Context, athleteID, activityID uint) ([]*Lap, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/activities/%d/laps", APIBaseURL, activityID), nil)
if err != nil {
return nil, fmt.Errorf("could not create request: %w", err)
}
body, err := c.call(ctx, athleteID, req, c.maxRetries)
if err != nil {
return nil, fmt.Errorf("could not call: %w", err)
}
var laps []*Lap
err = json.Unmarshal(body, &laps)
if err != nil {
return nil, err
}
return laps, nil
}