-
Notifications
You must be signed in to change notification settings - Fork 22
/
lib.go
513 lines (417 loc) · 13.8 KB
/
lib.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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package lib
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"strings"
"sync"
"time"
"github.com/spf13/viper"
ehttp "github.com/topfreegames/extensions/http"
)
// RequestError contains code and body of a request that failed
type RequestError struct {
statusCode int
body string
}
// NewRequestError returns a request error
func NewRequestError(statusCode int, body string) *RequestError {
return &RequestError{
statusCode: statusCode,
body: body,
}
}
func (r *RequestError) Error() string {
return fmt.Sprintf("Request error. Status code: %d. Body: %s", r.statusCode, r.body)
}
// Status returns the status code of the error
func (r *RequestError) Status() int {
return r.statusCode
}
// Podium is a struct that represents a podium API application
type Podium struct {
httpClient *http.Client
Config *viper.Viper
URL string
User string
Pass string
}
var (
client *http.Client
once sync.Once
)
// Member maps an member identified by their publicID to their score and rank
type Member struct {
LeaderboardID string
PublicID string
Score int
Rank int
PreviousRank int
}
//MemberList is a list of member
type MemberList struct {
Members []*Member
Member *Member
NotFound []string
}
//Score will represent a member Score in a Leaderboard
type Score struct {
LeaderboardID string
PublicID string
Score int
Rank int
PreviousRank int
}
//ScoreList is a list of Scores
type ScoreList struct {
Scores []*Score
}
//Response will determine if a request has been succeeded
type Response struct {
Success bool
Reason string
}
func getHTTPClient(
timeout time.Duration,
maxIdleConns, maxIdleConnsPerHost int,
) *http.Client {
once.Do(func() {
client = &http.Client{
Transport: getHTTPTransport(maxIdleConns, maxIdleConnsPerHost),
Timeout: timeout,
}
ehttp.Instrument(client)
})
return client
}
func getHTTPTransport(
maxIdleConns, maxIdleConnsPerHost int,
) http.RoundTripper {
if _, ok := http.DefaultTransport.(*http.Transport); !ok {
return http.DefaultTransport // tests use a mock transport
}
// We can't get http.DefaultTransport here and update its
// fields since it's an exported variable, so other libs could
// also change it and overwrite. This hardcoded values are copied
// from http.DefaultTransport but could be configurable too.
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: maxIdleConns,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
MaxIdleConnsPerHost: maxIdleConnsPerHost,
}
}
// NewPodium returns a new podium API application
func NewPodium(config *viper.Viper) PodiumInterface {
config.SetDefault("podium.timeout", 1*time.Second)
config.SetDefault("podium.maxIdleConnsPerHost", http.DefaultMaxIdleConnsPerHost)
config.SetDefault("podium.maxIdleConns", 100)
p := &Podium{
httpClient: getHTTPClient(
config.GetDuration("podium.timeout"),
config.GetInt("podium.maxIdleConns"),
config.GetInt("podium.maxIdleConnsPerHost"),
),
Config: config,
URL: config.GetString("podium.url"),
User: config.GetString("podium.user"),
Pass: config.GetString("podium.pass"),
}
return p
}
func (p *Podium) sendTo(ctx context.Context, method, url string, payload map[string]interface{}) ([]byte, error) {
payloadJSON, err := json.Marshal(payload)
if err != nil {
return nil, err
}
var req *http.Request
if payload != nil {
req, err = http.NewRequest(method, url, bytes.NewBuffer(payloadJSON))
if err != nil {
return nil, err
}
} else {
req, err = http.NewRequest(method, url, nil)
if err != nil {
return nil, err
}
}
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(p.User, p.Pass)
if ctx == nil {
ctx = context.Background()
}
req = req.WithContext(ctx)
resp, err := p.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, respErr := ioutil.ReadAll(resp.Body)
if respErr != nil {
return nil, respErr
}
if resp.StatusCode > 399 {
return nil, NewRequestError(resp.StatusCode, string(body))
}
return body, nil
}
func (p *Podium) buildURL(pathname string) string {
return fmt.Sprintf("%s%s", p.URL, pathname)
}
func (p *Podium) buildDeleteLeaderboardURL(leaderboard string) string {
var pathname = fmt.Sprintf("/l/%s", leaderboard)
return p.buildURL(pathname)
}
func (p *Podium) buildGetTopPercentURL(leaderboard string, percentage int) string {
var pathname = fmt.Sprintf("/l/%s/top-percent/%d", leaderboard, percentage)
return p.buildURL(pathname)
}
func (p *Podium) buildUpdateScoreURL(leaderboard, memberID string, scoreTTL int) string {
var pathname = fmt.Sprintf("/l/%s/members/%s/score", leaderboard, memberID)
pathname = p.appendScoreTTLAndPrevRank(pathname, scoreTTL)
return p.buildURL(pathname)
}
func (p *Podium) buildIncrementScoreURL(leaderboard, memberID string, scoreTTL int) string {
return p.buildUpdateScoreURL(leaderboard, memberID, scoreTTL)
}
func (p *Podium) buildUpdateScoresURL(memberID string, scoreTTL int) string {
var pathname = fmt.Sprintf("/m/%s/scores", memberID)
pathname = p.appendScoreTTLAndPrevRank(pathname, scoreTTL)
return p.buildURL(pathname)
}
func (p *Podium) buildUpdateMembersScoreURL(leaderboard string, scoreTTL int) string {
var pathname = fmt.Sprintf("/l/%s/scores", leaderboard)
pathname = p.appendScoreTTLAndPrevRank(pathname, scoreTTL)
return p.buildURL(pathname)
}
func (p *Podium) buildRemoveMemberFromLeaderboardURL(leaderboard, member string) string {
var pathname = fmt.Sprintf("/l/%s/members/%s", leaderboard, member)
return p.buildURL(pathname)
}
// page is 1-based
func (p *Podium) buildGetTopURL(leaderboard string, page, pageSize int) string {
var pathname = fmt.Sprintf("/l/%s/top/%d?pageSize=%d", leaderboard, page, pageSize)
return p.buildURL(pathname)
}
func (p *Podium) buildGetMemberURL(leaderboard, memberID string) string {
pathname := fmt.Sprintf("/l/%s/members/%s", leaderboard, memberID)
return p.buildURL(pathname)
}
func (p *Podium) buildGetMemberInLeaderboardsURL(leaderboards []string, memberID string, order string) string {
pathname := fmt.Sprintf("/m/%s/scores?leaderboardIds=%s&order=%s", memberID, strings.Join(leaderboards, ","), order)
return p.buildURL(pathname)
}
func (p *Podium) buildGetCountURL(leaderboard string) string {
pathname := fmt.Sprintf("/l/%s/members-count", leaderboard)
return p.buildURL(pathname)
}
func (p *Podium) buildGetMembersAroundMemberURL(leaderboard, memberID string, pageSize int, getLastIfNotFound bool, order string) string {
pathname := fmt.Sprintf("/l/%s/members/%s/around?pageSize=%d&getLastIfNotFound=%t&order=%s", leaderboard, memberID, pageSize, getLastIfNotFound, order)
return p.buildURL(pathname)
}
func (p *Podium) buildGetMembersURL(leaderboard string, memberIDs []string) string {
memberIDsCsv := strings.Join(memberIDs, ",")
pathname := fmt.Sprintf("/l/%s/members?ids=%s", leaderboard, memberIDsCsv)
return p.buildURL(pathname)
}
func (p *Podium) buildHealthcheckURL() string {
var pathname = "/healthcheck"
return p.buildURL(pathname)
}
func (p *Podium) appendScoreTTLAndPrevRank(pathname string, scoreTTL int) string {
pathname = fmt.Sprintf("%s?prevRank=true", pathname)
if scoreTTL <= 0 {
return pathname
}
return fmt.Sprintf("%s&scoreTTL=%d", pathname, scoreTTL)
}
// GetTop returns the top members for this leaderboard. Page is 1-index
func (p *Podium) GetTop(ctx context.Context, leaderboard string, page, pageSize int) (*MemberList, error) {
route := p.buildGetTopURL(leaderboard, page, pageSize)
body, err := p.sendTo(ctx, "GET", route, nil)
if err != nil {
return nil, err
}
var members MemberList
err = json.Unmarshal(body, &members)
return &members, err
}
// GetTopPercent returns the top x% of members in a leaderboard
func (p *Podium) GetTopPercent(ctx context.Context, leaderboard string, percentage int) (*MemberList, error) {
route := p.buildGetTopPercentURL(leaderboard, percentage)
body, err := p.sendTo(ctx, "GET", route, nil)
if err != nil {
return nil, err
}
var members MemberList
err = json.Unmarshal(body, &members)
return &members, err
}
// UpdateScore updates the score of a particular member in a leaderboard
func (p *Podium) UpdateScore(ctx context.Context, leaderboard, memberID string, score, scoreTTL int) (*Member, error) {
route := p.buildUpdateScoreURL(leaderboard, memberID, scoreTTL)
payload := map[string]interface{}{
"score": score,
}
body, err := p.sendTo(ctx, "PUT", route, payload)
if err != nil {
return nil, err
}
member := new(Member)
err = json.Unmarshal(body, member)
return member, err
}
// IncrementScore increments the score of a particular member in a leaderboard
func (p *Podium) IncrementScore(ctx context.Context, leaderboard, memberID string, increment, scoreTTL int) (*MemberList, error) {
route := p.buildIncrementScoreURL(leaderboard, memberID, scoreTTL)
payload := map[string]interface{}{
"increment": increment,
}
body, err := p.sendTo(ctx, "PATCH", route, payload)
if err != nil {
return nil, err
}
var member MemberList
err = json.Unmarshal(body, &member)
return &member, err
}
// UpdateScores updates the score of a member in more than one leaderboard
func (p *Podium) UpdateScores(ctx context.Context, leaderboards []string, memberID string, score, scoreTTL int) (*ScoreList, error) {
route := p.buildUpdateScoresURL(memberID, scoreTTL)
payload := map[string]interface{}{
"score": score,
"leaderboards": leaderboards,
}
body, err := p.sendTo(ctx, "PUT", route, payload)
if err != nil {
return nil, err
}
var scores ScoreList
err = json.Unmarshal(body, &scores)
return &scores, err
}
// UpdateMembersScore updates the score of a member in more than one leaderboard
func (p *Podium) UpdateMembersScore(ctx context.Context, leaderboard string, members []*Member, scoreTTL int) (*MemberList, error) {
route := p.buildUpdateMembersScoreURL(leaderboard, scoreTTL)
membersPayload := make([]map[string]interface{}, len(members))
for i, member := range members {
membersPayload[i] = map[string]interface{}{
"score": member.Score,
"publicID": member.PublicID,
}
}
payload := map[string]interface{}{"members": membersPayload}
body, err := p.sendTo(ctx, "PUT", route, payload)
if err != nil {
return nil, err
}
var resMember MemberList
err = json.Unmarshal(body, &resMember)
return &resMember, err
}
// RemoveMemberFromLeaderboard removes a member from a leaderboard
func (p *Podium) RemoveMemberFromLeaderboard(ctx context.Context, leaderboard, member string) (*Response, error) {
route := p.buildRemoveMemberFromLeaderboardURL(leaderboard, member)
body, err := p.sendTo(ctx, "DELETE", route, nil)
if err != nil {
return nil, err
}
var response Response
err = json.Unmarshal(body, &response)
return &response, err
}
// GetMember shows score and rank of a particular member in a leaderboard
func (p *Podium) GetMember(ctx context.Context, leaderboard, memberID string) (*Member, error) {
route := p.buildGetMemberURL(leaderboard, memberID)
body, err := p.sendTo(ctx, "GET", route, nil)
if err != nil {
return nil, err
}
var member Member
err = json.Unmarshal(body, &member)
return &member, err
}
// GetMembersAroundMember returns the members around the given memberID
func (p *Podium) GetMembersAroundMember(ctx context.Context, leaderboard, memberID string, pageSize int, getLastIfNotFound bool, order ...string) (*MemberList, error) {
var o = "desc"
if len(order) > 0 {
o = order[0]
}
route := p.buildGetMembersAroundMemberURL(leaderboard, memberID, pageSize, getLastIfNotFound, o)
body, err := p.sendTo(ctx, "GET", route, nil)
if err != nil {
return nil, err
}
var members MemberList
err = json.Unmarshal(body, &members)
return &members, err
}
// GetMembers returns the members for this leaderboard. Page is 1-index
func (p *Podium) GetMembers(ctx context.Context, leaderboard string, memberIDs []string) (*MemberList, error) {
route := p.buildGetMembersURL(leaderboard, memberIDs)
body, err := p.sendTo(ctx, "GET", route, nil)
if err != nil {
return nil, err
}
var members MemberList
err = json.Unmarshal(body, &members)
return &members, err
}
// GetMemberInLeaderboards returns the ranking and score of a player in multiple leaderboards
func (p *Podium) GetMemberInLeaderboards(ctx context.Context, leaderboards []string, memberID string, order ...string) (*ScoreList, error) {
var o = "desc"
if len(order) > 0 {
o = order[0]
}
route := p.buildGetMemberInLeaderboardsURL(leaderboards, memberID, o)
body, err := p.sendTo(ctx, "GET", route, nil)
if err != nil {
return nil, err
}
var scores ScoreList
err = json.Unmarshal(body, &scores)
return &scores, err
}
// Healthcheck verifies if podium is still up
func (p *Podium) Healthcheck(ctx context.Context) (string, error) {
route := p.buildHealthcheckURL()
body, err := p.sendTo(ctx, "GET", route, nil)
return string(body), err
}
// DeleteLeaderboard deletes the leaderboard from podium
func (p *Podium) DeleteLeaderboard(ctx context.Context, leaderboard string) (*Response, error) {
route := p.buildDeleteLeaderboardURL(leaderboard)
body, err := p.sendTo(ctx, "DELETE", route, nil)
if err != nil {
return nil, err
}
var response Response
err = json.Unmarshal(body, &response)
return &response, err
}
// GetCount gets the number of members in a leaderboard
func (p *Podium) GetCount(ctx context.Context, leaderboard string) (int, error) {
route := p.buildGetCountURL(leaderboard)
body, err := p.sendTo(ctx, "GET", route, nil)
if err != nil {
return 0, err
}
type countResp struct {
Count int `json:"count"`
}
var count countResp
err = json.Unmarshal(body, &count)
return count.Count, err
}