forked from veepee-oss/influxdb-relay
-
Notifications
You must be signed in to change notification settings - Fork 24
/
poster.go
116 lines (97 loc) · 2.69 KB
/
poster.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package backend
import (
"bytes"
"crypto/tls"
"io/ioutil"
"net/http"
"strconv"
"time"
)
type poster interface {
Post([]byte, string, string, string, string) (*ResponseData, error)
Query(string, string, string, string, []byte) (*http.Response, error)
GetStats() map[string]string
}
type simplePoster struct {
serverid string
clusterid string
client *http.Client
location string
}
func newSimplePoster(serverid string, location string, clusterid string, timeout time.Duration, skipTLSVerification bool) *simplePoster {
// Configure custom transport for http.Client
// Used for support skip-tls-verification option
transport := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: skipTLSVerification,
},
}
return &simplePoster{
client: &http.Client{
Timeout: timeout,
Transport: transport,
},
location: location,
serverid: serverid,
clusterid: clusterid,
}
}
func (s *simplePoster) GetStats() map[string]string {
v := make(map[string]string)
v["location"] = s.location
return v
}
func (s *simplePoster) Post(buf []byte, query string, auth string, endpoint string, ctype string) (*ResponseData, error) {
ret := &ResponseData{Serverid: s.serverid, Clusterid: s.clusterid, Location: s.location}
req, err := http.NewRequest("POST", s.location+endpoint, bytes.NewReader(buf))
if err != nil {
return ret, err
}
req.URL.RawQuery = query //<-Review
if len(ctype) > 0 {
req.Header.Set("Content-Type", ctype)
} else {
req.Header.Set("Content-Type", "text/plain")
}
req.Header.Set("Content-Length", strconv.Itoa(len(buf)))
if auth != "" {
req.Header.Set("Authorization", auth)
}
req.Header.Set("User-Agent", "influxdb-smart-relay")
resp, err := s.client.Do(req)
if err != nil {
return ret, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return ret, err
}
ret.ContentEncoding = resp.Header.Get("Content-Encoding")
ret.ContentType = resp.Header.Get("Content-Type")
ret.StatusCode = resp.StatusCode
ret.Body = data
return ret, nil
}
func (s *simplePoster) Query(method string, params string, authHeader string, endpoint string, body []byte) (*http.Response, error) {
req, err := http.NewRequest(method, s.location+endpoint, bytes.NewReader(body))
if err != nil {
return nil, err
}
req.URL.RawQuery = params
req.Header.Set("User-Agent", "influxdb-smart-relay")
if len(body) > 0 {
req.Header.Add("Accept", "application/csv")
req.Header.Add("Content-Type", "application/vnd.flux")
} else {
req.Header.Add("Accept", "application/json")
}
if len(authHeader) > 0 {
req.Header.Set("Authorization", authHeader)
}
resp, err := s.client.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}