This repository has been archived by the owner on Nov 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.go
75 lines (62 loc) · 1.39 KB
/
webhook.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
package krong
import (
"bytes"
"fmt"
"io"
"net/http"
"strings"
"time"
)
type Header struct {
Name string
Value string
}
type Headers []Header
type WebHook struct {
URL string
Method string
Headers Headers
Payload []byte
Timeout time.Duration
}
func (h *Headers) String() string {
if len(*h) == 0 {
return ""
}
result := make([]string, len(*h))
for i, header := range *h {
result[i] = fmt.Sprintf("%s=%s", header.Name, header.Value)
}
return strings.Join(result, ", ")
}
func (h *Headers) Set(value string) error {
splitResult := strings.SplitN(value, "=", 2)
if len(splitResult) != 2 {
return fmt.Errorf("header flag must be in name=value format")
}
*h = append(*h, Header{Name: splitResult[0], Value: splitResult[1]})
return nil
}
func (w *WebHook) Run() ([]byte, string, error) {
c := http.Client{Timeout: w.Timeout}
req, err := http.NewRequest(w.Method, w.URL, bytes.NewBuffer(w.Payload))
if err != nil {
return []byte(""), "", err
}
for _, h := range w.Headers {
req.Header.Add(h.Name, h.Value)
}
resp, err := c.Do(req)
if err != nil {
return []byte(""), "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return []byte(""), "", err
}
return body, resp.Status, nil
}
func (w *WebHook) String() string {
return fmt.Sprintf("URL: %s, Method: %s, Payload: %s, Timeout: %s", w.URL, w.Method, w.Payload, w.Timeout)
}