-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_test.go
53 lines (49 loc) · 1.2 KB
/
example_test.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
package nakama_test
import (
"context"
"fmt"
"log"
"github.com/ascii8/nakama-go"
)
func Example() {
const id = "6d0c9e83-8385-48a8-8601-060b8f6a3bf6"
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create client
cl := nakama.New(nakama.WithServerKey("nakama-go_server"))
// authenticate
if err := cl.AuthenticateDevice(ctx, id, true, ""); err != nil {
log.Fatal(err)
}
// retrieve account
account, err := cl.Account(ctx)
if err != nil {
log.Fatal(err)
}
// list devices on the account
for _, d := range account.Devices {
fmt.Println("id:", d.Id)
}
// Output:
// id: 6d0c9e83-8385-48a8-8601-060b8f6a3bf6
}
func ExampleRpc() {
const amount = 1000
type rewards struct {
Rewards int64 `json:"rewards,omitempty"`
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create client
cl := nakama.New(nakama.WithServerKey("nakama-go_server"))
// create request and response
res := new(rewards)
req := nakama.Rpc("dailyRewards", rewards{Rewards: amount}, res)
// execute rpc with http key
if err := req.WithHttpKey("nakama-go").Do(ctx, cl); err != nil {
log.Fatal(err)
}
fmt.Println("rewards:", res.Rewards)
// Output:
// rewards: 2000
}