-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_request.go
340 lines (288 loc) · 7.57 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
package bigdog
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"sync/atomic"
)
type APISource int
const (
APISourceUnknown APISource = iota
APISourceVSZ
APISourceSCI
)
func (s APISource) String() string {
switch s {
case APISourceVSZ:
return "vsz"
case APISourceSCI:
return "sci"
default:
return "UNKNOWN"
}
}
type RequestMutator func(*http.Request)
func ToString(value interface{}) string {
switch value.(type) {
case string:
return value.(string)
case int:
return strconv.Itoa(value.(int))
case bool:
return strconv.FormatBool(value.(bool))
case []string:
return strings.Join(value.([]string), ",")
case []int:
l := len(value.([]int))
if l == 0 {
return ""
}
tmp := make([]string, l, l)
for i, v := range value.([]int) {
tmp[i] = strconv.Itoa(v)
}
return ToString(tmp)
default:
panic(fmt.Sprintf("unable to handle type %T", value))
}
}
type PathValues map[string]string
func (pv PathValues) Set(key string, value interface{}) {
pv[key] = ToString(value)
}
func (pv PathValues) Get(key string) string {
v, _ := pv[key]
return v
}
type QueryValues url.Values
func (qv QueryValues) Values() url.Values {
return url.Values(qv)
}
func (qv QueryValues) Get(key string) string {
return url.Values(qv).Get(key)
}
func (qv QueryValues) Set(key string, value interface{}) {
url.Values(qv).Set(key, ToString(value))
}
func (qv QueryValues) SetStrings(key string, values []string) {
for i, v := range values {
if i == 0 {
qv.Set(key, v)
} else {
qv.Add(key, v)
}
}
}
func (qv QueryValues) Add(key string, value interface{}) {
url.Values(qv).Add(key, ToString(value))
}
func (qv QueryValues) AddStrings(key string, values []string) {
for _, v := range values {
qv.Add(key, v)
}
}
func (qv QueryValues) Del(key string) {
url.Values(qv).Del(key)
}
func (qv QueryValues) Encode() string {
return url.Values(qv).Encode()
}
var apiRequestID uint64
type APIRequest struct {
Source APISource
Method string
URI string
Authenticated bool
QueryParams QueryValues
PathParams PathValues
Header http.Header
id uint64
body interface{}
mpw *multipart.Writer
}
var apiRequestPool = sync.Pool{New: func() interface{} { return new(APIRequest) }}
func bootstrapRequest(src APISource, req *APIRequest, method, uri string, authenticated bool) {
req.Source = src
req.Method = method
req.URI = uri
req.Authenticated = authenticated
req.QueryParams = make(QueryValues)
req.PathParams = make(PathValues)
req.Header = make(http.Header)
req.id = atomic.AddUint64(&apiRequestID, 1)
}
func apiRequestFromPool(src APISource, method, uri string, authenticated bool) *APIRequest {
req := apiRequestPool.Get().(*APIRequest)
bootstrapRequest(src, req, method, uri, authenticated)
return req
}
func recycleAPIRequest(req *APIRequest) {
req.Source = APISourceUnknown
req.Method = ""
req.URI = ""
req.Authenticated = false
req.QueryParams = nil
req.PathParams = nil
req.Header = nil
req.id = 0
req.body = nil
req.mpw = nil
apiRequestPool.Put(req)
}
func NewAPIRequest(src APISource, method, uri string, authenticated bool) *APIRequest {
req := new(APIRequest)
bootstrapRequest(src, req, method, uri, authenticated)
return req
}
func (r *APIRequest) ID() uint64 {
return r.id
}
func (r *APIRequest) SetBody(body interface{}) {
r.body = body
}
func (r *APIRequest) Body() interface{} {
return r.body
}
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)
}
addContentDispositionFormDataHeader(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 {
uri := r.URI
if len(r.PathParams) > 0 {
for k, v := range r.PathParams {
uri = strings.Replace(uri, fmt.Sprintf(uriPathParameterSearchFormat, k), url.PathEscape(v), 1)
}
}
if len(r.QueryParams) > 0 {
uri = fmt.Sprintf(uriQueryParameterPrefixFormat, uri, r.QueryParams.Encode())
}
return uri
}
func (r *APIRequest) bodyReader() (io.Reader, error) {
if r.body == nil {
return nil, nil
}
// localize var
body := r.body
// test for reader
if ar, ok := body.(io.Reader); ok {
return ar, nil
}
// test for raw bytes
if b, ok := body.([]byte); ok {
return bytes.NewBuffer(b), nil
}
// test for form data
if v, ok := body.(url.Values); ok {
return bytes.NewBufferString(v.Encode()), nil
}
// finally, attempt json marshal
if b, err := json.Marshal(body); err != nil {
return nil, fmt.Errorf("error marshalling %T to json: %w", body, err)
} else {
return bytes.NewBuffer(b), nil
}
}
// ToHTTP will attempt to construct an executable http.request
func (r *APIRequest) ToHTTP(ctx context.Context, addr, pathPrefix, authParamName, authParamValue string) (*http.Request, error) {
var (
compiledURL string
bodyReader io.Reader
httpRequest *http.Request
err error
)
if r.Authenticated {
if authParamName == "" || authParamValue == "" {
return nil, errors.New("authParam cannot be empty with authenticated request")
}
r.QueryParams.Set(authParamName, authParamValue)
}
compiledURL = fmt.Sprintf(apiRequestURLFormat, addr, pathPrefix, r.CompiledURI())
if r.mpw != nil {
r.Header.Set(headerKeyContentType, r.mpw.FormDataContentType())
if err = r.mpw.Close(); err != nil {
return nil, fmt.Errorf("error closing multipart writer: %w", err)
}
}
if bodyReader, err = r.bodyReader(); err != nil {
return nil, fmt.Errorf("error creating reader from body: %w", err)
}
if httpRequest, err = http.NewRequest(r.Method, compiledURL, bodyReader); err != nil {
return nil, err
}
for header, values := range r.Header {
for _, value := range values {
httpRequest.Header.Add(header, value)
}
}
return httpRequest.WithContext(ctx), nil
}
func addContentDispositionFormDataHeader(req *APIRequest, key, filename string) {
req.Header.Add(
headerKeyContentDisposition,
fmt.Sprintf(
"form-data: name=%s; filename=%s;",
strconv.Quote(key),
strconv.Quote(filename),
),
)
}