-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
53 lines (44 loc) · 960 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
package dingtalk
import (
"bytes"
"context"
"io/ioutil"
"net"
"net/http"
"time"
)
var httpClient *http.Client
func init() {
httpClient = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
},
}
}
func request(url string, body []byte) (data []byte, err error) {
// timeout context
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(body))
if err != nil {
return
}
req.Header.Set("Content-type", "application/json")
resp, err := httpClient.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
data, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
return
}