-
Notifications
You must be signed in to change notification settings - Fork 8
/
util.go
177 lines (154 loc) · 3.2 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
package ghttp
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"reflect"
"strings"
)
// reqtimeout
type reqtimeout interface {
Timeout() bool
}
// Error
type Error struct {
timeout bool
Err error
}
// compression
type compression struct {
writer func(buffer io.Writer) (io.WriteCloser, error)
reader func(buffer io.Reader) (io.ReadCloser, error)
ContentEncoding string
}
// Timeout
func (e *Error) Timeout() bool {
return e.timeout
}
// Error
func (e *Error) Error() string {
return e.Err.Error()
}
// addHeaders
func (r Request) addHeaders(headersMap http.Header) {
if len(r.UserAgent) > 0 {
headersMap.Add("User-Agent", r.UserAgent)
}
if r.Accept != "" {
headersMap.Add("Accept", r.Accept)
}
if r.ContentType != "" {
headersMap.Add("Content-Type", r.ContentType)
}
if r.XForwardedFor != "" {
headersMap.Add("X-Forwarded-For", r.XForwardedFor)
}
}
// Return value if nonempty, def otherwise.
func valueOrDefault(value, def string) string {
if value != "" {
return value
}
return def
}
// paramParse
func paramParse(query interface{}) (string, error) {
switch query.(type) {
case url.Values:
return query.(url.Values).Encode(), nil
case *url.Values:
return query.(*url.Values).Encode(), nil
default:
var (
v = &url.Values{}
)
err := paramParseStruct(v, query)
return v.Encode(), err
}
}
// paramParseStruct
func paramParseStruct(v *url.Values, query interface{}) error {
var (
va = reflect.ValueOf(query)
t = reflect.TypeOf(query)
)
for t.Kind() == reflect.Ptr || t.Kind() == reflect.Interface {
va = va.Elem()
t = va.Type()
}
if t.Kind() != reflect.Struct {
return errors.New("Can't parse Query.")
}
for i := 0; i < t.NumField(); i++ {
var (
name string
)
field := va.Field(i)
typeField := t.Field(i)
if !field.CanInterface() {
continue
}
urlTag := typeField.Tag.Get("json")
if urlTag == "-" {
continue
}
name, opts := parseTag(urlTag)
var (
omitEmpty, squash bool
)
omitEmpty = opts.Contains("omitempty")
squash = opts.Contains("squash")
if squash {
err := paramParseStruct(v, field.Interface())
if err != nil {
return err
}
continue
}
if urlTag == "" {
name = strings.ToLower(typeField.Name)
}
if val := fmt.Sprintf("%v", field.Interface()); !(omitEmpty && len(val) == 0) {
v.Add(name, val)
}
}
return nil
}
func prepareRequestBody(b interface{}) (io.Reader, error) {
switch b.(type) {
case string:
return strings.NewReader(b.(string)), nil
case io.Reader:
return b.(io.Reader), nil
case []byte:
return bytes.NewReader(b.([]byte)), nil
case url.Values:
return strings.NewReader(b.(url.Values).Encode()), nil
case *url.Values:
return strings.NewReader(b.(*url.Values).Encode()), nil
case nil:
return nil, nil
default:
j, err := json.Marshal(b)
if err == nil {
return bytes.NewReader(j), nil
}
return nil, err
}
}
// GetTlsConfig 生成tls config
func GetTlsConfig(cerPath, keyPath string) *tls.Config {
// get certs
certs, err := tls.LoadX509KeyPair(cerPath, keyPath)
if err != nil {
return &tls.Config{InsecureSkipVerify: true}
}
return &tls.Config{
Certificates: []tls.Certificate{certs},
}
}