-
Notifications
You must be signed in to change notification settings - Fork 105
/
util.go
191 lines (162 loc) · 3.51 KB
/
util.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
// Copyright 2014-2019 Liu Dong <ddliuhb@gmail.com>.
// Licensed under the MIT license.
package httpclient
import (
"bytes"
"io"
"mime/multipart"
"net/url"
"os"
"path/filepath"
"strings"
)
// Add params to a url string.
func addParams(url_ string, params url.Values) string {
if len(params) == 0 {
return url_
}
if !strings.Contains(url_, "?") {
url_ += "?"
}
if strings.HasSuffix(url_, "?") || strings.HasSuffix(url_, "&") {
url_ += params.Encode()
} else {
url_ += "&" + params.Encode()
}
return url_
}
// Add a file to a multipart writer.
func addFormFile(writer *multipart.Writer, name, path string) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
part, err := writer.CreateFormFile(name, filepath.Base(path))
if err != nil {
return err
}
_, err = io.Copy(part, file)
return err
}
// Convert options with string keys to desired format.
func Option(o map[string]interface{}) map[int]interface{} {
rst := make(map[int]interface{})
for k, v := range o {
k := "OPT_" + strings.ToUpper(k)
if num, ok := CONST[k]; ok {
rst[num] = v
}
}
return rst
}
// Merge options(latter ones have higher priority)
func mergeOptions(options ...map[int]interface{}) map[int]interface{} {
rst := make(map[int]interface{})
for _, m := range options {
for k, v := range m {
rst[k] = v
}
}
return rst
}
// Merge headers(latter ones have higher priority)
func mergeHeaders(headers ...map[string]string) map[string]string {
rst := make(map[string]string)
for _, m := range headers {
for k, v := range m {
rst[k] = v
}
}
return rst
}
// Does the params contain a file?
func checkParamFile(params url.Values) bool {
for k, _ := range params {
if k[0] == '@' {
return true
}
}
return false
}
// Is opt in options?
func hasOption(opt int, options []int) bool {
for _, v := range options {
if opt == v {
return true
}
}
return false
}
// Map is a mixed structure with options and headers
type Map map[interface{}]interface{}
// Parse the Map, return options and headers
func parseMap(m Map) (map[int]interface{}, map[string]string) {
var options = make(map[int]interface{})
var headers = make(map[string]string)
if m == nil {
return options, headers
}
for k, v := range m {
// integer is option
if kInt, ok := k.(int); ok {
// don't need to validate
options[kInt] = v
} else if kString, ok := k.(string); ok {
kStringUpper := strings.ToUpper(kString)
if kInt, ok := CONST[kStringUpper]; ok {
options[kInt] = v
} else {
// it should be header, but we still need to validate it's type
if vString, ok := v.(string); ok {
headers[kString] = vString
}
}
}
}
return options, headers
}
func toUrlValues(v interface{}) url.Values {
switch t := v.(type) {
case url.Values:
return t
case map[string][]string:
return url.Values(t)
case map[string]string:
rst := make(url.Values)
for k, v := range t {
rst.Add(k, v)
}
return rst
case nil:
return make(url.Values)
default:
panic("Invalid value")
}
}
func checkParamsType(v interface{}) int {
switch v.(type) {
case url.Values, map[string][]string, map[string]string:
return 1
case []byte, string, *bytes.Reader:
return 2
case nil:
return 0
default:
return 3
}
}
func toReader(v interface{}) *bytes.Reader {
switch t := v.(type) {
case []byte:
return bytes.NewReader(t)
case string:
return bytes.NewReader([]byte(t))
case *bytes.Reader:
return t
case nil:
return bytes.NewReader(nil)
default:
panic("Invalid value")
}
}