-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
134 lines (110 loc) · 3.03 KB
/
example_test.go
File metadata and controls
134 lines (110 loc) · 3.03 KB
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
package flashduty_test
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"time"
flashduty "github.com/flashcatcloud/go-flashduty"
"github.com/flashcatcloud/go-flashduty/retry"
)
// ExampleNewClient shows how to construct a client with a couple of options.
func ExampleNewClient() {
client, err := flashduty.NewClient(
"YOUR_APP_KEY",
flashduty.WithTimeout(30*time.Second),
flashduty.WithUserAgent("my-app/1.0"),
)
if err != nil {
log.Fatal(err)
}
_ = client
fmt.Println("client ready")
}
// ExampleClient_Incidents lists open incidents and prints a short summary of each.
func ExampleClient_Incidents() {
ctx := context.Background()
client, err := flashduty.NewClient("YOUR_APP_KEY")
if err != nil {
log.Fatal(err)
}
req := &flashduty.ListIncidentsRequest{
Progress: "Triggered",
ListOptions: flashduty.ListOptions{Limit: 20},
}
list, resp, err := client.Incidents.List(ctx, req)
if err != nil {
log.Fatal(err)
}
fmt.Printf("request_id=%s total=%d\n", resp.RequestID, list.Total)
for _, inc := range list.Items {
fmt.Printf("[%s] %s\n", inc.IncidentSeverity, inc.Title)
}
}
// ExampleClient_Incidents_pagination walks every page using the search-after cursor.
func ExampleClient_Incidents_pagination() {
ctx := context.Background()
client, err := flashduty.NewClient("YOUR_APP_KEY")
if err != nil {
log.Fatal(err)
}
req := &flashduty.ListIncidentsRequest{
ListOptions: flashduty.ListOptions{Limit: 50},
}
// Bound the loop so a misbehaving cursor can never spin forever.
for page := 0; page < 100; page++ {
list, resp, err := client.Incidents.List(ctx, req)
if err != nil {
log.Fatal(err)
}
for _, inc := range list.Items {
fmt.Printf("%s: %s\n", inc.IncidentID, inc.Title)
}
if !resp.HasNextPage {
break
}
// Advance the cursor for the next request.
req.ListOptions.SearchAfterCtx = list.SearchAfterCtx
}
}
// ExampleClient_errorHandling distinguishes API errors from rate-limit errors.
func ExampleClient_errorHandling() {
ctx := context.Background()
client, err := flashduty.NewClient("YOUR_APP_KEY")
if err != nil {
log.Fatal(err)
}
_, _, err = client.Incidents.Info(ctx, &flashduty.IncidentInfoRequest{
IncidentID: "does-not-exist",
})
var rl *flashduty.RateLimitError
if errors.As(err, &rl) {
// Back off for the duration the server asked for, then retry.
time.Sleep(rl.RetryAfter)
return
}
var apiErr *flashduty.ErrorResponse
if errors.As(err, &apiErr) {
fmt.Printf("api error code=%s request_id=%s\n", apiErr.Code, apiErr.RequestID)
return
}
if err != nil {
log.Fatal(err)
}
}
// ExampleWithTransport_retry composes the retry helper as the client transport.
func ExampleWithTransport_retry() {
// retry.New returns an http.RoundTripper that transparently
// retries safe requests on 429 and 5xx responses with backoff.
var rt http.RoundTripper = retry.New()
client, err := flashduty.NewClient(
"YOUR_APP_KEY",
flashduty.WithTransport(rt),
)
if err != nil {
log.Fatal(err)
}
_ = client
fmt.Println("client with retry transport ready")
}