-
Notifications
You must be signed in to change notification settings - Fork 4
/
http.go
210 lines (180 loc) · 5.41 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
package illumioapi
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"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
Warnings []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 (p *PCE) httpSetup(action, apiURL string, body []byte, async bool, headers [][2]string) (APIResponse, error) {
var asyncResults asyncResults
// 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 p.DisableTLSChecking {
client.Transport = &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
}
req, err := http.NewRequest(action, apiURL, httpBody)
if err != nil {
return APIResponse{}, err
}
// Set basic authentication and headers
req.SetBasicAuth(p.User, p.Key)
for _, h := range headers {
req.Header.Set(h[0], h[1])
}
if async {
req.Header.Set("Prefer", "respond-async")
}
// Make HTTP Request
resp, err := client.Do(req)
if err != nil {
return APIResponse{}, err
}
// Process Async requests
if async {
for asyncResults.Status != "done" {
asyncResults, err = p.asyncPoll(baseURL, resp)
if err != nil {
return APIResponse{}, err
}
}
finalReq, err := http.NewRequest("GET", baseURL+asyncResults.Result.Href, httpBody)
if err != nil {
return APIResponse{}, err
}
// Set basic authentication and headers
finalReq.SetBasicAuth(p.User, p.Key)
finalReq.Header.Set("Content-Type", "application/json")
// Make HTTP Request
resp, err = client.Do(finalReq)
if err != nil {
return APIResponse{}, err
}
}
// Process response
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return APIResponse{}, err
}
// Put relevant response info into struct
var response APIResponse
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
}
// asyncPoll is used in async requests to check when the data is ready
func (p *PCE) asyncPoll(baseURL string, origResp *http.Response) (asyncResults asyncResults, err error) {
// 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(p.User, p.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
}
// httpReq makes an API call to the PCE with sepcified options
// 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 (p *PCE) httpReq(action, apiURL string, body []byte, async bool, jsonContentType bool) (APIResponse, error) {
// Set headers based on jsonContentType
headers := [][2]string{{"Content-Type", "application/json"}}
if !jsonContentType {
headers = nil
}
// Make initial http call
api, err := p.httpSetup(action, apiURL, body, async, headers)
retry := 0
// If the status code is 429, try 3 times
for api.StatusCode == 429 {
// If we have already tried 3 times, exit
if retry > 2 {
return api, errors.New("received three 429 errors with 30 second pauses between attempts")
}
// Increment the retry counter and sleep for 30 seconds
retry++
time.Sleep(30 * time.Second)
// Retry the API call
api, err = p.httpSetup(action, apiURL, body, async, headers)
}
// Return once response code isn't 429
return api, err
}
// cleanFQDN cleans up the provided PCE FQDN in case of common errors
func (p *PCE) cleanFQDN() string {
// Remove trailing slash if included
p.FQDN = strings.TrimSuffix(p.FQDN, "/")
// Remove HTTPS if included
p.FQDN = strings.TrimPrefix(p.FQDN, "https://")
return p.FQDN
}