-
Notifications
You must be signed in to change notification settings - Fork 50
/
request.go
229 lines (172 loc) · 4.86 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
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
package http
import (
"io"
"net/url"
"sync"
httpHeaders "github.com/go-http-utils/headers"
"github.com/go-resty/resty/v2"
"github.com/google/go-querystring/query"
"github.com/hashicorp/go-multierror"
"github.com/spf13/cast"
)
type Request struct {
errs error
outputFile *string
queryParams url.Values
restyRequest *resty.Request
url *url.URL
}
var r struct {
instance *resty.Client
lck sync.Mutex
}
// NewRequest or client.NewRequest() creates a request, don't create the object inline!
func NewRequest(client Client) *Request {
r.lck.Lock()
defer r.lck.Unlock()
if r.instance == nil {
r.instance = resty.New()
}
if client == nil {
return &Request{
restyRequest: r.instance.NewRequest(),
url: &url.URL{},
queryParams: url.Values{},
}
}
return client.NewRequest()
}
// NewJsonRequest creates a request that already contains the application/json content-type, don't create the object inline!
func NewJsonRequest(client Client) *Request {
return NewRequest(client).
WithHeader(httpHeaders.Accept, MimeTypeApplicationJson)
}
// NewXmlRequest creates a request that already contains the application/xml content-type, don't create the object inline!
func NewXmlRequest(client Client) *Request {
return NewRequest(client).
WithHeader(httpHeaders.Accept, MimeTypeApplicationXml)
}
func (r *Request) WithUrl(rawUrl string) *Request {
parsedUrl, err := url.Parse(rawUrl)
if err != nil {
r.errs = multierror.Append(r.errs, err)
return r
}
r.url = parsedUrl
if len(r.queryParams) != 0 {
r.takeQueryParamsFromUrl()
}
return r
}
func (r *Request) WithQueryParam(key string, values ...interface{}) *Request {
r.takeQueryParamsFromUrl()
for _, value := range values {
str, err := cast.ToStringE(value)
if err != nil {
r.errs = multierror.Append(r.errs, err)
continue
}
r.queryParams.Add(key, str)
}
return r
}
func (r *Request) WithQueryObject(obj interface{}) *Request {
parts, err := query.Values(obj)
if err != nil {
r.errs = multierror.Append(r.errs, err)
return r
}
r.takeQueryParamsFromUrl()
r.addQueryValues(parts)
return r
}
func (r *Request) WithQueryMap(values interface{}) *Request {
parts, err := cast.ToStringMapStringSliceE(values)
if err != nil {
r.errs = multierror.Append(r.errs, err)
return r
}
r.takeQueryParamsFromUrl()
r.addQueryValues(parts)
return r
}
func (r *Request) WithBasicAuth(username string, password string) *Request {
r.restyRequest.SetBasicAuth(username, password)
return r
}
func (r *Request) WithAuthToken(token string) *Request {
r.restyRequest.SetAuthToken(token)
return r
}
func (r *Request) WithHeader(key string, value string) *Request {
r.restyRequest.SetHeader(key, value)
return r
}
func (r *Request) WithBody(body interface{}) *Request {
r.restyRequest.SetBody(body)
return r
}
func (r *Request) WithMultipartFile(param, fileName string, reader io.Reader) *Request {
r.restyRequest.SetFileReader(param, fileName, reader)
return r
}
func (r *Request) WithMultipartFormData(params url.Values) *Request {
r.restyRequest.SetFormDataFromValues(params)
return r
}
func (r *Request) WithOutputFile(path string) *Request {
r.outputFile = &path
return r
}
// The following methods are mainly intended for tests
// You should not need to call them yourself
type Header map[string][]string
func (r *Request) GetHeader() Header {
header := make(Header)
// make a copy of our headers to prevent a caller
// from modifying them
for key, values := range r.restyRequest.Header {
header[key] = append(make([]string, 0, len(values)), values...)
}
return header
}
func (r *Request) GetBody() interface{} {
return r.restyRequest.Body
}
func (r *Request) GetToken() string {
return r.restyRequest.Token
}
func (r *Request) GetUrl() string {
if len(r.queryParams) != 0 {
r.url.RawQuery = r.queryParams.Encode()
}
return r.url.String()
}
func (r *Request) GetError() error {
return r.errs
}
func (r *Request) build() (*resty.Request, string, error) {
if r.errs != nil {
return nil, "", r.errs
}
return r.restyRequest, r.GetUrl(), nil
}
func (r *Request) addQueryValues(parts url.Values) {
for key, values := range parts {
for _, value := range values {
r.queryParams.Add(key, value)
}
}
}
// takeQueryParamsFromUrl parses the query parameters in the url object (if any) and merges them into the queryParams object
// We need this because we keep query params at two places: Either directly (without decoding and re-encoding) in the url
// object or in the queryParams object (after adding some manually). This allows you to specify urls with a specific encoding
// if you need to influence which characters are percent-encoded (because of a non-behaving service on the other side you
// can't change).
func (r *Request) takeQueryParamsFromUrl() {
if r.url.RawQuery == "" {
return
}
r.addQueryValues(r.url.Query())
r.url.RawQuery = ""
}