-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
request.go
87 lines (68 loc) · 1.33 KB
/
request.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
package rek
import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
func buildRequest(method, endpoint string, opts *options) (*http.Request, error) {
var body io.Reader
var contentType string
var req *http.Request
var err error
if opts.data != nil {
data, err := getData(opts)
if err != nil {
return nil, err
}
body = data
}
if opts.jsonObj != nil {
js, err := getJson(opts)
if err != nil {
return nil, err
}
body = js
}
if opts.file != nil {
b, ct, err := buildMultipartBody(opts)
if err != nil {
return nil, err
}
contentType = ct
body = b
}
if opts.formData != nil {
form := url.Values{}
for k, v := range opts.formData {
form.Set(k, v)
}
body = strings.NewReader(form.Encode())
}
if opts.ctx != nil {
req, err = http.NewRequestWithContext(opts.ctx, method, endpoint, body)
if err != nil {
return nil, err
}
} else {
req, err = http.NewRequest(method, endpoint, body)
if err != nil {
return nil, err
}
}
setHeaders(req, opts)
if opts.file != nil {
req.Header.Set("Content-Type", contentType)
}
if opts.bearer != "" {
bearerHeader := fmt.Sprintf("Bearer %s", opts.bearer)
req.Header.Set("Authorization", bearerHeader)
}
setBasicAuth(req, opts)
setCookies(req, opts)
if opts.reqModifier != nil {
opts.reqModifier(req)
}
return req, nil
}