/
service.go
121 lines (101 loc) · 2.88 KB
/
service.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
package webhook
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
)
type (
// Service is the webhook service implementation.
Service struct {
client *http.Client
signatureHeader string
signatureSecret []byte
webhookURI string
}
// ServiceOption is a function that configures the webhook service.
ServiceOption func(*Service)
)
// NewService creates a new webhook service.
func NewService(opts ...ServiceOption) *Service {
s := &Service{
client: &http.Client{
Timeout: 10 * time.Second,
},
signatureHeader: DefaultSignatureHeader,
}
for _, opt := range opts {
opt(s)
}
return s
}
// WithHTTPClient configures the webhook service with a custom HTTP client.
func WithHTTPClient(client *http.Client) ServiceOption {
return func(s *Service) {
s.client = client
}
}
// WithSignatureHeader configures the webhook service with a custom signature header.
func WithSignatureHeader(header string) ServiceOption {
return func(s *Service) {
s.signatureHeader = strings.TrimSpace(header)
}
}
// WithSignatureSecret configures the webhook service with a custom signature secret.
func WithSignatureSecret(secret []byte) ServiceOption {
return func(s *Service) {
s.signatureSecret = secret
}
}
// WithWebhookURI configures the webhook service with a custom webhook URI.
func WithWebhookURI(uri string) ServiceOption {
return func(s *Service) {
s.webhookURI = uri
}
}
// Send post request to webhook url with payload.
func (s *Service) Send(url string, payload interface{}) (*http.Response, error) {
body, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("failed to marshal webhook payload: %w", err)
}
signature, err := SignPayload(body, s.signatureSecret)
if err != nil {
return nil, fmt.Errorf("failed to sign webhook payload: %w", err)
}
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(body))
if err != nil {
return nil, fmt.Errorf("failed to create webhook request: %w", err)
}
req.Header.Set("Content-Type", ContentTypeJSON)
req.Header.Set(s.signatureHeader, signature)
resp, err := s.client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to make POST request: %w", err)
}
return resp, nil
}
// FireEvent sends a webhook event to the webhook url.
func (s *Service) FireEvent(event string, payload interface{}) error {
if s.webhookURI == "" {
return fmt.Errorf("webhook uri is not set")
}
return s.fireEvent(event, s.webhookURI, payload)
}
// fireEvent sends a webhook event to the webhook url.
func (s *Service) fireEvent(event, url string, payload interface{}) error {
reqData := WebhookRequestPayload{
Event: event,
Data: payload,
}
resp, err := s.Send(url, reqData)
if err != nil {
return fmt.Errorf("failed to send webhook event: %w", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to send webhook event: %s", resp.Status)
}
return nil
}