-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptions.go
189 lines (156 loc) · 3.43 KB
/
options.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
package stdsdk
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/url"
"reflect"
"strings"
"time"
)
type Files map[string][]byte
type Headers map[string]string
type Params map[string]interface{}
type Query map[string]interface{}
type RequestOptions struct {
Body io.Reader
Files Files
Headers Headers
Params Params
Query Query
}
func (o *RequestOptions) Querystring() (string, error) {
u, err := marshalValues(o.Query)
if err != nil {
return "", err
}
return u.Encode(), nil
}
func (o *RequestOptions) Content() (io.Reader, string, error) {
if o.Body != nil && len(o.Files) > 0 {
return nil, "", fmt.Errorf("cannot specify both Body and Files")
}
if o.Body != nil && len(o.Params) > 0 {
return nil, "", fmt.Errorf("cannot specify both Body and Params")
}
if o.Body == nil && len(o.Files) == 0 && len(o.Params) == 0 {
return nil, "application/octet-stream", nil
}
if o.Body != nil {
return o.Body, "application/octet-stream", nil
}
uv, err := marshalValues(o.Params)
if err != nil {
return nil, "", err
}
if len(o.Files) > 0 {
var buf bytes.Buffer
w := multipart.NewWriter(&buf)
for name, data := range o.Files {
part, err := w.CreateFormFile(name, "binary-data")
if err != nil {
return nil, "", err
}
if _, err := part.Write(data); err != nil {
return nil, "", err
}
}
for k := range uv {
w.WriteField(k, uv.Get(k))
}
if err := w.Close(); err != nil {
return nil, "", err
}
return &buf, w.FormDataContentType(), nil
}
return bytes.NewReader([]byte(uv.Encode())), "application/x-www-form-urlencoded", nil
}
func MarshalOptions(opts interface{}) (RequestOptions, error) {
ro := RequestOptions{
Headers: Headers{},
Params: Params{},
Query: Query{},
}
v := reflect.ValueOf(opts)
t := v.Type()
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
if n := f.Tag.Get("header"); n != "" {
if u, ok := marshalValue(v.Field(i)); ok {
ro.Headers[n] = u
}
}
if n := f.Tag.Get("param"); n != "" {
if u, ok := marshalValue(v.Field(i)); ok {
ro.Params[n] = u
}
}
if n := f.Tag.Get("query"); n != "" {
if u, ok := marshalValue(v.Field(i)); ok {
ro.Query[n] = u
}
}
}
return ro, nil
}
func marshalValue(f reflect.Value) (string, bool) {
if f.IsNil() {
return "", false
}
v := f.Interface()
if f.Kind() == reflect.Ptr {
v = f.Elem().Interface()
}
switch t := v.(type) {
case bool:
return fmt.Sprintf("%t", t), true
case int, int64:
return fmt.Sprintf("%d", t), true
case string:
return t, true
case time.Duration:
return t.String(), true
case time.Time:
return t.Format("20060102.150405.000000000"), true
case []string:
return strings.Join(t, ","), true
case map[string]string:
uv := url.Values{}
for k, v := range t {
uv.Add(k, v)
}
return uv.Encode(), true
default:
return "", false
}
return "", true
}
func marshalValues(vv map[string]interface{}) (url.Values, error) {
u := url.Values{}
for k, v := range vv {
switch t := v.(type) {
case bool:
u.Set(k, fmt.Sprintf("%t", t))
case int, int64:
u.Set(k, fmt.Sprintf("%d", t))
case string:
u.Set(k, t)
case []string:
for _, s := range t {
u.Add(k, s)
}
case time.Duration:
u.Set(k, t.String())
case map[string]string:
uv := url.Values{}
for kk, vv := range t {
uv.Set(kk, vv)
}
u.Set(k, uv.Encode())
default:
return nil, fmt.Errorf("unknown param type: %T", t)
}
}
return u, nil
}