Debian Codegraph @

example_test.go

raw
1 package proton_test
2
3 import (
4 "context"
5 "fmt"
6 "time"
7
8 "github.com/henrybear327/go-proton-api"
9 )
10
11 func ExampleManager_NewClient() {
12 // Create a new manager.
13 m := proton.New()
14
15 // If auth information is already known, it can be used to create a client straight away.
16 c := m.NewClient("...uid...", "...acc...", "...ref...")
17 defer c.Close()
18
19 // All API operations must be run within a context.
20 ctx, cancel := context.WithCancel(context.Background())
21 defer cancel()
22
23 // Do something with the client.
24 if _, err := c.GetUser(ctx); err != nil {
25 panic(err)
26 }
27 }
28
29 func ExampleManager_NewClientWithRefresh() {
30 // Create a new manager.
31 m := proton.New()
32
33 // All API operations must be run within a context.
34 ctx, cancel := context.WithCancel(context.Background())
35 defer cancel()
36
37 // If UID/RefreshToken is already known, it can be used to create a new client straight away.
38 c, _, err := m.NewClientWithRefresh(ctx, "...uid...", "...ref...")
39 if err != nil {
40 panic(err)
41 }
42 defer c.Close()
43
44 // Do something with the client.
45 if _, err := c.GetUser(ctx); err != nil {
46 panic(err)
47 }
48 }
49
50 func ExampleManager_NewClientWithLogin() {
51 // Create a new manager.
52 m := proton.New()
53
54 // All API operations must be run within a context.
55 ctx, cancel := context.WithCancel(context.Background())
56 defer cancel()
57
58 // Clients are created via username/password if auth information isn't already known.
59 c, auth, err := m.NewClientWithLogin(ctx, "...user...", []byte("...pass..."))
60 if err != nil {
61 panic(err)
62 }
63 defer c.Close()
64
65 // If 2FA is necessary, an additional request is required.
66 if auth.TwoFA.Enabled&proton.HasTOTP != 0 {
67 if err := c.Auth2FA(ctx, proton.Auth2FAReq{TwoFactorCode: "...TOTP..."}); err != nil {
68 panic(err)
69 }
70 }
71
72 // Do something with the client.
73 if _, err := c.GetUser(ctx); err != nil {
74 panic(err)
75 }
76 }
77
78 func ExampleClient_AddAuthHandler() {
79 ctx, cancel := context.WithCancel(context.Background())
80 defer cancel()
81
82 // Create a new manager.
83 m := proton.New()
84
85 // Create a new client.
86 c := m.NewClient("...uid...", "...acc...", "...ref...")
87 defer c.Close()
88
89 // Register an auth handler with the client.
90 // This could be used for example to save the auth to keychain.
91 c.AddAuthHandler(func(auth proton.Auth) {
92 // Do something with auth.
93 })
94
95 if _, err := c.GetUser(ctx); err != nil {
96 panic(err)
97 }
98 }
99
100 func ExampleClient_NewEventStream() {
101 m := proton.New()
102
103 ctx, cancel := context.WithCancel(context.Background())
104 defer cancel()
105
106 c, _, err := m.NewClientWithLogin(ctx, "...user...", []byte("...pass..."))
107 if err != nil {
108 panic(err)
109 }
110 defer c.Close()
111
112 // Get the latest event ID.
113 fromEventID, err := c.GetLatestEventID(context.Background())
114 if err != nil {
115 panic(err)
116 }
117
118 // Create a new event streamer.
119 for event := range c.NewEventStream(ctx, 20*time.Second, 20*time.Second, fromEventID) {
120 fmt.Println(event.EventID)
121 }
122 }