-
Notifications
You must be signed in to change notification settings - Fork 739
/
sender.go
45 lines (38 loc) · 1.07 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
package eventchannel
import (
"bytes"
"fmt"
"github.com/golang/glog"
"net/http"
"net/url"
"path"
)
type Sender = func(payload []byte) error
func NewHttpSender(client *http.Client, endpoint string) Sender {
return func(payload []byte) error {
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewReader(payload))
if err != nil {
glog.Error(err)
return err
}
req.Header.Set("Content-Type", "application/octet-stream")
req.Header.Set("Content-Encoding", "gzip")
resp, err := client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
glog.Errorf("[pubstack] Wrong code received %d instead of %d", resp.StatusCode, http.StatusOK)
return fmt.Errorf("wrong code received %d instead of %d", resp.StatusCode, http.StatusOK)
}
return nil
}
}
func BuildEndpointSender(client *http.Client, baseUrl string, module string) Sender {
endpoint, err := url.Parse(baseUrl)
if err != nil {
glog.Error(err)
}
endpoint.Path = path.Join(endpoint.Path, "intake", module)
return NewHttpSender(client, endpoint.String())
}