-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttp.go
189 lines (148 loc) · 4.9 KB
/
http.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package drycc
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"strings"
)
// createHTTPClient creates a HTTP Client with proper SSL options.
func createHTTPClient(sslVerify bool) *http.Client {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: !sslVerify},
DisableKeepAlives: true,
Proxy: http.ProxyFromEnvironment,
}
return &http.Client{Transport: tr}
}
// Do sends an HTTP request and returns an HTTP response,
// following policy (such as redirects, cookies, auth) as configured on the client.
func (c *Client) Do(req *http.Request) (*http.Response, error) {
if c.Token != "" {
req.Header.Add("Authorization", "token "+c.Token)
}
if c.HooksToken != "" {
req.Header.Add("X-Drycc-Builder-Auth", c.HooksToken)
}
if req.Header.Get("Content-Type") == "" {
req.Header.Add("Content-Type", "application/json")
}
addUserAgent(&req.Header, c.UserAgent)
res, err := c.HTTPClient.Do(req)
if err != nil {
return res, err
}
if err = checkForErrors(res); err != nil {
return res, err
}
apiVersion := res.Header.Get("DRYCC_API_VERSION")
// Update controller api and platform version
c.ControllerAPIVersion = apiVersion
setControllerVersion(c, res.Header)
// Return results along with api compatibility error
return res, CheckAPICompatibility(apiVersion, APIVersion)
}
// NewRequest wraps [NewRequestWithContext] using [context.Background].
func (c *Client) NewRequest(method string, path string, body io.Reader) (*http.Request, error) {
url := *c.ControllerURL
if strings.Contains(path, "?") {
parts := strings.Split(path, "?")
url.Path = parts[0]
url.RawQuery = parts[1]
} else {
url.Path = path
}
return http.NewRequest(method, url.String(), body)
}
// Request makes a HTTP request with the given method, relative URL, and body on the controller.
// It also sets the Authorization and Content-Type headers to properly authenticate and communicate
// API. This is primarily intended to use be used by the SDK itself, but could potentially be used elsewhere.
func (c *Client) Request(method string, path string, body []byte) (*http.Response, error) {
req, err := c.NewRequest(method, path, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
return c.Do(req)
}
// LimitedRequest allows limiting the number of responses in a request.
func (c *Client) LimitedRequest(path string, results int) (string, int, error) {
res, reqErr := c.Request("GET", path+"?limit="+strconv.Itoa(results), nil)
if reqErr != nil && !IsErrAPIMismatch(reqErr) {
return "", -1, reqErr
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return "", -1, err
}
r := make(map[string]interface{})
if err = json.Unmarshal([]byte(body), &r); err != nil {
return "", -1, err
}
out, err := json.Marshal(r["results"].([]interface{}))
if err != nil {
return "", -1, err
}
return string(out), int(r["count"].(float64)), reqErr
}
// CheckConnection checks that the user is connected to a network and the URL points to a valid controller.
func (c *Client) CheckConnection() error {
errorMessage := `%s does not appear to be a valid Drycc controller.
Make sure that the Controller URI is correct, the server is running and
your drycc version is correct.`
// Make a request to /v2/ and expect a 401 response
req, err := http.NewRequest("GET", c.ControllerURL.String()+"/v2/", bytes.NewBuffer(nil))
addUserAgent(&req.Header, c.UserAgent)
if err != nil {
return err
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != 401 {
return fmt.Errorf(errorMessage, c.ControllerURL.String())
}
// Update controller api version
apiVersion := res.Header.Get("DRYCC_API_VERSION")
c.ControllerAPIVersion = apiVersion
setControllerVersion(c, res.Header)
return CheckAPICompatibility(apiVersion, APIVersion)
}
// Healthcheck can be called to see if the controller is healthy
func (c *Client) Healthcheck() error {
// Make a request to /healthz and expect an ok HTTP response
controllerURL := c.ControllerURL.String()
// Don't double the last slash in the URL path
if !strings.HasSuffix(controllerURL, "/") {
controllerURL = controllerURL + "/"
}
req, err := http.NewRequest("GET", controllerURL+"healthz", bytes.NewBuffer(nil))
addUserAgent(&req.Header, c.UserAgent)
if err != nil {
return err
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
if err = checkForErrors(res); err != nil {
return err
}
res.Body.Close()
// Update controller api version
apiVersion := res.Header.Get("DRYCC_API_VERSION")
c.ControllerAPIVersion = apiVersion
setControllerVersion(c, res.Header)
return CheckAPICompatibility(apiVersion, APIVersion)
}
func addUserAgent(headers *http.Header, userAgent string) {
headers.Add("User-Agent", userAgent)
}
func setControllerVersion(c *Client, headers http.Header) {
c.ControllerVersion = headers.Get("DRYCC_PLATFORM_VERSION")
}