-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
58 lines (47 loc) · 939 Bytes
/
client.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"time"
)
var client *http.Client
func init() {
client = httpClient()
}
func httpClient() *http.Client {
return &http.Client{
Transport: &http.Transport{
MaxIdleConnsPerHost: *concurrency * 2,
},
Timeout: 10 * time.Second,
}
}
func sendWebhookRequest(body interface{}) error {
strJson, err := json.Marshal(body)
if err != nil {
return nil
}
req, err := http.NewRequest(http.MethodPost, *endpointURL, bytes.NewBuffer(strJson))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Basic "+apiKey)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(ioutil.Discard, resp.Body)
if err != nil {
return err
}
if resp.StatusCode >= 400 {
return fmt.Errorf("status code: %s", resp.Status)
}
return nil
}