-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_request.go
367 lines (313 loc) · 8.82 KB
/
api_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
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
package keycloak
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"strconv"
"strings"
"sync/atomic"
)
const (
uriPathParameterSearchFormat = "{%s}"
uriQueryParameterFormat = "%s?%s"
uriQueryParameterAddNoValueFormat = "%s%s&"
uriQueryParameterAddValueFormat = "%s%s=%s&"
uriQueryParameterCutSet = "&"
apiRequestURLFormat = "%s%s"
headerKeyContentType = "Content-Type"
headerKeyContentDisposition = "Content-Disposition"
headerKeyAccept = "Accept"
headerValueApplicationJSON = "application/json"
headerValueMultipartFormData = "multipart/form-data"
)
var apiRequestID uint64
type APIRequest struct {
id uint64
method string
uri string
queryParameters url.Values
pathParameters map[string]string
headers url.Values
cookies []*http.Cookie
body io.Reader
bodyType string
bodyLen int
mpw *multipart.Writer
}
func NewAPIRequest(method, requestURL string) *APIRequest {
r := &APIRequest{
id: atomic.AddUint64(&apiRequestID, 1),
method: method,
uri: requestURL,
queryParameters: make(url.Values),
pathParameters: make(map[string]string),
headers: make(url.Values),
cookies: make([]*http.Cookie, 0),
}
return r
}
func (r *APIRequest) ID() uint64 {
return r.id
}
func (r *APIRequest) Method() string {
return r.method
}
func (r *APIRequest) URI() string {
return r.uri
}
func (r *APIRequest) AddHeader(name, value string) {
r.headers.Add(name, value)
}
func (r *APIRequest) SetHeader(name, value string) {
r.headers.Set(name, value)
}
func (r *APIRequest) SetHeaders(headers url.Values) {
r.headers = headers
}
func (r *APIRequest) RemoveHeader(name string) {
r.headers.Del(name)
}
func (r *APIRequest) Headers() url.Values {
return r.headers
}
func (r *APIRequest) SetCookies(cookies []*http.Cookie) {
r.cookies = cookies
}
func (r *APIRequest) AddCookie(cookie *http.Cookie) {
r.cookies = append(r.cookies, cookie)
}
func (r *APIRequest) SetCookie(cookie *http.Cookie) {
for i, cc := range r.cookies {
if cc.Name == cookie.Name {
r.cookies[i] = cookie
return
}
}
r.cookies = append(r.cookies, cookie)
}
func (r *APIRequest) RemoveCookie(name string) {
nc := make([]*http.Cookie, 0)
for _, cc := range r.cookies {
if cc.Name != name {
nc = append(nc, cc)
}
}
r.cookies = nc
}
func (r *APIRequest) Cookies() []*http.Cookie {
return r.cookies
}
// AddQueryParameter will add a value to the specified param
func (r *APIRequest) AddQueryParameter(param string, value string) {
r.queryParameters.Add(param, value)
}
// SetQueryParameter will set a query param to a specific value, overriding any previously set value
func (r *APIRequest) SetQueryParameter(param string, value string) {
r.queryParameters.Set(param, value)
}
// SetQueryParameters will override any / all existing query parameters
func (r *APIRequest) SetQueryParameters(params url.Values) {
r.queryParameters = params
}
// RemoveQueryParameter will attempt to delete all values for a specific query parameter from this request.
func (r *APIRequest) RemoveQueryParameter(param string) {
delete(r.queryParameters, param)
}
// QueryParameters will return all values of currently set query parameters
func (r *APIRequest) QueryParameters() url.Values {
return r.queryParameters
}
// SetPathParameter will define a path parameter value, overriding any existing value
func (r *APIRequest) SetPathParameter(param, value string) {
r.pathParameters[param] = value
}
// SetPathParameters will re-define all path parameters, overriding any / all existing ones
func (r *APIRequest) SetPathParameters(params map[string]string) {
r.pathParameters = make(map[string]string)
for k, v := range params {
r.SetPathParameter(k, v)
}
}
// RemovePathParameter will attempt to remove a single parameter from the current list of path parameters
func (r *APIRequest) RemovePathParameter(param string) {
delete(r.pathParameters, param)
}
func (r *APIRequest) PathParameters() map[string]string {
return r.pathParameters
}
func (r *APIRequest) SetBody(body interface{}) error {
// if provided body is nil, move tf on!
if body == nil {
r.bodyType = "nil"
r.body = nil
return nil
}
// set body type string
r.bodyType = fmt.Sprintf("%T", body)
// test for reader
if ar, ok := body.(io.Reader); ok {
r.body = ar
return nil
}
// test for raw bytes
if b, ok := body.([]byte); ok {
r.body = bytes.NewBuffer(b)
r.bodyLen = len(b)
return nil
}
// test for form data
if v, ok := body.(url.Values); ok {
enc := v.Encode()
r.bodyLen = len(enc)
r.body = bytes.NewBufferString(enc)
return nil
}
// finally, attempt json marshal
b, err := json.Marshal(body)
if err != nil {
return err
}
r.bodyLen = len(b)
r.body = bytes.NewBuffer(b)
return nil
}
func (r *APIRequest) Body() io.Reader {
return r.body
}
func (r *APIRequest) BodyType() string {
return r.bodyType
}
func (r *APIRequest) BodyLen() int {
return r.bodyLen
}
func (r *APIRequest) MultipartForm() {
r.body = new(bytes.Buffer)
r.mpw = multipart.NewWriter(r.body.(*bytes.Buffer))
}
func (r *APIRequest) AddMultipartFile(key, filename string, f io.Reader) error {
w, err := r.mpw.CreateFormFile(key, filename)
if err != nil {
return fmt.Errorf("error creating multipart form file part with key=%q and filename=%q: %w", key, filename, err)
}
if _, err = io.Copy(w, f); err != nil {
return fmt.Errorf("error copying bytes from file %q to multipart writer: %w", filename, err)
}
addContentDispositionHeader(r, key, filename)
return nil
}
func (r *APIRequest) AddMultipartField(key string, value interface{}) error {
var (
vr io.Reader
ok bool
)
w, err := r.mpw.CreateFormField(key)
if err != nil {
return fmt.Errorf("error creating form field part with key=%q: %w", key, err)
}
vr, ok = value.(io.Reader)
if !ok {
if b, ok := value.([]byte); ok {
vr = bytes.NewBuffer(b)
} else if s, ok := value.(string); ok {
vr = bytes.NewBufferString(s)
} else if b, err := json.Marshal(value); err != nil {
return fmt.Errorf("error marshalling form field part with key=%q and type=%T: %w", key, value, err)
} else {
vr = bytes.NewBuffer(b)
}
}
if _, err = io.Copy(w, vr); err != nil {
return fmt.Errorf("error copying bytes into multipart writer for key=%q with type=%T: %w", key, value, err)
}
return nil
}
func (r *APIRequest) AddMultipartFieldsFromValues(values url.Values) error {
for k, vs := range values {
for _, v := range vs {
if err := r.AddMultipartField(k, v); err != nil {
return err
}
}
}
return nil
}
// CompiledURI will return to you the full request URI, not including scheme, hostname, and port. This method is not
// thread safe, as you shouldn't be calling this asynchronously anyway.
func (r *APIRequest) CompiledURI() string {
pathParams := r.PathParameters()
queryParams := r.QueryParameters()
uri := r.uri
if len(pathParams) > 0 {
for k, v := range pathParams {
uri = strings.Replace(uri, fmt.Sprintf(uriPathParameterSearchFormat, k), url.PathEscape(v), 1)
}
}
// TODO: could probably be made more efficient.
if len(queryParams) > 0 {
uri = fmt.Sprintf(uriQueryParameterFormat, uri, queryParams.Encode())
}
return uri
}
// ToHTTP will attempt to construct an executable http.request
func (r *APIRequest) ToHTTP(ctx context.Context) (*http.Request, error) {
var (
httpRequest *http.Request
err error
compiledURL = r.CompiledURI()
)
if r.mpw != nil {
r.SetHeader(headerKeyContentType, r.mpw.FormDataContentType())
if err = r.mpw.Close(); err != nil {
return nil, fmt.Errorf("error closing multipart writer: %w", err)
}
}
if httpRequest, err = http.NewRequestWithContext(ctx, r.method, compiledURL, r.Body()); err != nil {
return nil, err
}
for header, values := range r.headers {
for _, value := range values {
httpRequest.Header.Add(header, value)
}
}
return httpRequest, nil
}
//
//func (r *APIRequest) MarshalZerologObject(ev *zerolog.Event) {
// ev.Uint64("request_id", r.InstallDocument())
// ev.Str("method", r.Method())
// ev.Str("uri", r.URI())
// ev.Str("compiled_uri", r.CompiledURI())
// ev.Str("body_type", r.BodyType())
// tmp := make([]string, 0)
// for k := range r.Headers() {
// tmp = append(tmp, k)
// }
// ev.Strs("header_keys", tmp)
// tmp = make([]string, 0)
// for k := range r.QueryParameters() {
// tmp = append(tmp, k)
// }
// ev.Strs("query_keys", tmp)
// tmp = make([]string, 0)
// for k := range r.PathParameters() {
// tmp = append(tmp, k)
// }
// ev.Strs("path_keys", tmp)
// ev.Int("cookies", len(r.Cookies()))
// ev.Bool("is_multipart", r.mpw != nil)
//}
func addContentDispositionHeader(req *APIRequest, key, filename string) {
req.AddHeader(
headerKeyContentDisposition,
fmt.Sprintf(
"form-data: name=%s; filename=%s;",
strconv.Quote(key),
strconv.Quote(filename),
),
)
}