forked from nicklaw5/helix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helix.go
380 lines (308 loc) · 8.48 KB
/
helix.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package helix
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"strconv"
"strings"
"time"
)
const (
// APIBaseURL is the base URL for composing API requests.
APIBaseURL = "https://api.twitch.tv/helix"
// AuthBaseURL is the base URL for composing authentication requests.
AuthBaseURL = "https://id.twitch.tv/oauth2"
)
// HTTPClient ...
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
// Client ...
type Client struct {
clientID string
clientSecret string
appAccessToken string
userAccessToken string
userAgent string
redirectURI string
scopes []string
httpClient HTTPClient
rateLimitFunc RateLimitFunc
baseURL string
lastResponse *Response
}
// Options ...
type Options struct {
ClientID string
ClientSecret string
AppAccessToken string
UserAccessToken string
UserAgent string
RedirectURI string
Scopes []string
HTTPClient HTTPClient
RateLimitFunc RateLimitFunc
}
// RateLimitFunc ...
type RateLimitFunc func(*Response) error
// ResponseCommon ...
type ResponseCommon struct {
StatusCode int
Header http.Header
Error string `json:"error"`
ErrorStatus int `json:"status"`
ErrorMessage string `json:"message"`
}
func (rc *ResponseCommon) convertHeaderToInt(str string) int {
i, _ := strconv.Atoi(str)
return i
}
// GetRateLimit returns the "RateLimit-Limit" header as an int.
func (rc *ResponseCommon) GetRateLimit() int {
return rc.convertHeaderToInt(rc.Header.Get("RateLimit-Limit"))
}
// GetRateLimitRemaining returns the "RateLimit-Remaining" header as an int.
func (rc *ResponseCommon) GetRateLimitRemaining() int {
return rc.convertHeaderToInt(rc.Header.Get("RateLimit-Remaining"))
}
// GetRateLimitReset returns the "RateLimit-Reset" header as an int.
func (rc *ResponseCommon) GetRateLimitReset() int {
return rc.convertHeaderToInt(rc.Header.Get("RateLimit-Reset"))
}
// Response ...
type Response struct {
ResponseCommon
Data interface{}
}
// Pagination ...
type Pagination struct {
Cursor string `json:"cursor"`
}
// NewClient returns a new Twicth Helix API client. It returns an
// if clientID is an empty string. It is concurrecy safe.
func NewClient(options *Options) (*Client, error) {
if options.ClientID == "" {
return nil, errors.New("A client ID was not provided but is required")
}
c := &Client{
clientID: options.ClientID,
httpClient: http.DefaultClient,
}
// Set options
if options.HTTPClient != nil {
c.httpClient = options.HTTPClient
}
c.clientSecret = options.ClientSecret
c.appAccessToken = options.AppAccessToken
c.userAccessToken = options.UserAccessToken
c.userAgent = options.UserAgent
c.rateLimitFunc = options.RateLimitFunc
c.scopes = options.Scopes
c.redirectURI = options.RedirectURI
// Set non-options
c.baseURL = APIBaseURL
return c, nil
}
func (c *Client) get(path string, respData, reqData interface{}) (*Response, error) {
return c.sendRequest(http.MethodGet, path, respData, reqData)
}
func (c *Client) post(path string, respData, reqData interface{}) (*Response, error) {
return c.sendRequest(http.MethodPost, path, respData, reqData)
}
func (c *Client) put(path string, respData, reqData interface{}) (*Response, error) {
return c.sendRequest(http.MethodPut, path, respData, reqData)
}
func (c *Client) sendRequest(method, path string, respData, reqData interface{}) (*Response, error) {
resp := &Response{}
if respData != nil {
resp.Data = respData
}
req, err := c.newRequest(method, path, reqData)
if err != nil {
return nil, err
}
err = c.doRequest(req, resp)
if err != nil {
return nil, err
}
return resp, nil
}
func buildQueryString(req *http.Request, v interface{}) (string, error) {
isNil, err := isZero(v)
if err != nil {
return "", err
}
if isNil {
return "", nil
}
query := req.URL.Query()
vType := reflect.TypeOf(v).Elem()
vValue := reflect.ValueOf(v).Elem()
for i := 0; i < vType.NumField(); i++ {
var defaultValue string
field := vType.Field(i)
tag := field.Tag.Get("query")
// Get the default value from the struct tag
if strings.Contains(tag, ",") {
tagSlice := strings.Split(tag, ",")
tag = tagSlice[0]
defaultValue = tagSlice[1]
}
if field.Type.Kind() == reflect.Slice {
// Attach any slices as query params
fieldVal := vValue.Field(i)
for j := 0; j < fieldVal.Len(); j++ {
query.Add(tag, fmt.Sprintf("%v", fieldVal.Index(j)))
}
} else if isDatetimeTagField(tag) {
// Get and correctly format datetime fields, and attach them query params
dateStr := fmt.Sprintf("%v", vValue.Field(i))
if strings.Contains(dateStr, " m=-") {
datetimeSplit := strings.Split(dateStr, " m=-")
date, err := time.Parse(requestDateTimeFormat, datetimeSplit[0])
if err != nil {
return "", err
}
// Determine if the date has been set. If it has we'll add it to the query.
if !date.IsZero() {
query.Add(tag, date.Format(time.RFC3339))
}
}
} else {
// Add any scalar values as query params
fieldVal := fmt.Sprintf("%v", vValue.Field(i))
// If no value was set by the user, use the default
// value specified in the struct tag.
if fieldVal == "" || fieldVal == "0" {
if defaultValue == "" {
continue
}
fieldVal = defaultValue
}
query.Add(tag, fieldVal)
}
}
return query.Encode(), nil
}
func isZero(v interface{}) (bool, error) {
t := reflect.TypeOf(v)
if !t.Comparable() {
return false, fmt.Errorf("type is not comparable: %v", t)
}
return v == reflect.Zero(t).Interface(), nil
}
func (c *Client) newRequest(method, path string, data interface{}) (*http.Request, error) {
url := c.getBaseURL(path) + path
req, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, err
}
if data != nil {
query, err := buildQueryString(req, data)
if err != nil {
return nil, err
}
req.URL.RawQuery = query
}
return req, nil
}
func (c *Client) getBaseURL(path string) string {
for _, authPath := range authPaths {
if strings.Contains(path, authPath) {
return AuthBaseURL
}
}
return APIBaseURL
}
func (c *Client) doRequest(req *http.Request, resp *Response) error {
c.setRequestHeaders(req)
for {
if c.lastResponse != nil && c.rateLimitFunc != nil {
err := c.rateLimitFunc(c.lastResponse)
if err != nil {
return err
}
}
response, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("Failed to execute API request: %s", err.Error())
}
defer response.Body.Close()
resp.Header = response.Header
setResponseStatusCode(resp, "StatusCode", response.StatusCode)
bodyBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
// Only attempt to decode the response if we have a response we can handle
if len(bodyBytes) > 0 && resp.StatusCode < http.StatusInternalServerError {
if resp.Data != nil && resp.StatusCode < http.StatusBadRequest {
// Successful request
err = json.Unmarshal(bodyBytes, &resp.Data)
} else {
// Failed request
err = json.Unmarshal(bodyBytes, &resp)
}
if err != nil {
return fmt.Errorf("Failed to decode API response: %s", err.Error())
}
}
if c.rateLimitFunc == nil {
break
} else {
c.lastResponse = resp
if c.rateLimitFunc != nil &&
c.lastResponse.StatusCode == http.StatusTooManyRequests {
// Rate limit exceeded, retry to send request after
// applying rate limiter callback
continue
}
break
}
}
return nil
}
func (c *Client) setRequestHeaders(req *http.Request) {
req.Header.Set("Client-ID", c.clientID)
if c.userAgent != "" {
req.Header.Set("User-Agent", c.userAgent)
}
var bearerToken string
if c.appAccessToken != "" {
bearerToken = c.appAccessToken
}
if c.userAccessToken != "" {
bearerToken = c.userAccessToken
}
if bearerToken != "" {
req.Header.Set("Authorization", "Bearer "+bearerToken)
}
}
func setResponseStatusCode(v interface{}, fieldName string, code int) {
s := reflect.ValueOf(v).Elem()
field := s.FieldByName(fieldName)
field.SetInt(int64(code))
}
// SetAppAccessToken ...
func (c *Client) SetAppAccessToken(accessToken string) {
c.appAccessToken = accessToken
}
// SetUserAccessToken ...
func (c *Client) SetUserAccessToken(accessToken string) {
c.userAccessToken = accessToken
}
// SetUserAgent ...
func (c *Client) SetUserAgent(userAgent string) {
c.userAgent = userAgent
}
// SetScopes ...
func (c *Client) SetScopes(scopes []string) {
c.scopes = scopes
}
// SetRedirectURI ...
func (c *Client) SetRedirectURI(uri string) {
c.redirectURI = uri
}