-
Notifications
You must be signed in to change notification settings - Fork 8
/
abios_sdk.go
493 lines (426 loc) · 14.5 KB
/
abios_sdk.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package abios
import (
"bytes"
"encoding/json"
"net/http"
"strconv"
"strings"
"time"
"github.com/AbiosGaming/go-sdk-v2/v3/structs"
)
// Constant variables that represents endpoints
const (
baseUrl = "https://api.abiosgaming.com/v2/"
access_token = "oauth/access_token"
games = "games"
series = "series"
seriesById = series + "/"
matches = "matches/"
tournaments = "tournaments"
tournamentsById = tournaments + "/"
substages = "substages/"
teams = "teams"
teamsById = teams + "/"
organisations = "organisations"
organisationsById = organisations + "/"
players = "players"
playersById = players + "/"
rosters = "rosters/"
search = "search"
incidents = "incidents"
incidentsBySeries = incidents + "/"
)
// AbiosSdk defines the interface of an implementation of a SDK targeting the Abios endpoints.
type AbiosSdk interface {
SetRate(second, minute uint)
Games(params Parameters) (structs.PaginatedGames, error)
Series(params Parameters) (structs.PaginatedSeries, error)
SeriesById(id int64, params Parameters) (structs.Series, error)
MatchesById(id int64, params Parameters) (structs.Match, error)
Tournaments(params Parameters) (structs.PaginatedTournaments, error)
TournamentsById(id int64, params Parameters) (structs.Tournament, error)
SubstagesById(id int64, params Parameters) (structs.Substage, error)
Teams(params Parameters) (structs.PaginatedTeams, error)
TeamsById(id int64, params Parameters) (structs.Team, error)
Organisations(params Parameters) (structs.PaginatedOrganisations, error)
OrganisationsById(id int64) (structs.Organisation, error)
Players(params Parameters) (structs.PaginatedPlayers, error)
PlayersById(id int64, params Parameters) (structs.Player, error)
RostersById(id int64, params Parameters) (structs.Roster, error)
Search(query string, params Parameters) ([]structs.SearchResult, error)
Incidents(params Parameters) (structs.PaginatedIncidents, error)
IncidentsBySeriesId(id int64) (structs.SeriesIncidents, error)
}
// client holds the oauth string returned from Authenticate as well as this sessions
// requestHandler.
type client struct {
username string
password string
oauth structs.AccessToken
handler *requestHandler
base_url string
}
// authenticator makes sure the oauth token doesn't expire.
func (a *client) authenticator() {
for {
// Wait until token is about to expire
expires := time.Duration(a.oauth.ExpiresIn) * time.Second
time.Sleep(expires - time.Minute*9) // Sleep until at most 9 minutes left.
err := a.authenticate() // try once
if err == nil {
continue // It succeded.
}
// If we get an error we retry every 30 seconds for 7 minutes before we override
// the responses.
retry := time.NewTicker(30 * time.Second)
fail := time.NewTimer(7 * time.Minute)
select {
case <-retry.C:
err = a.authenticate()
if err == nil {
continue
}
case <-fail.C:
a.handler.override = responseOverride{override: true, data: *err}
return
}
}
}
// New returns a new endpoint-wrapper for api version 2 with given credentials using the default url.
func New(username, password string) *client {
return NewWithUrl(username, password, baseUrl)
}
// NewWithUrl returns a new endpoint-wrapper for api version 2 with given credentials and base_url.
// Will append "/" to base_url if missing.
func NewWithUrl(username, password, base_url string) *client {
if !strings.HasSuffix(base_url, "/") {
base_url += "/"
}
r := newRequestHandler()
c := &client{username, password, structs.AccessToken{}, r, base_url}
err := c.authenticate()
if err != nil {
c.handler.override = responseOverride{override: true, data: *err}
}
go c.authenticator() // Launch authenticator
return c
}
// SetRate sets the outgoing rate to "second" requests per second and "minute" requests
// per minte. A value less than or equal to 0 means previous
// value is kept. Default values are (5, 300)
func (a *client) SetRate(second, minute uint) {
a.handler.setRate(second, minute)
}
// authenticate queries the /oauth/access_token endpoint with the given credentials and
// stores the returned oauth token. Return nil if the request was successful.
func (a *client) authenticate() *result {
var payload = []byte(`grant_type=client_credentials&client_id=` + a.username + `&client_secret=` + a.password)
req, err := http.NewRequest("POST", a.base_url+access_token, bytes.NewBuffer(payload))
if err != nil {
return &result{body: nil, err: err}
}
req.Header = http.Header{"Content-Type": {"application/x-www-form-urlencoded"}}
statusCode, b, err := apiCall(req)
if err != nil {
return &result{body: nil, err: err}
}
dec := json.NewDecoder(bytes.NewBuffer(b))
if 200 <= statusCode && statusCode < 300 {
target := structs.AccessToken{}
err := dec.Decode(&target)
if err != nil {
return &result{body: nil, err: err}
}
a.oauth = target
return nil
}
return &result{body: b, err: nil}
}
// Games queries the /games endpoint and returns a structs.PaginatedGames.
func (a *client) Games(params Parameters) (structs.PaginatedGames, error) {
if params == nil {
params = make(Parameters)
} else {
params = copyParams(params)
}
params.Set("access_token", a.oauth.AccessToken)
result := <-a.handler.addRequest(a.base_url+games, params)
if result.err != nil {
return structs.PaginatedGames{}, result.err
}
target := structs.PaginatedGames{}
err := json.Unmarshal(result.body, &target)
if err != nil {
return structs.PaginatedGames{}, err
}
return target, nil
}
// Series queries the /series endpoint and returns a structs.PaginatedSeries.
func (a *client) Series(params Parameters) (structs.PaginatedSeries, error) {
if params == nil {
params = make(Parameters)
} else {
params = copyParams(params)
}
params.Set("access_token", a.oauth.AccessToken)
result := <-a.handler.addRequest(a.base_url+series, params)
if result.err != nil {
return structs.PaginatedSeries{}, result.err
}
target := structs.PaginatedSeries{}
err := json.Unmarshal(result.body, &target)
return target, err
}
// SeriesById queries the /series/:id endpoint and returns a structs.Series.
func (a *client) SeriesById(id int64, params Parameters) (structs.Series, error) {
sId := strconv.FormatInt(id, 10)
if params == nil {
params = make(Parameters)
} else {
params = copyParams(params)
}
params.Set("access_token", a.oauth.AccessToken)
result := <-a.handler.addRequest(a.base_url+seriesById+sId, params)
if result.err != nil {
return structs.Series{}, result.err
}
target := structs.Series{}
err := json.Unmarshal(result.body, &target)
return target, err
}
// MatchesById queries the /matches/:id endpoint and returns a structs.Match.
func (a *client) MatchesById(id int64, params Parameters) (structs.Match, error) {
sId := strconv.FormatInt(id, 10)
if params == nil {
params = make(Parameters)
} else {
params = copyParams(params)
}
params.Set("access_token", a.oauth.AccessToken)
result := <-a.handler.addRequest(a.base_url+matches+sId, params)
if result.err != nil {
return structs.Match{}, result.err
}
target := structs.Match{}
err := json.Unmarshal(result.body, &target)
return target, err
}
// Tournaments queries the /tournaments endpoint and returns a list of structs.PaginatedTournaments.
func (a *client) Tournaments(params Parameters) (structs.PaginatedTournaments, error) {
if params == nil {
params = make(Parameters)
} else {
params = copyParams(params)
}
params.Set("access_token", a.oauth.AccessToken)
result := <-a.handler.addRequest(a.base_url+tournaments, params)
if result.err != nil {
return structs.PaginatedTournaments{}, result.err
}
target := structs.PaginatedTournaments{}
err := json.Unmarshal(result.body, &target)
return target, err
}
// TournamentsById queries the /tournaments/:id endpoint and return a structs.Tournament.
func (a *client) TournamentsById(id int64, params Parameters) (structs.Tournament, error) {
sId := strconv.FormatInt(id, 10)
if params == nil {
params = make(Parameters)
} else {
params = copyParams(params)
}
params.Set("access_token", a.oauth.AccessToken)
result := <-a.handler.addRequest(a.base_url+tournamentsById+sId, params)
if result.err != nil {
return structs.Tournament{}, result.err
}
target := structs.Tournament{}
err := json.Unmarshal(result.body, &target)
return target, err
}
// SubstagesById queries the /substages/:id endpoint and returns a structs.Substage.
func (a *client) SubstagesById(id int64, params Parameters) (structs.Substage, error) {
sId := strconv.FormatInt(id, 10)
if params == nil {
params = make(Parameters)
} else {
params = copyParams(params)
}
params.Set("access_token", a.oauth.AccessToken)
result := <-a.handler.addRequest(a.base_url+substages+sId, params)
if result.err != nil {
return structs.Substage{}, result.err
}
target := structs.Substage{}
err := json.Unmarshal(result.body, &target)
return target, err
}
// Teams queries the /teams endpoint and returns a structs.PaginatedTeams.
func (a *client) Teams(params Parameters) (structs.PaginatedTeams, error) {
if params == nil {
params = make(Parameters)
} else {
params = copyParams(params)
}
params.Set("access_token", a.oauth.AccessToken)
result := <-a.handler.addRequest(a.base_url+teams, params)
if result.err != nil {
return structs.PaginatedTeams{}, result.err
}
target := structs.PaginatedTeams{}
err := json.Unmarshal(result.body, &target)
return target, err
}
// TeamsById queries the /teams/:id endpoint and return a structs.Team.
func (a *client) TeamsById(id int64, params Parameters) (structs.Team, error) {
sId := strconv.FormatInt(id, 10)
if params == nil {
params = make(Parameters)
} else {
params = copyParams(params)
}
params.Set("access_token", a.oauth.AccessToken)
result := <-a.handler.addRequest(a.base_url+teamsById+sId, params)
if result.err != nil {
return structs.Team{}, result.err
}
target := structs.Team{}
err := json.Unmarshal(result.body, &target)
return target, err
}
// Organisations queries the /organisations endpoint
func (a *client) Organisations(params Parameters) (structs.PaginatedOrganisations, error) {
if params == nil {
params = make(Parameters)
} else {
params = copyParams(params)
}
params.Set("access_token", a.oauth.AccessToken)
result := <-a.handler.addRequest(a.base_url+organisations, params)
if result.err != nil {
return structs.PaginatedOrganisations{}, result.err
}
target := structs.PaginatedOrganisations{}
err := json.Unmarshal(result.body, &target)
return target, err
}
// OrganisationsById queries the /organisations/:id endpoint
func (a *client) OrganisationsById(id int64) (structs.Organisation, error) {
sId := strconv.FormatInt(id, 10)
params := make(Parameters)
params.Set("access_token", a.oauth.AccessToken)
result := <-a.handler.addRequest(a.base_url+organisationsById+sId, params)
if result.err != nil {
return structs.Organisation{}, result.err
}
target := structs.Organisation{}
err := json.Unmarshal(result.body, &target)
return target, err
}
// Players queries the /players endpoint and returns structs.PaginatedPlayers.
func (a *client) Players(params Parameters) (structs.PaginatedPlayers, error) {
if params == nil {
params = make(Parameters)
} else {
params = copyParams(params)
}
params.Set("access_token", a.oauth.AccessToken)
result := <-a.handler.addRequest(a.base_url+players, params)
if result.err != nil {
return structs.PaginatedPlayers{}, result.err
}
target := structs.PaginatedPlayers{}
err := json.Unmarshal(result.body, &target)
return target, err
}
// PlayersById queries the /players/:id endpoint and returns a structs.Player.
func (a *client) PlayersById(id int64, params Parameters) (structs.Player, error) {
sId := strconv.FormatInt(id, 10)
if params == nil {
params = make(Parameters)
} else {
params = copyParams(params)
}
params.Set("access_token", a.oauth.AccessToken)
result := <-a.handler.addRequest(a.base_url+playersById+sId, params)
if result.err != nil {
return structs.Player{}, result.err
}
target := structs.Player{}
err := json.Unmarshal(result.body, &target)
return target, err
}
// RostersById queries the /rosters/:id endpoint and returns a structs.Roster.
func (a *client) RostersById(id int64, params Parameters) (structs.Roster, error) {
sId := strconv.FormatInt(id, 10)
if params == nil {
params = make(Parameters)
} else {
params = copyParams(params)
}
params.Set("access_token", a.oauth.AccessToken)
result := <-a.handler.addRequest(a.base_url+rosters+sId, params)
if result.err != nil {
return structs.Roster{}, result.err
}
target := structs.Roster{}
err := json.Unmarshal(result.body, &target)
return target, err
}
// Search queries the /search endpoint with the given query and returns a list of
// structs.SearchResult.
func (a *client) Search(query string, params Parameters) ([]structs.SearchResult, error) {
if params == nil {
params = make(Parameters)
} else {
params = copyParams(params)
}
params.Set("access_token", a.oauth.AccessToken)
params.Add("q", query)
result := <-a.handler.addRequest(a.base_url+search, params)
if result.err != nil {
return nil, result.err
}
target := []structs.SearchResult{}
err := json.Unmarshal(result.body, &target)
return target, err
}
// Incidents queries the /incidents endpoint and returns an structs.PaginatedIncidents.
func (a *client) Incidents(params Parameters) (structs.PaginatedIncidents, error) {
if params == nil {
params = make(Parameters)
} else {
params = copyParams(params)
}
params.Set("access_token", a.oauth.AccessToken)
result := <-a.handler.addRequest(a.base_url+incidents, params)
if result.err != nil {
return structs.PaginatedIncidents{}, result.err
}
target := structs.PaginatedIncidents{}
err := json.Unmarshal(result.body, &target)
return target, err
}
// IncidentBySeriesId queries the /incidents/:series_id endpoint and returns a
// structs.SeriesIncidents.
func (a *client) IncidentsBySeriesId(id int64) (structs.SeriesIncidents, error) {
sId := strconv.FormatInt(id, 10)
params := make(Parameters)
params.Set("access_token", a.oauth.AccessToken)
result := <-a.handler.addRequest(a.base_url+incidentsBySeries+sId, params)
if result.err != nil {
return structs.SeriesIncidents{}, result.err
}
target := structs.SeriesIncidents{}
err := json.Unmarshal(result.body, &target)
return target, err
}
// copyParams copies the parameters to a new map so different routines don't share a
// non-thread-safe map.
func copyParams(from Parameters) (to Parameters) {
to = make(Parameters)
for k, v := range from {
to[k] = v
}
return
}