/
requests.go
435 lines (379 loc) · 10.7 KB
/
requests.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package requests
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"text/template"
"time"
)
// URLEncode will encode `data` as a URL-safe string
// Uses fmt.Sprint and url.PathEscape to do the encoding.
func URLEncode(data interface{}) string {
return url.PathEscape(fmt.Sprint(data))
}
// makeTemplate creates a new Go template instance
// pre-loaded with the URLEncode function.
func makeTemplate() *template.Template {
return template.New("url").Funcs(template.FuncMap{
"URLEncode": URLEncode,
})
}
// JSONMust marshals a map into a JSON byte slice
// using json.Marshal and panics if there is an error.
func JSONMust(data map[string]interface{}) []byte {
res, err := json.Marshal(data)
if err != nil {
panic(err)
}
return res
}
// HTTPMethod is a type that represents an
// HTTP request method.
// Read more here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
type HTTPMethod int
// Enums representing HTTP methods
const (
GET HTTPMethod = iota // An HTTP GET method
POST // An HTTP POST method
PUT // An HTTP PUT method
DELETE // An HTTP DELETE method
OPTIONS // An HTTP OPTIONS method
HEAD // An HTTP HEAD method
CONNECT // An HTTP CONNECT method
TRACE // An HTTP TRACE method
PATCH // An HTTP PATCH method
)
// Convert an HTTPMethod to it's string format
func (m HTTPMethod) String() string {
switch m {
case GET:
return "GET"
case POST:
return "POST"
case PUT:
return "PUT"
case DELETE:
return "DELETE"
case OPTIONS:
return "OPTIONS"
case HEAD:
return "HEAD"
case CONNECT:
return "CONNECT"
case TRACE:
return "TRACE"
case PATCH:
return "PATCH"
}
return ""
}
// Request is a type that represents an HTTP request
//
// Notes:
// - Headers and Query dont support multiple values
// - Timeout of 0 means no timeout
type Request struct {
URL string // URL to send the request to
Method HTTPMethod // HTTP method to use
Headers map[string]string // Headers to send with the request
Query map[string]string // Query parameters to send with the request
Body []byte // Body to send with the request
Timeout time.Duration // Timeout for the request
}
// NewGetRequest creates a new Request object
// with the supplied URL and sets the HTTP method
// to GET.
func NewGetRequest(url string) *Request {
return &Request{
URL: url,
Method: GET,
}
}
// SendGetRequest creates a new HTTP GET request
// and sends it to the specified URL.
// Internally, calls `NewGetRequest(url).Send()`
func SendGetRequest(url string) (*Response, error) {
return NewGetRequest(url).Send()
}
// NewPostRequest creates a new Request object
// with the supplied URL, content-type header, and body sets the HTTP method
// to POST.
func NewPostRequest(url string, contentType string, body []byte) *Request {
return &Request{
URL: url,
Method: POST,
Headers: map[string]string{"content-type": contentType},
Body: body,
}
}
// SendPostRequest creates a new HTTP POST request
// and sends it to the specified URL.
// Internally, calls `NewPostRequest(url, contentType, body).Send()`
func SendPostRequest(url string, contentType string, body []byte) (*Response, error) {
return NewPostRequest(url, contentType, body).Send()
}
// Copy will create a copy of the Request object
func (req *Request) Copy() *Request {
r := Request{
URL: req.URL,
Method: req.Method,
Timeout: req.Timeout,
}
if req.Headers != nil {
r.Headers = make(map[string]string)
for k, v := range req.Headers {
r.Headers[k] = v
}
}
if req.Query != nil {
r.Query = make(map[string]string)
for k, v := range req.Query {
r.Query[k] = v
}
}
if req.Body != nil {
r.Body = make([]byte, len(req.Body))
copy(r.Body, req.Body)
}
return &r
}
// getReqBody returns the request body as a buffer that can be
// passed to the http.NewRequest function
func (req *Request) getReqBody() *bytes.Buffer {
return bytes.NewBuffer(req.Body)
}
// getURL returns the string formatted URL with
// the query parameters
func (req *Request) getURL() (string, error) {
// Make sure there's a URL
if req.URL == "" {
return "", fmt.Errorf("URL is required")
}
// Create a URL object from the supplied string URL
u, err := url.Parse(req.URL)
if err != nil {
return "", err
}
// Encode the query parameters (if any)
vals := url.Values{}
for k, v := range req.Query {
vals.Set(k, v)
}
q := vals.Encode()
u.RawQuery = q
return u.String(), nil
}
// formatPath will use Go templates to format the path
// using the data parameter.
func (req *Request) formatPath(data interface{}) (string, error) {
tmpl, err := makeTemplate().Parse(req.URL)
if err != nil {
return "", err
}
buf := bytes.Buffer{}
err = tmpl.Execute(&buf, data)
if err != nil {
return "", err
}
return buf.String(), nil
}
// ParsePathParams will create a copy of the Request object
// and replace URL parameters with the supplied data.
//
// Note: The URL template has access to the `URLEncode` function
// which can be used to safely encode a string. (ex `{{ "Hello world" | URLEncode }}`
// will return `Hello%20world`)
func (req *Request) ParsePathParams(data interface{}) (*Request, error) {
u, err := req.formatPath(data)
if err != nil {
return nil, err
}
r := req.Copy()
r.URL = u
return r, nil
}
// MustParsePathParams is the same as ParsePathParams except it panics
// if there is an error.
func (req *Request) MustParsePathParams(data interface{}) *Request {
r, err := req.ParsePathParams(data)
if err != nil {
panic(err)
}
return r
}
// GetHeader gets a header value from the request. Normalizes the key
// to lowercase before checking. Returns the value associated with the
// key and whether it exists.
func (req *Request) GetHeader(name string) (string, bool) {
// Create the map if it doesn't exist
if req.Headers == nil {
req.Headers = make(map[string]string)
}
// Normalize the key (convert to lowercase)
key := strings.ToLower(name)
// Do a case-insensitive check for the key
for k, v := range req.Headers {
if strings.ToLower(k) == key {
return v, true
}
}
// Key not found
return "", false
}
// SetHeader sets a header value in the request. Normalizes the key
// to lowercase before setting.
func (req *Request) SetHeader(name, value string) {
// Create the map if it doesn't exist
if req.Headers == nil {
req.Headers = make(map[string]string)
}
// Normalize the key (convert to lowercase)
key := strings.ToLower(name)
// Add the header to the Headers map
req.Headers[key] = value
}
// DelHeader deletes a header value from the request headers
// if it exists. Normalizes the key to lowercase
// before deleting.
func (req *Request) DelHeader(name string) {
// Create the map if it doesn't exist
if req.Headers == nil {
req.Headers = make(map[string]string)
}
// Normalize the key (convert to lowercase)
key := strings.ToLower(name)
for k := range req.Headers {
if strings.ToLower(k) == key {
// Delete the header if it exists
delete(req.Headers, k)
}
}
}
// GetQuery gets a query value from the request. Returns the
// value associated with the key and whether it exists.
func (req *Request) GetQuery(name string) (string, bool) {
// Create the map if it doesn't exist
if req.Query == nil {
req.Query = make(map[string]string)
}
val, ok := req.Query[name]
return val, ok
}
// SetQuery sets a header value in the request.
func (req *Request) SetQuery(name, value string) {
// Create the map if it doesn't exist
if req.Query == nil {
req.Query = make(map[string]string)
}
// Set the Query param if it exists
req.Query[name] = value
}
// DelQuery deletes a query value from the request headers
// if it exists.
func (req *Request) DelQuery(name string) {
// Create the map if it doesn't exist
if req.Query == nil {
req.Query = make(map[string]string)
}
// Delete the query param if it exists
delete(req.Query, name)
}
// Send sends the HTTP request with the supplied parameters
func (req *Request) Send() (*Response, error) {
// Create an http client (with optional timeout)
client := http.Client{
Timeout: req.Timeout,
}
// Format the URL with the query parameters (if any)
u, err := req.getURL()
if err != nil {
return nil, err
}
// Create the underlying request
httpRequest, err := http.NewRequest(req.Method.String(), u, req.getReqBody())
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}
// Set the headers in the underlying request
for k, v := range req.Headers {
httpRequest.Header.Set(k, v)
}
// Make the reuquest
httpResponse, err := client.Do(httpRequest)
if err != nil {
return nil, err
}
// Add return headers
rHeaders := make(map[string]string)
for k, v := range httpResponse.Header {
if len(v) > 0 {
lowerKey := strings.ToLower(k)
rHeaders[lowerKey] = v[0]
}
}
// Load the request body
defer httpResponse.Body.Close()
body, err := ioutil.ReadAll(httpResponse.Body)
if err != nil {
return nil, err
}
// Format the response & return
res := Response{
Ok: httpResponse.StatusCode < 400,
StatusCode: httpResponse.StatusCode,
Headers: rHeaders,
Body: body,
}
return &res, nil
}
// MustSend sends the HTTP request and panic if an error
// is returned. (Calls Send() internally)
func (req *Request) MustSend() *Response {
res, err := req.Send()
if err != nil {
panic(err)
}
return res
}
// Response is a type that represents an HTTP response
// returned from an HTTP request
type Response struct {
Ok bool // Was the request successful? (Status codes: 100-399)
StatusCode int // HTTP response status code
Headers map[string]string // HTTP Response headers
Body []byte // HTTP Response body
}
// GetHeader gets a header value from the response if it exists.
// Normalizes the key to lowercase before checking.
// Returns the value of the header and whether it exists.
func (res *Response) GetHeader(name string) (string, bool) {
// Create the map if it doesn't exist
if res.Headers == nil {
res.Headers = make(map[string]string)
}
// Normalize the key (convert to lowercase)
key := strings.ToLower(name)
// Do a case-insensitive check for the key
for k, v := range res.Headers {
if strings.ToLower(k) == key {
return v, true
}
}
// Return not found
return "", false
}
// JSON unmarshalls the response body into a map
func (res *Response) JSON() (map[string]interface{}, error) {
// Create a new map to store the JSON data
data := make(map[string]interface{})
// Unmarshal the JSON data
err := json.Unmarshal(res.Body, &data)
if err != nil {
return nil, err
}
return data, nil
}