-
Notifications
You must be signed in to change notification settings - Fork 3
/
sender.go
57 lines (45 loc) · 1.29 KB
/
sender.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
package http
import (
"net/http"
sh "github.com/alexfalkowski/go-service/transport/http"
h "github.com/alexfalkowski/go-service/transport/http/hooks"
events "github.com/cloudevents/sdk-go/v2"
"github.com/cloudevents/sdk-go/v2/client"
eh "github.com/cloudevents/sdk-go/v2/protocol/http"
hooks "github.com/standard-webhooks/standard-webhooks/libraries/go"
)
// SenderOption for HTTP.
type SenderOption interface{ apply(opts *senderOptions) }
type senderOptions struct {
roundTripper http.RoundTripper
hook *hooks.Webhook
}
type senderOptionFunc func(*senderOptions)
func (f senderOptionFunc) apply(o *senderOptions) { f(o) }
// WithSenderRoundTripper for HTTP.
func WithSenderRoundTripper(rt http.RoundTripper) SenderOption {
return senderOptionFunc(func(o *senderOptions) {
o.roundTripper = rt
})
}
// WithSenderHook for HTTP.
func WithSenderHook(hook *hooks.Webhook) SenderOption {
return senderOptionFunc(func(o *senderOptions) {
o.hook = hook
})
}
// NewSender for HTTP.
func NewSender(opts ...SenderOption) (client.Client, error) {
os := &senderOptions{}
for _, o := range opts {
o.apply(os)
}
rt := os.roundTripper
if rt == nil {
rt = sh.Transport()
}
if os.hook != nil {
rt = h.NewRoundTripper(os.hook, rt)
}
return events.NewClientHTTP(eh.WithRoundTripper(rt))
}