-
Notifications
You must be signed in to change notification settings - Fork 5
/
http.go
254 lines (210 loc) · 6.84 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package illumioapi
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
// APIResponse contains the information from the response of the API
type APIResponse struct {
RespBody string
StatusCode int
Header http.Header
Request *http.Request
ReqBody string
}
// Unexported struct for handling the asyncResults
type asyncResults struct {
Href string `json:"href"`
JobType string `json:"job_type"`
Description string `json:"description"`
Result struct {
Href string `json:"href"`
} `json:"result"`
Status string `json:"status"`
RequestedAt string `json:"requested_at"`
TerminatedAt string `json:"terminated_at"`
RequestedBy struct {
Href string `json:"href"`
} `json:"requested_by"`
}
func httpSetUp(httpAction, apiURL string, pce PCE, body []byte, async bool, headers [][2]string) (APIResponse, error) {
var response APIResponse
var httpBody *bytes.Buffer
var asyncResults asyncResults
// Validate the provided action
httpAction = strings.ToUpper(httpAction)
if httpAction != "GET" && httpAction != "POST" && httpAction != "PUT" && httpAction != "DELETE" {
return response, errors.New("invalid http action string. action must be GET, POST, PUT, or DELETE")
}
// Get the base URL
u, err := url.Parse(apiURL)
if err != nil {
return APIResponse{}, err
}
baseURL := "https://" + u.Host + "/api/v2"
// Create body
httpBody = bytes.NewBuffer(body)
// Create HTTP client and request
client := &http.Client{}
if pce.DisableTLSChecking {
client.Transport = &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
}
req, err := http.NewRequest(httpAction, apiURL, httpBody)
if err != nil {
return response, err
}
// Set basic authentication and headers
req.SetBasicAuth(pce.User, pce.Key)
// Set the user provided headers
for _, h := range headers {
req.Header.Set(h[0], h[1])
}
// Set headers for async
if async {
req.Header.Set("Prefer", "respond-async")
}
// Make HTTP Request
resp, err := client.Do(req)
if err != nil {
return response, err
}
// Process Async requests
if async {
for asyncResults.Status != "done" {
asyncResults, err = polling(baseURL, pce, resp)
if err != nil {
return response, err
}
}
finalReq, err := http.NewRequest("GET", baseURL+asyncResults.Result.Href, httpBody)
if err != nil {
return response, err
}
// Set basic authentication and headers
finalReq.SetBasicAuth(pce.User, pce.Key)
finalReq.Header.Set("Content-Type", "application/json")
// Make HTTP Request
resp, err = client.Do(finalReq)
if err != nil {
return response, err
}
}
// Process response
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return response, err
}
// Put relevant response info into struct
response.RespBody = string(data[:])
response.StatusCode = resp.StatusCode
response.Header = resp.Header
response.Request = resp.Request
// Check for a 200 response code
if strconv.Itoa(resp.StatusCode)[0:1] != "2" {
return response, errors.New("http status code of " + strconv.Itoa(response.StatusCode))
}
// Return data and nil error
return response, nil
}
// polling is used in async requests to check when the data is ready
func polling(baseURL string, pce PCE, origResp *http.Response) (asyncResults, error) {
var asyncResults asyncResults
// Create HTTP client and request
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
pollReq, err := http.NewRequest("GET", baseURL+origResp.Header.Get("Location"), nil)
if err != nil {
return asyncResults, err
}
// Set basic authentication and headers
pollReq.SetBasicAuth(pce.User, pce.Key)
pollReq.Header.Set("Content-Type", "application/json")
// Wait for recommended time from Retry-After
wait, err := strconv.Atoi(origResp.Header.Get("Retry-After"))
if err != nil {
return asyncResults, err
}
duration := time.Duration(wait) * time.Second
time.Sleep(duration)
// Check if the data is ready
pollResp, err := client.Do(pollReq)
if err != nil {
return asyncResults, err
}
// Process Response
data, err := ioutil.ReadAll(pollResp.Body)
if err != nil {
return asyncResults, err
}
// Put relevant response info into struct
json.Unmarshal(data[:], &asyncResults)
return asyncResults, err
}
// apicall uses the httpSetup function with the header ContentType set to application/json header
// httpAction must be GET, POST, PUT, or DELETE.
// apiURL is the full endpoint being called.
// PUT and POST methods should have a body that is JSON run through the json.marshal function so it's a []byte.
// async parameter should be set to true for any GET requests returning > 500 items.
func apicall(httpAction, apiURL string, pce PCE, body []byte, async bool) (APIResponse, error) {
a, e := httpSetUp(httpAction, apiURL, pce, body, async, [][2]string{[2]string{"Content-Type", "application/json"}})
retry := 0
for a.StatusCode == 429 {
// If we have already tried 3 times, exit
if retry > 2 {
return a, fmt.Errorf("received three 429 errors with 30 second pauses between attempts")
}
// Increment the retry counter
retry++
// Sleep for 30 seconds
time.Sleep(30 * time.Second)
// Retry the API call
a, e = httpSetUp(httpAction, apiURL, pce, body, async, [][2]string{[2]string{"Content-Type", "application/json"}})
}
// Return once response code isn't 429 or if we've exceeded our attempts.
return a, e
}
// apicallNoContentType uses the httpSetup function without setting the header ContentType
// httpAction must be GET, POST, PUT, or DELETE.
// apiURL is the full endpoint being called.
// PUT and POST methods should have a body that is JSON run through the json.marshal function so it's a []byte.
// async parameter should be set to true for any GET requests returning > 500 items.
func apicallNoContentType(httpAction, apiURL string, pce PCE, body []byte, async bool) (APIResponse, error) {
a, e := httpSetUp(httpAction, apiURL, pce, body, async, [][2]string{})
retry := 0
for a.StatusCode == 429 {
// If we have already tried 3 times, exit
if retry > 2 {
return a, fmt.Errorf("received three 429 errors with 30 second pauses between attempts")
}
// Increment the retry counter
retry++
// Sleep for 30 seconds
time.Sleep(30 * time.Second)
// Retry the API call
a, e = httpSetUp(httpAction, apiURL, pce, body, async, [][2]string{})
}
// Return once response code isn't 429 or if we've exceeded our attempts.
return a, e
}
// pceSanitization cleans up the provided PCE FQDN in case of common errors
func pceSanitization(pceFQDN string) string {
// Remove trailing slash if included
if strings.HasSuffix(pceFQDN, "/") {
pceFQDN = pceFQDN[:len(pceFQDN)-1]
}
// Remove HTTPS if included
if strings.HasPrefix(pceFQDN, "https://") {
pceFQDN = strings.Replace(pceFQDN, "https://", "", -1)
}
return pceFQDN
}