-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
83 lines (70 loc) · 1.72 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
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
package lol
import (
"net/http"
"time"
"github.com/dghubble/sling"
)
const (
baseURL = "api.riotgames.com/"
defaultRegion = "na1"
maxIdleConnections = 10
requestTimeout = 5
)
var (
// DefaultHTTPClient default http client to use
DefaultHTTPClient = &http.Client{
Transport: &http.Transport{
MaxIdleConnsPerHost: maxIdleConnections,
},
Timeout: time.Duration(requestTimeout) * time.Second,
}
)
// ClientOption is a func that operates on *Client
type ClientOption func(*Client) error
// Client API struct to League of Legends
type Client struct {
Token, Region string
sling *sling.Sling
httpClient *http.Client
*LOL
*TFT
}
// NewClient returns interface to League of Legends API
func NewClient(token string, options ...ClientOption) (*Client, error) {
cli := &Client{}
WithToken(token)(cli)
WithRegion(defaultRegion)(cli)
cli.sling = sling.New().Base("https://" + cli.Region + "." + baseURL)
cli.sling.Set("User-Agent", "jonwho/lol")
for _, option := range options {
if err := option(cli); err != nil {
return nil, err
}
}
cli.sling.Set("X-Riot-Token", cli.Token)
cli.LOL = NewLOL(cli.sling)
cli.TFT = NewTFT(cli.sling)
return cli, nil
}
// WithToken set the client token
func WithToken(token string) ClientOption {
return func(c *Client) error {
c.Token = token
return nil
}
}
// WithRegion set the client region
func WithRegion(region string) ClientOption {
return func(c *Client) error {
c.Region = region
return nil
}
}
// WithHTTPClient set the client http.Client and the sling http.Client
func WithHTTPClient(httpClient *http.Client) ClientOption {
return func(c *Client) error {
c.httpClient = httpClient
c.sling.Client(httpClient)
return nil
}
}