forked from raystack/siren
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
125 lines (103 loc) · 2.88 KB
/
client.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
package pagerduty
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"github.com/goto/siren/pkg/httpclient"
"github.com/goto/siren/pkg/retry"
)
const (
defaultPagerdutyHost = "https://events.pagerduty.com"
)
type eventsV1HTTPResponse struct {
Status string `json:"status"`
Message string `json:"message"`
IncidentKey string `json:"incident_key"`
}
type ClientOption func(*Client)
// ClientWithHTTPClient assigns custom client when creating a pagerduty client
func ClientWithHTTPClient(cli *httpclient.Client) ClientOption {
return func(c *Client) {
c.httpClient = cli
}
}
// ClientWithRetrier wraps client call with retrier
func ClientWithRetrier(runner retry.Runner) ClientOption {
return func(c *Client) {
c.retrier = runner
}
}
type Client struct {
cfg AppConfig
httpClient *httpclient.Client
retrier retry.Runner
}
func NewClient(cfg AppConfig, opts ...ClientOption) *Client {
c := &Client{
cfg: cfg,
}
for _, opt := range opts {
opt(c)
}
if cfg.APIHost == "" {
c.cfg.APIHost = defaultPagerdutyHost
}
if c.httpClient == nil {
c.httpClient = httpclient.New(cfg.HTTPClient)
}
return c
}
func (c *Client) NotifyV1(ctx context.Context, msg MessageV1) error {
if c.retrier != nil {
if err := c.retrier.Run(ctx, func(ctx context.Context) error {
return c.notifyV1(ctx, msg)
}); err != nil {
return err
}
}
return c.notifyV1(ctx, msg)
}
func (c *Client) notifyV1(ctx context.Context, message MessageV1) error {
// TODO need to sanitize body first?
messageJSON, err := json.Marshal(message)
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.cfg.APIHost+"/generic/2010-04-15/create_event.json", bytes.NewReader(messageJSON))
if err != nil {
return fmt.Errorf("failed to create request body: %w", err)
}
resp, err := c.httpClient.HTTP().Do(req)
if err != nil {
return fmt.Errorf("failure in http call: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode == 429 || resp.StatusCode >= 500 {
return retry.RetryableError{Err: errors.New(http.StatusText(resp.StatusCode))}
}
if resp.StatusCode >= 300 {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error with status code %s without response body", http.StatusText(resp.StatusCode))
}
return fmt.Errorf("error with status code %s and body %s", http.StatusText(resp.StatusCode), string(bodyBytes))
} else {
// Status code 2xx only
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response body: %w", err)
}
apiResponse := eventsV1HTTPResponse{}
if err = json.Unmarshal(bodyBytes, &apiResponse); err != nil {
return fmt.Errorf("failed to unmarshal response body: %w", err)
}
if apiResponse.Status != "success" {
return fmt.Errorf("something wrong when sending pagerduty event: %v", apiResponse)
}
}
return nil
}