-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpclient.go
49 lines (44 loc) · 1.05 KB
/
httpclient.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
package httpclient
import (
"io/ioutil"
"net/http"
"time"
)
// HTTPClient interface for http communication
type HTTPClient interface {
SendGetRequest(uri string, headers map[string]string) ([]byte, error)
}
// Impl implements httpClient interface
type Impl struct {
}
func getClient() *http.Client {
tr := &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 30 * time.Second,
DisableCompression: false,
}
return &http.Client{Transport: tr}
}
// SendGetRequest will send a "GET" request to the uri with the optional additional headers. Will return a byte array of the body or error.
func (h *Impl) SendGetRequest(uri string, headers map[string]string) ([]byte, error) {
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, err
}
if headers != nil {
for k, value := range headers {
req.Header.Set(k, value)
}
}
client := getClient()
r, err := client.Do(req)
if err != nil {
return nil, err
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
return body, nil
}