forked from ProtonMail/go-proton-api
-
Notifications
You must be signed in to change notification settings - Fork 4
/
example_test.go
122 lines (97 loc) · 2.82 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
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
package proton_test
import (
"context"
"fmt"
"time"
"github.com/henrybear327/go-proton-api"
)
func ExampleManager_NewClient() {
// Create a new manager.
m := proton.New()
// If auth information is already known, it can be used to create a client straight away.
c := m.NewClient("...uid...", "...acc...", "...ref...")
defer c.Close()
// All API operations must be run within a context.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Do something with the client.
if _, err := c.GetUser(ctx); err != nil {
panic(err)
}
}
func ExampleManager_NewClientWithRefresh() {
// Create a new manager.
m := proton.New()
// All API operations must be run within a context.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// If UID/RefreshToken is already known, it can be used to create a new client straight away.
c, _, err := m.NewClientWithRefresh(ctx, "...uid...", "...ref...")
if err != nil {
panic(err)
}
defer c.Close()
// Do something with the client.
if _, err := c.GetUser(ctx); err != nil {
panic(err)
}
}
func ExampleManager_NewClientWithLogin() {
// Create a new manager.
m := proton.New()
// All API operations must be run within a context.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Clients are created via username/password if auth information isn't already known.
c, auth, err := m.NewClientWithLogin(ctx, "...user...", []byte("...pass..."))
if err != nil {
panic(err)
}
defer c.Close()
// If 2FA is necessary, an additional request is required.
if auth.TwoFA.Enabled&proton.HasTOTP != 0 {
if err := c.Auth2FA(ctx, proton.Auth2FAReq{TwoFactorCode: "...TOTP..."}); err != nil {
panic(err)
}
}
// Do something with the client.
if _, err := c.GetUser(ctx); err != nil {
panic(err)
}
}
func ExampleClient_AddAuthHandler() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Create a new manager.
m := proton.New()
// Create a new client.
c := m.NewClient("...uid...", "...acc...", "...ref...")
defer c.Close()
// Register an auth handler with the client.
// This could be used for example to save the auth to keychain.
c.AddAuthHandler(func(auth proton.Auth) {
// Do something with auth.
})
if _, err := c.GetUser(ctx); err != nil {
panic(err)
}
}
func ExampleClient_NewEventStream() {
m := proton.New()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c, _, err := m.NewClientWithLogin(ctx, "...user...", []byte("...pass..."))
if err != nil {
panic(err)
}
defer c.Close()
// Get the latest event ID.
fromEventID, err := c.GetLatestEventID(context.Background())
if err != nil {
panic(err)
}
// Create a new event streamer.
for event := range c.NewEventStream(ctx, 20*time.Second, 20*time.Second, fromEventID) {
fmt.Println(event.EventID)
}
}