-
Notifications
You must be signed in to change notification settings - Fork 361
/
endpoint.go
136 lines (109 loc) · 2.96 KB
/
endpoint.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
package proxy
import (
"bytes"
"io/ioutil"
"net/http"
"time"
log "github.com/sirupsen/logrus"
)
//
// Public types
//
// EndpointConfig contains the optional configuration parameters of an EndpointClient.
type EndpointConfig struct {
HTTPClient *http.Client
Log *log.Logger
ResponseHandler EndpointResponseHandler
}
// ResponseHandler handles a response from the endpoint.
type EndpointResponseHandler interface {
ProcessResponse(string, *http.Response)
}
// ResponseHandlerFunc is an adapter to allow the use of ordinary
// functions as response handlers. If f is a function with the
// appropriate signature, ResponseHandler(f) is a
// ResponseHandler that calls f.
type EndpointResponseHandlerFunc func(string, *http.Response)
// ProcessResponse calls f(webhookID, resp).
func (f EndpointResponseHandlerFunc) ProcessResponse(webhookID string, resp *http.Response) {
f(webhookID, resp)
}
// EndpointClient is the client used to POST webhook requests to the local endpoint.
type EndpointClient struct {
// URL the client sends POST requests to
URL string
events map[string]bool
// Optional configuration parameters
cfg *EndpointConfig
}
// SupportsEventType takes an event of a webhook and compares it to the internal
// list of supported events
func (c *EndpointClient) SupportsEventType(eventType string) bool {
// Endpoint supports all events, always return true
if c.events["*"] || c.events[eventType] {
return true
}
return false
}
// Post sends a message to the local endpoint.
func (c *EndpointClient) Post(webhookID string, body string, headers map[string]string) error {
c.cfg.Log.WithFields(log.Fields{
"prefix": "proxy.EndpointClient.Post",
}).Debug("Forwarding event to local endpoint")
req, err := http.NewRequest(http.MethodPost, c.URL, bytes.NewBuffer([]byte(body)))
if err != nil {
return err
}
for k, v := range headers {
req.Header.Add(k, v)
}
resp, err := c.cfg.HTTPClient.Do(req)
if err != nil {
c.cfg.Log.Errorf("Failed to POST event to local endpoint, error = %v\n", err)
return err
}
defer resp.Body.Close()
c.cfg.ResponseHandler.ProcessResponse(webhookID, resp)
return nil
}
//
// Public functions
//
// NewEndpointClient returns a new EndpointClient.
func NewEndpointClient(url string, events []string, cfg *EndpointConfig) *EndpointClient {
if cfg == nil {
cfg = &EndpointConfig{}
}
if cfg.Log == nil {
cfg.Log = &log.Logger{Out: ioutil.Discard}
}
if cfg.HTTPClient == nil {
cfg.HTTPClient = &http.Client{
Timeout: defaultTimeout,
}
}
if cfg.ResponseHandler == nil {
cfg.ResponseHandler = EndpointResponseHandlerFunc(func(string, *http.Response) {})
}
return &EndpointClient{
URL: url,
events: convertToMap(events),
cfg: cfg,
}
}
//
// Private constants
//
const (
defaultTimeout = 30 * time.Second
)
//
// Private functions
//
func convertToMap(events []string) map[string]bool {
eventsMap := make(map[string]bool)
for _, event := range events {
eventsMap[event] = true
}
return eventsMap
}