-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmega_http_request.go
83 lines (67 loc) · 1.66 KB
/
mega_http_request.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
76
77
78
79
80
81
82
83
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
)
func Req(server Server, endpoint Endpoint) int {
var tr *http.Transport
_u := fmt.Sprintf(urlformat, server.Host, endpoint.Path)
tr = &http.Transport{}
tr.ResponseHeaderTimeout = endpoint.Timeout * time.Second
requestBodyReader := strings.NewReader(endpoint.Data)
req, _ := http.NewRequest(endpoint.Method, _u, requestBodyReader)
SetHeaders(endpoint.Headers, req)
client := &http.Client{Transport: tr}
resp, err := client.Do(req)
var ert int
if err != nil {
ert = NetworkAccessErrorCode
log.Println(err)
} else {
ert = resp.StatusCode
}
if resp != nil {
if resp.Body != nil {
resp.Body.Close()
}
}
tr.CloseIdleConnections()
return ert
}
func GetMetricData() (data []byte) {
var tr *http.Transport
_u := fmt.Sprintf(APIFormat, Config.KubeSettings.MetricAPIHost, Config.KubeSettings.MetricAPIPort, Config.KubeSettings.MetricAPIPath)
tr = &http.Transport{}
tr.ResponseHeaderTimeout = 60 * time.Second
req, _ := http.NewRequest("GET", _u, nil)
client := &http.Client{Transport: tr}
resp, err := client.Do(req)
SetHeaders(APIAuthHeaders, req)
if err != nil {
Config.KubeSettings.BadConfig = true
} else {
Config.KubeSettings.BadConfig = false
}
if resp != nil {
if resp.Body != nil {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
data = bodyBytes
resp.Body.Close()
}
}
tr.CloseIdleConnections()
return
}
func SetHeaders(headers string, req *http.Request) {
sets := strings.Split(headers, NewLine)
for i := range sets {
split := strings.SplitN(sets[i], HeaderSeparator, 2)
if len(split) == 2 {
req.Header.Set(split[0], split[1])
}
}
}