-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_example_test.go
82 lines (58 loc) · 1.62 KB
/
client_example_test.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
package examples
import (
"context"
"encoding/json"
"testing"
"github.com/obalunenko/getenv"
"github.com/stretchr/testify/require"
"github.com/obalunenko/strava-api/client"
)
// helper function to print JSON
func printJSON(t testing.TB, v any) {
indent, err := json.MarshalIndent(v, "", " ")
require.NoError(t, err)
t.Log(string(indent))
}
// helper to get a valid API client
func getToken(t testing.TB) string {
token := getenv.EnvOrDefault("STRAVA_ACCESS_TOKEN", "")
if token == "" {
t.Skip("STRAVA_ACCESS_TOKEN not set")
}
return token
}
func TestGetLoggedInAthlete(t *testing.T) {
token := getToken(t)
apiClient, err := client.NewAPIClient(token)
require.NoError(t, err)
ctx := context.Background()
athlete, err := apiClient.Athletes.GetLoggedInAthlete(ctx)
require.NoError(t, err)
// Indent athlete
printJSON(t, athlete)
}
// Test Activites API
func TestGetLoggedInAthleteActivities(t *testing.T) {
token := getToken(t)
apiClient, err := client.NewAPIClient(token)
require.NoError(t, err)
ctx := context.Background()
activities, err := apiClient.Activities.GetLoggedInAthleteActivities(ctx)
require.NoError(t, err)
// Indent activities
printJSON(t, activities)
}
// Test Gear API
func TestGetLoggedInAthleteGear(t *testing.T) {
token := getToken(t)
apiClient, err := client.NewAPIClient(token)
require.NoError(t, err)
ctx := context.Background()
athlete, err := apiClient.Athletes.GetLoggedInAthlete(ctx)
require.NoError(t, err)
require.NotNil(t, athlete.Bikes)
gear, err := apiClient.Gears.GetGearById(ctx, athlete.Bikes[0].ID)
require.NoError(t, err)
// Indent gear
printJSON(t, gear)
}