/
rwgps.go
71 lines (61 loc) · 1.46 KB
/
rwgps.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
package rwgps
//go:generate genwith --do --client --token --config --ratelimit --package rwgps
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"golang.org/x/oauth2"
"github.com/bzimmer/gravl/pkg"
"github.com/bzimmer/gravl/pkg/providers/activity"
)
const (
apiVersion = "2"
baseURL = "https://ridewithgps.com"
)
// Client for communicating with RWGPS
type Client struct {
config oauth2.Config
token *oauth2.Token
client *http.Client
Users *UsersService
Trips *TripsService
}
func (c *Client) Uploader() activity.Uploader {
return newUploader(c.Trips)
}
func withServices() Option {
return func(c *Client) error {
c.Users = &UsersService{client: c}
c.Trips = &TripsService{client: c}
return nil
}
}
func (c *Client) newAPIRequest(ctx context.Context, uri string, params map[string]string) (*http.Request, error) {
u, err := url.Parse(fmt.Sprintf("%s/%s", baseURL, uri))
if err != nil {
return nil, err
}
x := map[string]string{
"version": apiVersion,
"apikey": c.config.ClientID,
"auth_token": c.token.AccessToken,
}
for k, v := range params {
x[k] = v
}
b, err := json.Marshal(x)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), bytes.NewReader(b))
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", pkg.UserAgent)
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
return req, nil
}