-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
55 lines (47 loc) · 1.44 KB
/
client.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
// Consume the PandaScore API in Go.
//
// TODO look at errors and how they're returned; we should probably be using Wrap() and stuff but I don't fully understand how that works
package pandascore
import (
"net/http"
"net/url"
"os"
)
const (
// PandaScore API base URL
BaseURL string = "api.pandascore.co"
// Name of the environment variable that contains the PandaScore access token
AccessTokenEnvironmentVariable string = "PANDASCORE_ACCESS_TOKEN"
)
// PandaScore client which is the primary entity.
type Client struct {
baseURL *url.URL
httpClient *http.Client
accessToken string
filter string
}
// Construct a new PandaScore client with the default URL.
//
// By default, the PandaScore access token will be read from the environment variable defined in the
// AccessTokenEnvironmentVariable constant. You can set it afterwards, for example if there is not environment variable.
func New() *Client {
c := &Client{
httpClient: http.DefaultClient,
baseURL: &url.URL{Scheme: "https", Host: BaseURL},
accessToken: os.Getenv(AccessTokenEnvironmentVariable),
}
return c
}
// Sets this client's PandaScore access token to the given value.
func (c *Client) AccessToken(accessToken string) *Client {
c.accessToken = accessToken
return c
}
// Construct a new request for the given game with the given path.
func (c *Client) Request(game Game, path string) *Request {
return &Request{
client: c,
game: game,
path: path,
}
}