-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_simulations_test.go
342 lines (286 loc) · 10.4 KB
/
example_simulations_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
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
package paddle_test
import (
"context"
"fmt"
"os"
"time"
paddle "github.com/PaddleHQ/paddle-go-sdk/v3"
"github.com/PaddleHQ/paddle-go-sdk/v3/pkg/paddlenotification"
)
// Demonstrates how to create a Simulation with Payload and read the Payload back out of the response
func Example_simulation_create() {
// Create a mock HTTP server for this example - skip over this bit!
s := mockServerForExample(mockServerResponse{stub: &stub{paths: []stubPath{simulation}}})
// 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")
simulation, err := client.CreateSimulation(ctx, paddle.NewCreateSimulationRequestSimulationSingleEventCreate(&paddle.SimulationSingleEventCreate{
NotificationSettingID: "ntfset_01j84xydheq48n3btebwf6ndn6",
Name: "Go SDK Test with Payload",
Type: "customer.created",
Payload: paddlenotification.CustomerNotification{
ID: "ctm_01j870snka0xdp6szgyxze6d6d",
Name: paddle.PtrTo("John Doe"),
Email: "[email protected]",
MarketingConsent: false,
Status: paddlenotification.StatusActive,
CustomData: nil,
Locale: "en",
CreatedAt: time.Date(2024, 4, 12, 0, 0, 0, 0, time.UTC).Format(time.RFC3339),
UpdatedAt: time.Date(2024, 4, 12, 0, 0, 0, 0, time.UTC).Format(time.RFC3339),
ImportMeta: nil,
},
}))
if err != nil {
return
}
// Use type assertion on the response for payload
payload, ok := (simulation.Payload).(*paddlenotification.CustomerNotification)
if !ok {
fmt.Println("Payload is unexpected type")
return
}
fmt.Println(simulation.ID)
fmt.Println(payload.ID)
// Output:
//ntfsim_01j9y0jwekrcyezscgkehvdmd6
//ctm_01j870snka0xdp6szgyxze6d6d
}
// Demonstrates how to create a scenario Simulation
func Example_simulation_create_scenario_simulation() {
// Create a mock HTTP server for this example - skip over this bit!
s := mockServerForExample(mockServerResponse{stub: &stub{paths: []stubPath{simulationScenario}}})
// 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")
simulation, err := client.CreateSimulation(ctx, paddle.NewCreateSimulationRequestSimulationScenarioCreate(&paddle.SimulationScenarioCreate{
NotificationSettingID: "ntfset_01j84xydheq48n3btebwf6ndn6",
Name: "Go SDK Test without Payload",
Type: "subscription_creation",
}))
if err != nil {
return
}
fmt.Println(simulation.ID)
fmt.Println(simulation.Type)
fmt.Println(simulation.Name)
fmt.Println(simulation.NotificationSettingID)
fmt.Println(simulation.Payload)
// Output:
//ntfsim_01j9y0jwekrcyezscgkehvdmd6
//subscription_creation
//Go SDK Test without Payload
//ntfset_01j84xydheq48n3btebwf6ndn6
//<nil>
}
// Demonstrates how to update a Simulation with Payload and read the Payload back out of the response
func Example_simulation_update() {
// Create a mock HTTP server for this example - skip over this bit!
s := mockServerForExample(mockServerResponse{stub: &stub{paths: []stubPath{simulation}}})
// 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")
simulation, err := client.UpdateSimulation(
ctx,
paddle.NewUpdateSimulationRequestSimulationSingleEventUpdate(
"ntfsim_01j9y0jwekrcyezscgkehvdmd6",
&paddle.SimulationSingleEventUpdate{
NotificationSettingID: "ntfset_01j84xydheq48n3btebwf6ndn6",
Name: "Go SDK Test with Payload",
Type: "customer.created",
Payload: paddlenotification.CustomerNotification{
ID: "ctm_01j870snka0xdp6szgyxze6d6d",
Name: paddle.PtrTo("John Doe"),
Email: "[email protected]",
MarketingConsent: false,
Status: paddlenotification.StatusActive,
CustomData: nil,
Locale: "en",
CreatedAt: time.Date(2024, 4, 12, 0, 0, 0, 0, time.UTC).Format(time.RFC3339),
UpdatedAt: time.Date(2024, 4, 12, 0, 0, 0, 0, time.UTC).Format(time.RFC3339),
ImportMeta: nil,
},
},
),
)
if err != nil {
return
}
// Use type assertion on the response for payload
payload, ok := (simulation.Payload).(*paddlenotification.CustomerNotification)
if !ok {
fmt.Println("Payload is unexpected type")
return
}
fmt.Println(simulation.ID)
fmt.Println(payload.ID)
// Output:
//ntfsim_01j9y0jwekrcyezscgkehvdmd6
//ctm_01j870snka0xdp6szgyxze6d6d
}
// // Demonstrates how to list Simulations with Payload and read the Payload back out of the response
func Example_simulation_list() {
// Create a mock HTTP server for this example - skip over this bit!
s := mockServerForExample(mockServerResponse{stub: &stub{paths: []stubPath{simulations}}})
// 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")
simulations, err := client.ListSimulations(ctx, &paddle.ListSimulationsRequest{})
if err != nil {
}
simulations.Iter(ctx, func(s *paddle.Simulation) (bool, error) {
switch p := s.Payload.(type) {
case *paddlenotification.AddressNotification:
// here v could be used as concrete type AddressNotification
fmt.Println(p.CustomerID)
case *paddlenotification.CustomerNotification:
// here v could be used as concrete type CustomerNotification
fmt.Println(p.Email)
}
return true, nil
})
// Output:
//ctm_01hv6y1jedq4p1n0yqn5ba3ky4
}
// Demonstrates how to get a Simulation with Payload and read the Payload back out of the response
func Example_simulation_get() {
// Create a mock HTTP server for this example - skip over this bit!
s := mockServerForExample(mockServerResponse{stub: &stub{paths: []stubPath{simulation}}})
// 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")
simulation, err := client.GetSimulation(ctx, &paddle.GetSimulationRequest{SimulationID: "ntfsim_01j9y0jwekrcyezscgkehvdmd6"})
if err != nil {
return
}
// Use type assertion on the response for payload
payload, ok := (simulation.Payload).(*paddlenotification.CustomerNotification)
if !ok {
fmt.Println("Payload is unexpected type")
return
}
fmt.Println(simulation.ID)
fmt.Println(payload.ID)
// Output:
//ntfsim_01j9y0jwekrcyezscgkehvdmd6
//ctm_01j870snka0xdp6szgyxze6d6d
}
// Demonstrates how to run a Simulation
func Example_simulation_run() {
// Create a mock HTTP server for this example - skip over this bit!
s := mockServerForExample(mockServerResponse{stub: &stub{paths: []stubPath{simulationRun}}})
// 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")
simulationRun, err := client.CreateSimulationRun(ctx, &paddle.CreateSimulationRunRequest{
SimulationID: "ntfsim_01j9y0jwekrcyezscgkehvdmd6",
})
if err != nil {
return
}
fmt.Println(simulationRun.ID)
// Output:
//ntfsimrun_01j9yq3yspewy5r8zr05vkeekd
}
// Demonstrates how to get a SimulationRun with included SimulationRunEvents
func Example_simulation_run_get() {
// Create a mock HTTP server for this example - skip over this bit!
s := mockServerForExample(mockServerResponse{stub: &stub{paths: []stubPath{simulationRunWithEvents}}})
// 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")
simulationRun, err := client.GetSimulationRun(ctx, &paddle.GetSimulationRunRequest{
SimulationID: "ntfsim_01j9y0jwekrcyezscgkehvdmd6",
SimulationRunID: "ntfsimrun_01j9yq3yspewy5r8zr05vkeekd",
IncludeEvents: true,
})
if err != nil {
return
}
// Use type assertion on the response for payload
for _, event := range simulationRun.Events {
payload, ok := (event.Payload).(*paddlenotification.CustomerNotification)
if !ok {
fmt.Println("Payload is unexpected type")
return
}
fmt.Println(payload.ID)
fmt.Println(event.Response.StatusCode)
}
fmt.Println(simulationRun.ID)
// Output:
//ctm_01j870snka0xdp6szgyxze6d6d
//200
//ntfsimrun_01j9yq3yspewy5r8zr05vkeekd
}