-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_list_test.go
166 lines (142 loc) · 4.12 KB
/
example_list_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
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
package paddle_test
import (
"context"
"fmt"
"os"
paddle "github.com/PaddleHQ/paddle-go-sdk/v3"
)
// Demonstrates how to fetch a list and iterate over the provided results.
func Example_list() {
// Create a mock HTTP server for this example - skip over this bit!
s := mockServerForExample(mockServerResponse{stub: &stub{paths: []stubPath{transactions}}})
// Create a new Paddle client.
client, err := paddle.New(
os.Getenv("PADDLE_API_KEY"),
paddle.WithBaseURL(s.URL), // Uses the mock server, you will not need this in your integration.
)
if err != nil {
fmt.Println(err)
return
}
ctx := context.Background()
// Optionally set a transit ID on the context. This is useful to link your
// own request IDs to Paddle API requests.
ctx = paddle.ContextWithTransitID(ctx, "sdk-testing-request-1")
// Get a collection of transactions.
res, err := client.ListTransactions(ctx, &paddle.ListTransactionsRequest{})
// Iterate the transactions.
err = res.Iter(ctx, func(v *paddle.Transaction) (bool, error) {
fmt.Println(v.ID)
return true, nil
})
fmt.Println(err)
// Output:
//txn_01hv8xxw3etar07vaxsqbyqasy
//txn_01hv8xbtmb6zc7c264ycteehth
//txn_01hv8wptq8987qeep44cyrewp9
//<nil>
}
// Demonstrates how to fetch a list and iterate over the provided results, including the automatic pagination.
func Example_pagination() {
// Create a mock HTTP server for this example - skip over this bit!
s := mockServerForExample(mockServerResponse{stub: &stub{paths: []stubPath{
transactionsPaginatedPg1,
transactionsPaginatedPg2,
}}})
// Create a new Paddle client.
client, err := paddle.New(
os.Getenv("PADDLE_API_KEY"),
paddle.WithBaseURL(s.URL), // Uses the mock server, you will not need this in your integration.
)
if err != nil {
fmt.Println(err)
return
}
ctx := context.Background()
// Optionally set a transit ID on the context. This is useful to link your
// own request IDs to Paddle API requests.
ctx = paddle.ContextWithTransitID(ctx, "sdk-testing-request-1")
// Get a collection of transactions.
res, err := client.ListTransactions(ctx, &paddle.ListTransactionsRequest{})
// Iterate the transactions which will internally paginate to the next page.
err = res.Iter(ctx, func(v *paddle.Transaction) (bool, error) {
fmt.Println(v.ID)
return true, nil
})
fmt.Println(err)
// Output:
//txn_01hv8xxw3etar07vaxsqbyqasy
//txn_01hv8xbtmb6zc7c264ycteehth
//txn_01hv8wptq8987qeep44cyrewp9
//txn_01hv8wnvvtedwjrhfhpr9vkq9w
//txn_01hv8m0mnx3sj85e7gxc6kga03
//txn_01hv8kxg3hxyxs9t471ms9kfsz
//<nil>
}
func Example_listWithoutPagination() {
s := mockServerForExample(mockServerResponse{stub: &stub{paths: []stubPath{
event_types,
}}})
// Create a new Paddle client.
client, err := paddle.New(
os.Getenv("PADDLE_API_KEY"),
paddle.WithBaseURL(s.URL), // Uses the mock server, you will not need this in your integration.
)
if err != nil {
fmt.Println(err)
return
}
ctx := context.Background()
// Get a collection of transactions.
res, err := client.ListEventTypes(ctx, &paddle.ListEventTypesRequest{})
// Iterate the transactions which will internally paginate to the next page.
err = res.Iter(ctx, func(v *paddle.EventType) (bool, error) {
fmt.Println(v.Name)
return true, nil
})
fmt.Println(err)
// Output:
//transaction.billed
//transaction.canceled
//transaction.completed
//transaction.created
//transaction.paid
//transaction.past_due
//transaction.payment_failed
//transaction.ready
//transaction.updated
//subscription.activated
//subscription.canceled
//subscription.created
//subscription.imported
//subscription.past_due
//subscription.paused
//subscription.resumed
//subscription.trialing
//subscription.updated
//product.created
//product.imported
//product.updated
//price.created
//price.imported
//price.updated
//customer.created
//customer.imported
//customer.updated
//address.created
//address.imported
//address.updated
//business.created
//business.imported
//business.updated
//adjustment.created
//adjustment.updated
//payout.created
//payout.paid
//discount.created
//discount.imported
//discount.updated
//report.created
//report.updated
//<nil>
}