forked from ddliu/go-httpclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
153 lines (125 loc) · 3.35 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
// Copyright 2014-2015 Liu Dong <ddliuhb@gmail.com>.
// Licensed under the MIT license.
package httpclient
import (
"os"
"io"
"path/filepath"
"net/url"
"mime/multipart"
"strings"
)
// Convert string map to url component.
func paramsToString(params map[string]string) string {
values := url.Values{}
for k, v := range(params) {
values.Set(k, v)
}
return values.Encode()
}
// Add params to a url string.
func addParams(url_ string, params map[string]string) string {
if len(params) == 0 {
return url_
}
if !strings.Contains(url_, "?") {
url_ += "?"
}
if strings.HasSuffix(url_, "?") || strings.HasSuffix(url_, "&") {
url_ += paramsToString(params)
} else {
url_ += "&" + paramsToString(params)
}
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 map[string]string) 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
}