-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
1701 lines (1468 loc) · 57.7 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
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package nakama
//go:generate stringer -type Code -trimprefix Code
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"sync"
"time"
"golang.org/x/net/publicsuffix"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
// DefaultWsPath is the default websocket path.
var DefaultWsPath = "/ws"
// AuthHandler is an empty interface that provides a clean, extensible way to
// register a type as a handler that is future-proofed. As used by WithAuthHandler,
// a type that supports the any of the following smuggled interfaces:
//
// AuthHandler(context.Context, *nakama.Client) error
//
// Will have its method added to Client as its respective <MessageType>Handler.
//
// For an overview of Go interface smuggling as a concept, see:
//
// https://utcc.utoronto.ca/~cks/space/blog/programming/GoInterfaceSmuggling
type AuthHandler interface{}
// Client is a nakama client.
type Client struct {
cl *http.Client
url string
serverKey string
username string
password string
refreshAuto bool
expiryGrace time.Duration
session *SessionResponse
expiry time.Time
expiryGraced time.Time
expiryRefresh time.Time
expiryRefreshGraced time.Time
marshaler *protojson.MarshalOptions
unmarshaler *protojson.UnmarshalOptions
logf func(string, ...interface{})
AuthHandler func(context.Context, *Client) error
rw sync.RWMutex
}
// New creates a new nakama client.
func New(opts ...Option) *Client {
jar, _ := cookiejar.New(&cookiejar.Options{
PublicSuffixList: publicsuffix.List,
})
cl := &Client{
cl: &http.Client{
Jar: jar,
},
url: "http://127.0.0.1:7350",
refreshAuto: true,
expiryGrace: 5 * time.Second,
marshaler: &protojson.MarshalOptions{
UseProtoNames: true,
UseEnumNumbers: true,
},
unmarshaler: &protojson.UnmarshalOptions{
DiscardUnknown: true,
},
}
for _, o := range opts {
o(cl)
}
cl.url = strings.TrimSuffix(cl.url, "/")
return cl
}
// Logf satisfies the Handler interface.
func (cl *Client) Logf(s string, v ...interface{}) {
if cl.logf != nil {
cl.logf(s, v...)
}
}
// Errf satisfies the handler interface.
func (cl *Client) Errf(s string, v ...interface{}) {
cl.Logf("ERROR: "+s, v...)
}
// HttpClient satisfies the handler interface.
func (cl *Client) HttpClient() *http.Client {
return cl.cl
}
// SocketURL satisfies the Handler interface.
func (cl *Client) SocketURL() (string, error) {
u, err := url.Parse(cl.url)
if err != nil {
return "", err
}
scheme := "ws"
switch strings.ToLower(u.Scheme) {
case "http":
case "https":
scheme = "wss"
default:
return "", fmt.Errorf("invalid scheme %q", u.Scheme)
}
return scheme + "://" + u.Host + DefaultWsPath, nil
}
// Token returns the current session token. Satisfies the Handler interface.
func (cl *Client) Token(ctx context.Context) (string, error) {
if cl.session == nil && cl.AuthHandler != nil {
if err := cl.AuthHandler(ctx, cl); err != nil {
return "", err
}
}
if err := cl.SessionRefresh(ctx); err != nil {
return "", err
}
return cl.session.Token, nil
}
// BuildRequest builds a http request.
func (cl *Client) BuildRequest(ctx context.Context, method, typ string, query url.Values, body io.Reader) (*http.Request, error) {
// build url
urlstr := cl.url + "/" + typ
if len(query) != 0 {
urlstr += "?" + query.Encode()
}
// create request
req, err := http.NewRequestWithContext(ctx, method, urlstr, body)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
var auth *url.Userinfo
switch {
case cl.serverKey != "" && (strings.Contains(typ, "authenticate") || strings.Contains(typ, "refresh")):
auth = url.UserPassword(cl.serverKey, "")
case cl.username != "":
auth = url.UserPassword(cl.username, cl.password)
}
if auth != nil {
req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(auth.String())))
}
if body != nil {
req.Header.Add("Content-Type", "application/json")
}
return req, nil
}
// Exec executes the request http request.
func (cl *Client) Exec(req *http.Request) (*http.Response, error) {
res, err := cl.cl.Do(req)
if err != nil {
return nil, err
}
switch {
case res.StatusCode != http.StatusOK:
defer res.Body.Close()
return nil, NewClientErrorFromReader(res.StatusCode, res.Body)
}
return res, nil
}
// Do executes a http request with method, type and url query values, passing
// msg as the request body (when not nil), and decoding the response body to v
// (when not nil). Will attempt to refresh the session token if the session is
// expired and refresh is true.
//
// Uses Protobuf's google.golang.org/protobuf/encoding/protojson package to
// encode/decode msg and v when msg/v are a proto.Message. Otherwise uses Go's
// encoding/json package to encode/decode.
//
// See: Marshal and Unmarshal.
func (cl *Client) Do(ctx context.Context, method, typ string, auth bool, query url.Values, msg, v interface{}) error {
// marshal
var body io.Reader
if msg != nil {
var err error
if body, err = cl.Marshal(msg); err != nil {
return err
}
}
// build request
req, err := cl.BuildRequest(ctx, method, typ, query, body)
if err != nil {
return err
}
// refresh
if auth && cl.refreshAuto {
if err := cl.SessionRefresh(ctx); err != nil {
return err
}
}
// check active session
switch session := cl.session != nil; {
case auth && !session:
// error here ?
case auth && session:
// add auth token
req.Header.Set("Authorization", "Bearer "+cl.session.Token)
}
// exec
res, err := cl.Exec(req)
if err != nil {
return err
}
defer res.Body.Close()
if v == nil {
return nil
}
// unmarshal
return cl.Unmarshal(res.Body, v)
}
// Marshal marshals v. If v is a proto.Message, will use Protobuf's
// google.golang.org/protobuf/encoding/protojson package to encode the message,
// otherwise uses Go's encoding/json package.
func (cl *Client) Marshal(v interface{}) (io.Reader, error) {
// protojson encode
msg, ok := v.(proto.Message)
if ok {
if msg != nil {
buf, err := cl.marshaler.Marshal(msg)
if err != nil {
return nil, err
}
return bytes.NewReader(buf), nil
}
return nil, nil
}
// json encode
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(v); err != nil {
return nil, err
}
return buf, nil
}
// Unmarshal unmarshals r to v. If v is a proto.Message, will use Protobuf's
// google.golang.org/protobuf/encoding/protojson package to decode the message,
// otherwise uses Go's encoding/json package.
func (cl *Client) Unmarshal(r io.Reader, v interface{}) error {
// protojson decode
if msg, ok := v.(proto.Message); ok {
buf, err := io.ReadAll(r)
if err != nil {
return err
}
return cl.unmarshaler.Unmarshal(buf, msg)
}
// json decode
dec := json.NewDecoder(r)
dec.DisallowUnknownFields()
return dec.Decode(v)
}
/*
// MarshalBytes marshals v. If v is a proto.Message, will use Protobuf's
// google.golang.org/protobuf/encoding/protojson package to encode the message,
// otherwise uses Go's encoding/json package.
func (cl *Client) MarshalBytes(v interface{}) ([]byte, error) {
// protojson encode
if msg, ok := v.(proto.Message); ok {
if msg != nil {
return cl.marshaler.Marshal(msg)
}
return nil, nil
}
// json encode
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(v); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// UnmarshalBytes unmarshals r to v. If v is a proto.Message, will use
// Protobuf's google.golang.org/protobuf/encoding/protojson package to decode
// the message, otherwise uses Go's encoding/json package.
func (cl *Client) UnmarshalBytes(buf []byte, v interface{}) error {
// protojson decode
if msg, ok := v.(proto.Message); ok {
return cl.unmarshaler.Unmarshal(buf, msg)
}
// json decode
dec := json.NewDecoder(bytes.NewReader(buf))
dec.DisallowUnknownFields()
return dec.Decode(v)
}
*/
// SessionStart starts a session.
func (cl *Client) SessionStart(session *SessionResponse) error {
expiry, expiryGraced, err := ParseTokenExpiry(session.Token, "session", cl.expiryGrace)
if err != nil {
return fmt.Errorf("unable to start session: %w", err)
}
expiryRefresh, expiryRefreshGraced, err := ParseTokenExpiry(session.RefreshToken, "refresh", cl.expiryGrace)
if err != nil {
return fmt.Errorf("unable to start session: %w", err)
}
cl.rw.Lock()
defer cl.rw.Unlock()
cl.session, cl.expiry, cl.expiryGraced, cl.expiryRefresh, cl.expiryRefreshGraced = session, expiry, expiryGraced, expiryRefresh, expiryRefreshGraced
return nil
}
// SessionEnd ends a session without logging out. Use SessionLogout to perform a logout on the server.
func (cl *Client) SessionEnd() {
cl.rw.Lock()
defer cl.rw.Unlock()
cl.session, cl.expiry, cl.expiryGraced, cl.expiryRefresh, cl.expiryRefreshGraced = nil, time.Time{}, time.Time{}, time.Time{}, time.Time{}
}
// SessionRefresh refreshes auth token for the session.
func (cl *Client) SessionRefresh(ctx context.Context) error {
switch {
case cl.session == nil:
return fmt.Errorf("unable to refresh session: %w", NewClientError(0, CodeUnauthenticated, "no active session"))
case !cl.SessionExpired():
return nil
case cl.SessionRefreshExpired() && cl.AuthHandler != nil:
return cl.AuthHandler(ctx, cl)
case cl.SessionRefreshExpired():
return fmt.Errorf("unable to refresh session: %w", NewClientError(0, CodeUnauthenticated, "refresh token expired"))
}
res, err := SessionRefresh(cl.session.RefreshToken).Do(ctx, cl)
if err != nil {
return fmt.Errorf("unable to refresh session: %w", err)
}
if err := cl.SessionStart(res); err != nil {
return fmt.Errorf("unable to refresh session: %w", err)
}
return nil
}
// SessionLogout logs out the session.
func (cl *Client) SessionLogout(ctx context.Context) error {
cl.rw.Lock()
defer cl.rw.Unlock()
if cl.session == nil {
return nil
}
_ = SessionLogout(cl.session.Token, cl.session.RefreshToken).Do(ctx, cl)
cl.session, cl.expiry, cl.expiryGraced, cl.expiryRefresh, cl.expiryRefreshGraced = nil, time.Time{}, time.Time{}, time.Time{}, time.Time{}
return nil
}
// SessionToken returns the session token.
func (cl *Client) SessionToken() string {
cl.rw.RLock()
defer cl.rw.RUnlock()
if cl.session != nil {
return cl.session.Token
}
return ""
}
// SessionRefreshToken returns the session refresh token.
func (cl *Client) SessionRefreshToken() string {
if cl.session != nil {
return cl.session.RefreshToken
}
return ""
}
// SessionExpiry returns the session expiry time.
func (cl *Client) SessionExpiry() time.Time {
return cl.expiry
}
// SessionRefreshExpiry returns the session refresh expiry time.
func (cl *Client) SessionRefreshExpiry() time.Time {
return cl.expiryRefresh
}
// SessionExpired returns whether or not the session is expired.
func (cl *Client) SessionExpired() bool {
return cl.session == nil || cl.expiry.IsZero() || time.Now().After(cl.expiryGraced)
}
// SessionRefreshExpired returns whether or not the session refresh token is expired.
func (cl *Client) SessionRefreshExpired() bool {
return cl.session == nil || cl.expiryRefresh.IsZero() || time.Now().After(cl.expiryRefreshGraced)
}
// SessionWasCreated returns whether or not the account was newly created at the beginning of the session.
func (cl *Client) SessionWasCreated() bool {
return cl.session != nil && cl.session.Created
}
// NewConn creates a new a nakama realtime websocket connection, and runs until
// the context is closed.
func (cl *Client) NewConn(ctx context.Context, opts ...ConnOption) (*Conn, error) {
return NewConn(ctx, append([]ConnOption{WithConnClientHandler(cl)}, opts...)...)
}
// Account retrieves the user's account.
func (cl *Client) Account(ctx context.Context) (*AccountResponse, error) {
return Account().Do(ctx, cl)
}
// AccountAsync retrieves the user's account.
func (cl *Client) AccountAsync(ctx context.Context, f func(*AccountResponse, error)) {
Account().Async(ctx, cl, f)
}
// Healthcheck checks the health of the server.
func (cl *Client) Healthcheck(ctx context.Context) error {
return Healthcheck().Do(ctx, cl)
}
// Healthcheck checks the health of the server.
func (cl *Client) HealthcheckAsync(ctx context.Context, f func(error)) {
Healthcheck().Async(ctx, cl, f)
}
// AddGroupUsers adds users to a group, or accepts their join requests.
func (cl *Client) AddGroupUsers(ctx context.Context, groupId string, userIds ...string) error {
return AddGroupUsers(groupId, userIds...).Do(ctx, cl)
}
// AddGroupUsersAsync adds users to a group or accepts their join requests.
func (cl *Client) AddGroupUsersAsync(ctx context.Context, groupId string, userIds []string, f func(error)) {
AddGroupUsers(groupId, userIds...).Async(ctx, cl, f)
}
// AddFriends adds friends by id.
func (cl *Client) AddFriends(ctx context.Context, ids ...string) error {
return AddFriends(ids...).Do(ctx, cl)
}
// AddFriendsAsync adds friends by id.
func (cl *Client) AddFriendsAsync(ctx context.Context, ids []string, f func(error)) {
AddFriends(ids...).Async(ctx, cl, f)
}
// AddFriendsUsernames adds friends by username.
func (cl *Client) AddFriendsUsernames(ctx context.Context, usernames ...string) error {
return AddFriends().WithUsernames(usernames...).Do(ctx, cl)
}
// AddFriendsUsernamesAsync adds friends by username.
func (cl *Client) AddFriendsUsernamesAsync(ctx context.Context, usernames []string, f func(error)) {
AddFriends().WithUsernames(usernames...).Async(ctx, cl, f)
}
// AuthenticateApple authenticates a user with a Apple token.
func (cl *Client) AuthenticateApple(ctx context.Context, token string, create bool, username string) error {
res, err := AuthenticateApple(token).
WithCreate(create).
WithUsername(username).
Do(ctx, cl)
if err != nil {
return err
}
return cl.SessionStart(res)
}
// AuthenticateAppleAsync authenticates a user with a Apple token.
func (cl *Client) AuthenticateAppleAsync(ctx context.Context, token string, create bool, username string, f func(err error)) {
AuthenticateApple(token).
WithCreate(create).
WithUsername(username).
Async(ctx, cl, func(res *SessionResponse, err error) {
if err == nil {
err = cl.SessionStart(res)
}
f(err)
})
}
// AuthenticateCustom authenticates a user with a id.
func (cl *Client) AuthenticateCustom(ctx context.Context, id string, create bool, username string) error {
res, err := AuthenticateCustom(id).
WithCreate(create).
WithUsername(username).
Do(ctx, cl)
if err != nil {
return err
}
return cl.SessionStart(res)
}
// AuthenticateCustomAsync authenticates a user with a id.
func (cl *Client) AuthenticateCustomAsync(ctx context.Context, id string, create bool, username string, f func(err error)) {
AuthenticateCustom(id).
WithCreate(create).
WithUsername(username).
Async(ctx, cl, func(res *SessionResponse, err error) {
if err == nil {
err = cl.SessionStart(res)
}
f(err)
})
}
// AuthenticateDevice authenticates a user with a device id.
func (cl *Client) AuthenticateDevice(ctx context.Context, id string, create bool, username string) error {
res, err := AuthenticateDevice(id).
WithCreate(create).
WithUsername(username).
Do(ctx, cl)
if err != nil {
return err
}
return cl.SessionStart(res)
}
// AuthenticateDeviceAsync authenticates a user with a device id.
func (cl *Client) AuthenticateDeviceAsync(ctx context.Context, id string, create bool, username string, f func(err error)) {
AuthenticateDevice(id).
WithCreate(create).
WithUsername(username).
Async(ctx, cl, func(res *SessionResponse, err error) {
if err == nil {
err = cl.SessionStart(res)
}
f(err)
})
}
// AuthenticateEmail authenticates a user with a email/password.
func (cl *Client) AuthenticateEmail(ctx context.Context, email, password string, create bool, username string) error {
res, err := AuthenticateEmail(email, password).
WithCreate(create).
WithUsername(username).
Do(ctx, cl)
if err != nil {
return err
}
return cl.SessionStart(res)
}
// AuthenticateEmailAsync authenticates a user with a email/password.
func (cl *Client) AuthenticateEmailAsync(ctx context.Context, email, password string, create bool, username string, f func(err error)) {
AuthenticateEmail(email, password).
WithCreate(create).
WithUsername(username).
Async(ctx, cl, func(res *SessionResponse, err error) {
if err == nil {
err = cl.SessionStart(res)
}
f(err)
})
}
// AuthenticateFacebook authenticates a user with a Facebook token.
func (cl *Client) AuthenticateFacebook(ctx context.Context, token string, create bool, username string, sync bool) error {
res, err := AuthenticateFacebook(token).
WithCreate(create).
WithUsername(username).
WithSync(sync).
Do(ctx, cl)
if err != nil {
return err
}
return cl.SessionStart(res)
}
// AuthenticateFacebookAsync authenticates a user with a Facebook token.
func (cl *Client) AuthenticateFacebookAsync(ctx context.Context, token string, create bool, username string, sync bool, f func(err error)) {
AuthenticateFacebook(token).
WithCreate(create).
WithUsername(username).
WithSync(sync).
Async(ctx, cl, func(res *SessionResponse, err error) {
if err == nil {
err = cl.SessionStart(res)
}
f(err)
})
}
// AuthenticateFacebookInstantGame authenticates a user with a Facebook Instant Game token.
func (cl *Client) AuthenticateFacebookInstantGame(ctx context.Context, token string, create bool, username string) error {
res, err := AuthenticateFacebookInstantGame(token).
WithCreate(create).
WithUsername(username).
Do(ctx, cl)
if err != nil {
return err
}
return cl.SessionStart(res)
}
// AuthenticateFacebookInstantGameAsync authenticates a user with a Facebook Instant Game token.
func (cl *Client) AuthenticateFacebookInstantGameAsync(ctx context.Context, signedPlayerInfo string, create bool, username string, f func(err error)) {
AuthenticateFacebookInstantGame(signedPlayerInfo).
WithCreate(create).
WithUsername(username).
Async(ctx, cl, func(res *SessionResponse, err error) {
if err == nil {
err = cl.SessionStart(res)
}
f(err)
})
}
// AuthenticateGoogle authenticates a user with a Google token.
func (cl *Client) AuthenticateGoogle(ctx context.Context, token string, create bool, username string) error {
res, err := AuthenticateGoogle(token).
WithCreate(create).
WithUsername(username).
Do(ctx, cl)
if err != nil {
return err
}
return cl.SessionStart(res)
}
// AuthenticateGoogleAsync authenticates a user with a Google token.
func (cl *Client) AuthenticateGoogleAsync(ctx context.Context, token string, create bool, username string, f func(err error)) {
AuthenticateGoogle(token).
WithCreate(create).
WithUsername(username).
Async(ctx, cl, func(res *SessionResponse, err error) {
if err == nil {
err = cl.SessionStart(res)
}
f(err)
})
}
// AuthenticateGameCenter authenticates a user with a Apple GameCenter token.
func (cl *Client) AuthenticateGameCenter(ctx context.Context, req *AuthenticateGameCenterRequest) error {
res, err := req.Do(ctx, cl)
if err != nil {
return err
}
return cl.SessionStart(res)
}
// AuthenticateGameCenterAsync authenticates a user with a Apple GameCenter token.
func (cl *Client) AuthenticateGameCenterAsync(ctx context.Context, req *AuthenticateGameCenterRequest, f func(err error)) {
req.Async(ctx, cl, func(res *SessionResponse, err error) {
if err == nil {
err = cl.SessionStart(res)
}
f(err)
})
}
// AuthenticateSteam authenticates a user with a Steam token.
func (cl *Client) AuthenticateSteam(ctx context.Context, token string, create bool, username string, sync bool) error {
res, err := AuthenticateSteam(token).
WithCreate(create).
WithUsername(username).
WithSync(sync).
Do(ctx, cl)
if err != nil {
return err
}
return cl.SessionStart(res)
}
// AuthenticateSteamAsync authenticates a user with a Steam token.
func (cl *Client) AuthenticateSteamAsync(ctx context.Context, token string, create bool, username string, sync bool, f func(err error)) {
AuthenticateSteam(token).
WithCreate(create).
WithUsername(username).
WithSync(sync).
Async(ctx, cl, func(res *SessionResponse, err error) {
if err == nil {
err = cl.SessionStart(res)
}
f(err)
})
}
// BanGroupUsers bans users from a group.
func (cl *Client) BanGroupUsers(ctx context.Context, groupId string, userIds ...string) error {
return BanGroupUsers(groupId, userIds...).Do(ctx, cl)
}
// BanGroupUsersAsync bans users from a group.
func (cl *Client) BanGroupUsersAsync(ctx context.Context, groupId string, userIds []string, f func(error)) {
BanGroupUsers(groupId, userIds...).Async(ctx, cl, f)
}
// BlockFriends blocks friends by id.
func (cl *Client) BlockFriends(ctx context.Context, ids ...string) error {
return BlockFriends(ids...).Do(ctx, cl)
}
// BlockFriendsAsync blocks friends by id.
func (cl *Client) BlockFriendsAsync(ctx context.Context, ids []string, f func(error)) {
BlockFriends(ids...).Async(ctx, cl, f)
}
// BlockFriendsUsernames blocks friends by username.
func (cl *Client) BlockFriendsUsernames(ctx context.Context, usernames ...string) error {
return BlockFriends().WithUsernames(usernames...).Do(ctx, cl)
}
// BlockFriendsUsernamesAsync blocks friends by username.
func (cl *Client) BlockFriendsUsernamesAsync(ctx context.Context, usernames []string, f func(error)) {
BlockFriends().WithUsernames(usernames...).Async(ctx, cl, f)
}
// CreateGroup creates a new group.
func (cl *Client) CreateGroup(ctx context.Context, req *CreateGroupRequest) (*CreateGroupResponse, error) {
return req.Do(ctx, cl)
}
// CreateGroupAsync creates a new group.
func (cl *Client) CreateGroupAsync(ctx context.Context, req *CreateGroupRequest, f func(*CreateGroupResponse, error)) {
req.Async(ctx, cl, f)
}
// DeleteFriends deletes friends by id.
func (cl *Client) DeleteFriends(ctx context.Context, ids ...string) error {
return DeleteFriends(ids...).Do(ctx, cl)
}
// DeleteFriendsAsync deletes friends by id.
func (cl *Client) DeleteFriendsAsync(ctx context.Context, ids []string, f func(error)) {
DeleteFriends(ids...).Async(ctx, cl, f)
}
// DeleteFriendsUsernames deletes friends by username.
func (cl *Client) DeleteFriendsUsernames(ctx context.Context, usernames ...string) error {
return DeleteFriends().WithUsernames(usernames...).Do(ctx, cl)
}
// DeleteFriendsUsernamesAsync deletes friends by username.
func (cl *Client) DeleteFriendsUsernamesAsync(ctx context.Context, usernames []string, f func(error)) {
DeleteFriends().WithUsernames(usernames...).Async(ctx, cl, f)
}
// DeleteGroup deletes a group.
func (cl *Client) DeleteGroup(ctx context.Context, groupId string) error {
return DeleteGroup(groupId).Do(ctx, cl)
}
// DeleteGroupAsync deletes a group.
func (cl *Client) DeleteGroupAsync(ctx context.Context, groupId string, f func(error)) {
DeleteGroup(groupId).Async(ctx, cl, f)
}
// DeleteLeaderboardRecord deletes a leaderboardRecord.
func (cl *Client) DeleteLeaderboardRecord(ctx context.Context, leaderboardId string) error {
return DeleteLeaderboardRecord(leaderboardId).Do(ctx, cl)
}
// DeleteLeaderboardRecordAsync deletes a leaderboardRecord.
func (cl *Client) DeleteLeaderboardRecordAsync(ctx context.Context, leaderboardRecordId string, f func(error)) {
DeleteLeaderboardRecord(leaderboardRecordId).Async(ctx, cl, f)
}
// DeleteNotifications deletes notifications.
func (cl *Client) DeleteNotifications(ctx context.Context, ids ...string) error {
return DeleteNotifications(ids...).Do(ctx, cl)
}
// DeleteNotificationsAsync deletes notifications.
func (cl *Client) DeleteNotificationsAsync(ctx context.Context, ids []string, f func(error)) {
DeleteNotifications(ids...).Async(ctx, cl, f)
}
// DeleteStorageObjects deletes storage objects.
func (cl *Client) DeleteStorageObjects(ctx context.Context, req *DeleteStorageObjectsRequest) error {
return req.Do(ctx, cl)
}
// DeleteStorageObjectsAsync deletes storage objects.
func (cl *Client) DeleteStorageObjectsAsync(ctx context.Context, req *DeleteStorageObjectsRequest, f func(error)) {
req.Async(ctx, cl, f)
}
// DeleteTournamentRecord deletes a tournament record.
func (cl *Client) DeleteTournamentRecord(ctx context.Context, tournamentId string) error {
return DeleteTournamentRecord(tournamentId).Do(ctx, cl)
}
// DeleteTournamentRecordAsync deletes a tournament record.
func (cl *Client) DeleteTournamentRecordAsync(ctx context.Context, tournamentId string, f func(error)) {
DeleteTournamentRecord(tournamentId).Async(ctx, cl, f)
}
// DemoteGroupUsers demotes users from a group.
func (cl *Client) DemoteGroupUsers(ctx context.Context, groupId string, userIds ...string) error {
return DemoteGroupUsers(groupId, userIds...).Do(ctx, cl)
}
// DemoteGroupUsersAsync demotes users from a group.
func (cl *Client) DemoteGroupUsersAsync(ctx context.Context, groupId string, userIds []string, f func(error)) {
DemoteGroupUsers(groupId, userIds...).Async(ctx, cl, f)
}
// Event sends an event.
func (cl *Client) Event(ctx context.Context, req *EventRequest) error {
return req.Do(ctx, cl)
}
// EventAsync sends an event.
func (cl *Client) EventAsync(ctx context.Context, req *EventRequest, f func(error)) {
req.Async(ctx, cl, f)
}
// ImportFacebookFriends imports Facebook friends.
func (cl *Client) ImportFacebookFriends(ctx context.Context, token string, reset bool) error {
return ImportFacebookFriends(token).WithReset(reset).Do(ctx, cl)
}
// ImportFacebookFriendsAsync imports Facebook friends.
func (cl *Client) ImportFacebookFriendsAsync(ctx context.Context, token string, reset bool, f func(error)) {
ImportFacebookFriends(token).WithReset(reset).Async(ctx, cl, f)
}
// ImportSteamFriends imports Steam friends.
func (cl *Client) ImportSteamFriends(ctx context.Context, token string, reset bool) error {
return ImportSteamFriends(token).WithReset(reset).Do(ctx, cl)
}
// ImportSteamFriendsAsync imports Steam friends.
func (cl *Client) ImportSteamFriendsAsync(ctx context.Context, token string, reset bool, f func(error)) {
ImportSteamFriends(token).WithReset(reset).Async(ctx, cl, f)
}
// Users retrieves users by id.
func (cl *Client) Users(ctx context.Context, ids ...string) (*UsersResponse, error) {
return Users(ids...).Do(ctx, cl)
}
// UsersAsync retrieves users by id.
func (cl *Client) UsersAsync(ctx context.Context, ids []string, f func(*UsersResponse, error)) {
Users(ids...).Async(ctx, cl, f)
}
// UsersUsernames retrieves users by username.
func (cl *Client) UsersUsernames(ctx context.Context, usernames ...string) (*UsersResponse, error) {
return Users().WithUsernames(usernames...).Do(ctx, cl)
}
// UsersUsernamesAsync retrieves users by username.
func (cl *Client) UsersUsernamesAsync(ctx context.Context, usernames []string, f func(*UsersResponse, error)) {
Users().WithUsernames(usernames...).Async(ctx, cl, f)
}
// JoinGroup joins a group.
func (cl *Client) JoinGroup(ctx context.Context, groupId string) error {
return JoinGroup(groupId).Do(ctx, cl)
}
// JoinGroupAsync joins a group.
func (cl *Client) JoinGroupAsync(ctx context.Context, groupId string, f func(error)) {
JoinGroup(groupId).Async(ctx, cl, f)
}
// JoinTournament joins a tournament.
func (cl *Client) JoinTournament(ctx context.Context, tournamentId string) error {
return JoinTournament(tournamentId).Do(ctx, cl)
}
// JoinTournamentAsync joins a tournament.
func (cl *Client) JoinTournamentAsync(ctx context.Context, tournamentId string, f func(error)) {
JoinTournament(tournamentId).Async(ctx, cl, f)
}
// KickGroupUsers kicks users from a group or decline their join request.
func (cl *Client) KickGroupUsers(ctx context.Context, groupId string, userIds ...string) error {
return KickGroupUsers(groupId, userIds...).Do(ctx, cl)
}
// KickGroupUsersAsync kicks users froum a group or decline their join request.
func (cl *Client) KickGroupUsersAsync(ctx context.Context, groupId string, userIds []string, f func(error)) {
KickGroupUsers(groupId, userIds...).Async(ctx, cl, f)
}
// LeaveGroup leaves a group.
func (cl *Client) LeaveGroup(ctx context.Context, groupId string) error {
return LeaveGroup(groupId).Do(ctx, cl)
}
// LeaveGroupAsync leaves a group.
func (cl *Client) LeaveGroupAsync(ctx context.Context, groupId string, f func(error)) {
LeaveGroup(groupId).Async(ctx, cl, f)
}
// ChannelMessages retrieves a channel's messages.
func (cl *Client) ChannelMessages(ctx context.Context, req *ChannelMessagesRequest) (*ChannelMessagesResponse, error) {
return req.Do(ctx, cl)
}
// ChannelMessagesAsync retrieves a channel's messages.
func (cl *Client) ChannelMessagesAsync(ctx context.Context, req *ChannelMessagesRequest, f func(*ChannelMessagesResponse, error)) {
req.Async(ctx, cl, f)
}
// GroupUsers retrieves a group's users.
func (cl *Client) GroupUsers(ctx context.Context, req *GroupUsersRequest) (*GroupUsersResponse, error) {
return req.Do(ctx, cl)
}
// GroupUsersAsync retrieves a group's users.
func (cl *Client) GroupUsersAsync(ctx context.Context, req *GroupUsersRequest, f func(*GroupUsersResponse, error)) {
req.Async(ctx, cl, f)
}
// UserGroups retrieves a user's groups.
func (cl *Client) UserGroups(ctx context.Context, userId string) (*UserGroupsResponse, error) {
return UserGroups(userId).Do(ctx, cl)
}
// UserGroupsAsync retrieves a user's groups.
func (cl *Client) UserGroupsAsync(ctx context.Context, userId string, f func(*UserGroupsResponse, error)) {
UserGroups(userId).Async(ctx, cl, f)
}
// Groups retrieves groups.
func (cl *Client) Groups(ctx context.Context, req *GroupsRequest) (*GroupsResponse, error) {
return req.Do(ctx, cl)
}
// GroupsAsync retrieves groups.
func (cl *Client) GroupsAsync(ctx context.Context, req *GroupsRequest, f func(*GroupsResponse, error)) {
req.Async(ctx, cl, f)
}
// LinkApple adds a Apple token to the user's account.
func (cl *Client) LinkApple(ctx context.Context, token string) error {
return LinkApple(token).Do(ctx, cl)
}
// LinkApple adds a Apple token to the user's account.
func (cl *Client) LinkAppleAsync(ctx context.Context, token string, f func(error)) {
LinkApple(token).Async(ctx, cl, f)
}
// LinkCustom adds a custom id to the user's account.
func (cl *Client) LinkCustom(ctx context.Context, id string) error {
return LinkCustom(id).Do(ctx, cl)
}
// LinkCustom adds a custom id to the user's account.
func (cl *Client) LinkCustomAsync(ctx context.Context, id string, f func(error)) {
LinkCustom(id).Async(ctx, cl, f)
}
// LinkDevice adds a device id to the user's account.
func (cl *Client) LinkDevice(ctx context.Context, id string) error {
return LinkDevice(id).Do(ctx, cl)
}
// LinkDevice adds a device id to the user's account.
func (cl *Client) LinkDeviceAsync(ctx context.Context, id string, f func(error)) {
LinkDevice(id).Async(ctx, cl, f)
}
// LinkEmail adds a email/password to the user's account.
func (cl *Client) LinkEmail(ctx context.Context, email, password string) error {
return LinkEmail(email, password).Do(ctx, cl)
}
// LinkEmail adds a email/password to the user's account.
func (cl *Client) LinkEmailAsync(ctx context.Context, email, password string, f func(error)) {
LinkEmail(email, password).Async(ctx, cl, f)
}
// LinkFacebook adds a Facebook token to the user's account.
func (cl *Client) LinkFacebook(ctx context.Context, token string, sync bool) error {
return LinkFacebook(token).WithSync(sync).Do(ctx, cl)
}
// LinkFacebook adds a Facebook token to the user's account.
func (cl *Client) LinkFacebookAsync(ctx context.Context, token string, sync bool, f func(error)) {
LinkFacebook(token).WithSync(sync).Async(ctx, cl, f)
}
// LinkFacebookInstantGame adds a Facebook Instant Game signedPlayerInfo to the
// user's account.
func (cl *Client) LinkFacebookInstantGame(ctx context.Context, signedPlayerInfo string) error {
return LinkFacebookInstantGame(signedPlayerInfo).Do(ctx, cl)
}
// LinkFacebookInstantGame adds a Facebook Instant Game signedPlayerInfo to the
// user's account.
func (cl *Client) LinkFacebookInstantGameAsync(ctx context.Context, signedPlayerInfo string, f func(error)) {
LinkFacebookInstantGame(signedPlayerInfo).Async(ctx, cl, f)
}