forked from RobWC/riotapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_client.go
185 lines (162 loc) · 3.66 KB
/
api_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package riotapi
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"strings"
"time"
)
/*
API End Points
BR BR1 br.api.pvp.net
EUNE EUN1 eune.api.pvp.net
EUW EUW1 euw.api.pvp.net
KR KR kr.api.pvp.net
LAN LA1 lan.api.pvp.net
LAS LA2 las.api.pvp.net
NA NA1 na.api.pvp.net
OCE OC1 oce.api.pvp.net
TR TR1 tr.api.pvp.net
RU RU ru.api.pvp.net
PBE PBE1 pbe.api.pvp.net
*/
// APIEndpoints mapping to api endpoints
var APIEndpoints = map[string]string{
"br": "br.api.pvp.net",
"eune": "eune.api.pvp.net",
"euw": "euw.api.pvp.net",
"kr": "kr.api.pvp.net",
"lan": "lan.api.pvp.net",
"las": "las.api.pvp.net",
"na": "na.api.pvp.net",
"oce": "oce.api.pvp.net",
"tr": "tr.api.pvp.net",
"ru": "ru.api.pvp.net",
"jp": "jp.api.pvp.net",
}
// ShardName a mapping of regions to shard names
var ShardName = map[string]string{
"br": "BR1",
"eune": "EUN1",
"euw": "EUW1",
"kr": "KR",
"lan": "LA1",
"las": "LA2",
"na": "NA1",
"oce": "OC1",
"tr": "TR1",
"ru": "RU",
"jp": "JP1",
}
// RateLimit the current rate limit of the api
type RateLimit struct {
Limits map[string]string
RetryAfter int
}
// APIClient Riot API client
type APIClient struct {
endpoint string
game string
region string
shardName string
client *http.Client
totalRequests int
key string // API key
tokens chan struct{}
RateLimit *RateLimit
}
// NewAPIClient create an initalized APIClient
func NewAPIClient(region, key string) *APIClient {
c := &APIClient{
key: key,
game: "lol",
region: strings.ToLower(region),
shardName: ShardName[strings.ToLower(region)],
client: &http.Client{
Jar: nil,
Timeout: time.Second * 5,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
}},
tokens: make(chan struct{}, 20),
RateLimit: &RateLimit{Limits: make(map[string]string)},
}
return c
}
func (c *APIClient) genURL(path []string) string {
return strings.Join(path, "/")
}
func (c *APIClient) genRequest(method, version, api string, query url.Values) (*http.Request, error) {
u := url.URL{}
u.Scheme = "https"
u.Host = strings.Join([]string{c.region, ".api.pvp.net"}, "")
u.Path = fmt.Sprintf("/api/%s/%s/%s/%s", c.game, c.region, version, api)
u.RawQuery = query.Encode()
return http.NewRequest(method, u.String(), nil)
}
// do execute a request
func (c *APIClient) do(req *http.Request, apiKey bool) ([]byte, error) {
// add api key
// manipulate request
if apiKey {
query := req.URL.Query()
query.Add("api_key", strings.ToUpper(c.key))
//rebuild request
req.URL.RawQuery = query.Encode()
}
if c.RateLimit.RetryAfter > 0 {
time.Sleep(time.Second * time.Duration(c.RateLimit.RetryAfter))
}
rc := make(chan struct {
data []byte
err error
})
go func(req http.Request) {
defer func() {
<-c.tokens
}()
c.tokens <- struct{}{}
resp, err := c.client.Do(&req)
if err != nil {
rc <- struct {
data []byte
err error
}{nil, err}
}
defer resp.Body.Close()
rl := req.Header.Get("X-Rate-Limit-Count")
if rl != "" {
c.updateRateLimits(rl)
}
ra := req.Header.Get("Retry-After")
if rl != "" {
c.updateRetry(ra)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
rc <- struct {
data []byte
err error
}{nil, err}
}
if resp.StatusCode != http.StatusOK {
rc <- struct {
data []byte
err error
}{nil, fmt.Errorf("API Error %s", string(data))}
}
rc <- struct {
data []byte
err error
}{data, err}
}(*req)
r := <-rc
return r.data, r.err
}