/
http_proxy_sender.go
69 lines (55 loc) · 1.62 KB
/
http_proxy_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
58
59
60
61
62
63
64
65
66
67
68
69
package senders
import (
"bytes"
"crypto/tls"
"encoding/json"
"io/ioutil"
"net/http"
interfaces "github.com/shoplineapp/captin/interfaces"
models "github.com/shoplineapp/captin/models"
log "github.com/sirupsen/logrus"
)
var hpLogger = log.WithFields(log.Fields{"class": "HTTPProxyEventSender"})
// HTTPProxyResponse - HTTP Response
type HTTPProxyResponse struct {
url string
response *http.Response
err error
}
// HTTPProxyEventSender - Send Event through HTTP with payload only
// Different from HTTPEventSender, which parses the whole event body
// in order to pass event meta data to destinations,
// HTTPProxyEventSender only parses payload for general usage of
// third party API calls.
type HTTPProxyEventSender struct {
interfaces.EventSenderInterface
}
// SendEvent - #HTTPProxyEventSender SendEvent
func (c *HTTPProxyEventSender) SendEvent(ev interfaces.IncomingEventInterface, dv interfaces.DestinationInterface) error {
e := ev.(models.IncomingEvent)
d := dv.(models.Destination)
url := d.GetCallbackURL()
payload, err := json.Marshal(e.Payload)
if err != nil {
return err
}
req, reqErr := http.NewRequest("POST", url, bytes.NewBuffer(payload))
if reqErr != nil {
return reqErr
}
req.Header.Set("Content-Type", "application/json")
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
Transport: tr,
}
res, resErr := client.Do(req)
if resErr != nil {
return resErr
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
hpLogger.WithFields(log.Fields{"result": string(body)}).Debug("Send http event with result")
return nil
}