-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
519 lines (442 loc) · 12.3 KB
/
client.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
package reflectclient
import (
"bytes"
"errors"
"fmt"
"golang.org/x/net/websocket"
"io"
"io/ioutil"
"net/http"
"net/url"
"reflect"
"strings"
)
type Service interface{}
type FieldAdder interface {
Add(string, string)
}
type Client struct {
baseUrl string
retryHandler RetryHandler
unmarshaler Unmarshaler
requestTransformers []RequestTransformer
httpClient *http.Client
}
type Builder struct {
baseUrl string
retryHandler RetryHandler
httpClient *http.Client
requestTransformers []RequestTransformer
unmarshaler Unmarshaler
}
type Arg struct {
Name string
OmitEmpty bool
}
func NewBuilder() *Builder {
return &Builder{
requestTransformers: make([]RequestTransformer, 0),
}
}
func (b *Builder) BaseUrl(baseUrl string) *Builder {
b.baseUrl = baseUrl
return b
}
func (b *Builder) AddRequestTransformer(transformer RequestTransformer) *Builder {
b.requestTransformers = append(b.requestTransformers, transformer)
return b
}
func (b *Builder) SetUnmarshaler(unmarshaler Unmarshaler) *Builder {
b.unmarshaler = unmarshaler
return b
}
func (b *Builder) SetRetryHandler(r RetryHandler) *Builder {
b.retryHandler = r
return b
}
func (b *Builder) SetHttpClient(c *http.Client) *Builder {
b.httpClient = c
return b
}
func (b *Builder) Build() (*Client, error) {
return &Client{
b.baseUrl,
b.retryHandler,
b.unmarshaler,
b.requestTransformers,
http.DefaultClient,
}, nil
}
// For validation
var HttpMethods = []string{
"GET",
"POST",
"PUT",
"DELETE",
}
type MethodMeta struct {
returnType reflect.Type
methodArgs []MethodArg
hasBody bool
webSocket bool
path string
method string
origin string
}
func (m *MethodMeta) hasFields() bool {
for _, arg := range m.methodArgs {
if arg.isStruct {
if len(arg.structMeta.formFields) > 0 {
return true
}
}
}
return false
}
type MethodArg struct {
isStruct bool
structMeta *StructMeta
}
type StructMeta struct {
pathFields map[string]*Arg
formFields map[string]*Arg
queryFields map[string]*Arg
headerFields map[string]*Arg
bodyField *Arg
}
type RequestMeta struct {
path string
method string
query url.Values
fields url.Values
headers http.Header
body []byte
}
const (
TagMethod = "rc_method"
TagPath = "rc_path"
TagFeature = "rc_feature"
TagName = "rc_name"
TagOrigin = "rc_origin"
TagOptions = "rc_options"
FeaturePath = "path"
FeatureField = "field"
FeatureQuery = "query"
FeatureHeader = "header"
FeatureBody = "body"
OptionOmitEmpty = "omitempty"
)
func (c *Client) applyRequestTransformers(req *http.Request) *http.Request {
for _, t := range c.requestTransformers {
req = t(req)
}
return req
}
// Initialize the target service
func (c *Client) Init(service Service) error {
serviceValue := reflect.ValueOf(service).Elem()
serviceType := serviceValue.Type()
for fieldIdx := 0; fieldIdx < serviceType.NumField(); fieldIdx++ {
fieldValue := serviceValue.Field(fieldIdx)
fieldStruct := serviceType.Field(fieldIdx)
fieldType := fieldStruct.Type
// If field isn't a Func, ignore it. We can do better checks in the future.
if fieldType.Kind() != reflect.Func {
continue
}
// Construct the MethodMeta
meta := &MethodMeta{
methodArgs: make([]MethodArg, fieldType.NumIn()),
}
if fieldType.NumOut() != 2 {
return errors.New("Functions must return two values")
}
meta.returnType = fieldType.Out(0)
if meta.returnType == reflect.TypeOf((**websocket.Conn)(nil)).Elem() {
meta.webSocket = true
meta.origin = fieldStruct.Tag.Get(TagOrigin)
}
if fieldType.Out(1) != reflect.TypeOf((*error)(nil)).Elem() {
return errors.New("Second return value must be an error.")
}
meta.method = fieldStruct.Tag.Get(TagMethod)
if !in(meta.method, HttpMethods) {
return errors.New("Unsupported method: " + meta.method)
}
// TODO(dforsyth): Warn for WebSockets if method is not GET? Or make WebSocket a method?
meta.path = fieldStruct.Tag.Get(TagPath)
for argIdx := 0; argIdx < fieldType.NumIn(); argIdx++ {
argType := fieldType.In(argIdx)
argValue := elementType(argType)
// TODO: make sure we only accept certain Kinds here. No Methods, etc.
if argValue.Kind() == reflect.Struct {
meta.methodArgs[argIdx].isStruct = true
sm, err := processStructArg(argValue)
if err != nil {
return err
}
if sm.bodyField != nil {
if meta.hasBody {
return errors.New("Only one body per request is supported.")
}
meta.hasBody = true
}
meta.methodArgs[argIdx].structMeta = sm
} else {
meta.methodArgs[argIdx].isStruct = false
}
}
// Check for issues with body and form fields
if meta.hasBody && meta.hasFields() {
return errors.New("Requests cannot have form fields and an explicit body.")
}
if !meta.webSocket {
fieldValue.Set(c.makeRequestFunc(fieldType, meta))
} else {
fieldValue.Set(c.makeWebSocketFunc(fieldType, meta))
}
}
return nil
}
func isEmptyValue(v reflect.Value) bool {
switch v.Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
return v.Len() == 0
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Interface, reflect.Ptr:
return v.IsNil()
}
return false
}
func applyPathFields(value reflect.Value, path string, nameMap map[string]*Arg) string {
for fn, n := range nameMap {
if !value.IsValid() || n.OmitEmpty && isEmptyValue(value) {
continue
}
path = strings.Replace(path, fmt.Sprintf("{%s}", n.Name), extractFieldValue(value, fn), -1)
}
return path
}
func applyPathIndex(value reflect.Value, path string, index int) string {
return strings.Replace(path, fmt.Sprintf("{%d}", index), fmt.Sprint(value.Interface()), -1)
}
func applyAdderFields(value reflect.Value, adder FieldAdder, nameMap map[string]*Arg) {
for fn, n := range nameMap {
if !value.IsValid() || n.OmitEmpty && isEmptyValue(value.FieldByName(fn)) {
continue
}
adder.Add(n.Name, extractFieldValue(value, fn))
}
}
// Unmarshal an HTTP response and return it. If an erro is found, return that instead.
func (c *Client) handleResponse(meta *MethodMeta, resp *http.Response, err error) []reflect.Value {
rvals := []reflect.Value{
reflect.Zero(meta.returnType),
reflect.Zero(reflect.TypeOf((*error)(nil)).Elem()),
}
if err != nil {
rvals[1] = reflect.ValueOf(&err).Elem()
} else if resp != nil {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
rvals[1] = reflect.ValueOf(&err).Elem()
} else {
if c.unmarshaler == nil {
rvals[0] = reflect.ValueOf(body)
} else {
instance := reflect.New(meta.returnType)
if err := c.unmarshaler.Unmarshal(body, instance.Interface()); err != nil {
rvals[1] = reflect.ValueOf(&err).Elem()
} else {
rvals[0] = instance.Elem()
}
}
}
}
return rvals
}
// Handle the tagged fields of a struct and put them into a StructMeta.
func processStructArg(argType reflect.Type) (*StructMeta, error) {
structMeta := &StructMeta{
pathFields: make(map[string]*Arg),
formFields: make(map[string]*Arg),
queryFields: make(map[string]*Arg),
headerFields: make(map[string]*Arg),
}
for i := 0; i < argType.NumField(); i++ {
field := argType.Field(i)
// TODO: Validate only simple Kinds -- no funcs or structs (or maps, for now).
if field.Type.Kind() == reflect.Func {
continue
}
// Only process the field is we find a feature Tag
feature := field.Tag.Get(TagFeature)
if feature == "" {
continue
}
// If we don't find a name, use the Field name
name := field.Tag.Get(TagName)
if name == "" {
name = field.Name
}
arg := &Arg{Name: name}
switch feature {
case FeaturePath:
structMeta.pathFields[field.Name] = arg
case FeatureField:
structMeta.formFields[field.Name] = arg
case FeatureQuery:
structMeta.queryFields[field.Name] = arg
case FeatureHeader:
structMeta.headerFields[field.Name] = arg
case FeatureBody:
if structMeta.bodyField != nil {
return nil, errors.New("Only one body per request is supported.")
}
structMeta.bodyField = arg
default:
println(feature)
continue
}
optTag := field.Tag.Get(TagOptions)
opts := strings.Split(optTag, ",")
for _, opt := range opts {
switch opt {
case OptionOmitEmpty:
arg.OmitEmpty = true
default:
continue
}
}
}
return structMeta, nil
}
// Go through meta and args to build out request info.
func buildRequestMeta(meta *MethodMeta, args []reflect.Value) (*RequestMeta, error) {
rm := &RequestMeta{
path: meta.path,
method: meta.method,
query: url.Values{},
fields: url.Values{},
headers: http.Header{},
}
// Walk arguments, using collected information to build our request
for argIdx, arg := range args {
methodArg := meta.methodArgs[argIdx]
// If we don't have a struct, do a path replace for the index
if !methodArg.isStruct {
rm.path = applyPathIndex(arg, rm.path, argIdx)
} else {
structMeta := methodArg.structMeta
argValue := elementValue(arg)
// update path
rm.path = applyPathFields(argValue, rm.path, structMeta.pathFields)
// collect query values
applyAdderFields(argValue, rm.query, structMeta.queryFields)
// collect form values
applyAdderFields(argValue, rm.fields, structMeta.formFields)
// collect header values
applyAdderFields(argValue, rm.headers, structMeta.headerFields)
// handle a body if the argument provides one
if structMeta.bodyField != nil {
val := argValue.FieldByName(structMeta.bodyField.Name)
if val.IsValid() && !(structMeta.bodyField.OmitEmpty && isEmptyValue(val)) {
rm.body = val.Bytes()
}
}
}
}
if len(rm.fields) > 0 {
if rm.body != nil {
return nil, errors.New("Body and fields are incompatible.")
}
rm.body = []byte(rm.fields.Encode())
}
return rm, nil
}
// Build a function that makes an HTTP request and returns a given type, decoded from
// the body of the response.
func (c *Client) makeRequestFunc(typ reflect.Type, meta *MethodMeta) reflect.Value {
return reflect.MakeFunc(typ, func(args []reflect.Value) []reflect.Value {
rm, err := buildRequestMeta(meta, args)
if err != nil {
return c.handleResponse(meta, nil, err)
}
var bodyReader io.Reader
if rm.body != nil {
bodyReader = bytes.NewBuffer(rm.body)
}
// Once we have the base path and the bodyReader, we can generate the request and update the rest of it.
req, err := http.NewRequest(rm.method, c.baseUrl+rm.path, bodyReader)
if err != nil {
c.handleResponse(meta, nil, err)
}
qu := req.URL.Query()
for qn, ql := range rm.query {
for _, q := range ql {
qu.Add(qn, q)
}
}
req.URL.RawQuery = qu.Encode()
for hn, hl := range rm.headers {
for _, h := range hl {
req.Header.Add(hn, h)
}
}
req = c.applyRequestTransformers(req)
client := c.httpClient
// Make the request
for {
resp, err := client.Do(req)
if err != nil && c.retryHandler != nil {
if err = c.retryHandler.Retry(err); err == nil {
continue
}
}
return c.handleResponse(meta, resp, err)
}
panic("Not reached.")
})
}
// Build a function that connects to a WebSocket and returns a conneciton.
func (c *Client) makeWebSocketFunc(typ reflect.Type, meta *MethodMeta) reflect.Value {
return reflect.MakeFunc(typ, func(args []reflect.Value) []reflect.Value {
// We don't expect the body error.
rm, _ := buildRequestMeta(meta, args)
rvals := []reflect.Value{
reflect.Zero(meta.returnType),
reflect.Zero(reflect.TypeOf((*error)(nil)).Elem()),
}
config, err := websocket.NewConfig(c.baseUrl+rm.path, meta.origin)
if err != nil {
rvals[1] = reflect.ValueOf(&err).Elem()
return rvals
}
qu := config.Location.Query()
for qn, ql := range rm.query {
for _, q := range ql {
qu.Add(qn, q)
}
}
config.Location.RawQuery = qu.Encode()
for hn, hl := range rm.headers {
for _, h := range hl {
config.Header.Add(hn, h)
}
}
conn, err := websocket.DialConfig(config)
if err != nil {
rvals[1] = reflect.ValueOf(&err).Elem()
return rvals
}
rvals[0] = reflect.ValueOf(&conn).Elem()
return rvals
})
}