-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatatype.go
327 lines (289 loc) · 8.48 KB
/
datatype.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
package direwolf
import (
"net/http"
"net/url"
"sort"
"strings"
jsoniter "github.com/json-iterator/go"
)
// RequestOption is the interface of Request Options. Use to bind the options to Request.
type RequestOption interface {
// Bind Options to Request
bindRequest(request *Request) error
}
// Body is the data you want to post, one of the Request Options.
type Body []byte
// RequestOption interface method, bind request option to request.
func (options Body) bindRequest(request *Request) error {
request.Body = options
return nil
}
// JsonBody is the json data you want to post.
type JsonBody []byte
// NewJsonBody new a json type body.
func NewJsonBody(v interface{}) JsonBody {
body, err := jsoniter.Marshal(v)
if err != nil {
return nil
}
return body
}
// RequestOption interface method, bind request option to request.
func (options JsonBody) bindRequest(request *Request) error {
request.Body = options
return nil
}
// RedirectNum is the number of request redirect allowed.
// If RedirectNum > 0, it means a redirect number limit for requests.
// If RedirectNum <= 0, it means ban redirect.
// If RedirectNum is not set, it means default 5 times redirect limit.
type RedirectNum int
// RequestOption interface method, bind request option to request.
func (options RedirectNum) bindRequest(request *Request) error {
request.RedirectNum = int(options)
return nil
}
// Timeout is the number of time to timeout request.
// if timeout > 0, it means a time limit for requests.
// if timeout < 0, it means no limit.
// if timeout = 0, it means keep default 30 second timeout.
type Timeout int
// RequestOption interface method, bind request option to request.
func (options Timeout) bindRequest(request *Request) error {
request.Timeout = int(options)
return nil
}
// Proxy is the proxy server address, like "http://127.0.0.1:1080".
// You can set different proxies for HTTP and HTTPS sites.
type Proxy struct {
HTTP string
HTTPS string
}
// RequestOption interface method, bind request option to request.
func (options *Proxy) bindRequest(request *Request) error {
request.Proxy = options
return nil
}
// strSliceMap type is map[string][]string, used for Params, PostForm.
type strSliceMap struct {
data map[string][]string
}
// New is the way to create a strSliceMap.
// You can set key-value pair when you init it by sent params. Just like this:
// stringSliceMap{}.New(
// "key1", "value1",
// "key2", "value2",
// )
// But be careful, between the key and value is a comma.
// And if the number of parameters is not a multiple of 2, it will panic.
func (ssm *strSliceMap) New(keyValue ...string) {
ssm.data = make(map[string][]string)
if keyValue != nil {
if len(keyValue)%2 != 0 {
panic("key and value must be pair")
}
for i := 0; i < len(keyValue)/2; i++ {
key := keyValue[i*2]
value := keyValue[i*2+1]
ssm.data[key] = append(ssm.data[key], value)
}
}
}
// Add key and value to stringSliceMap.
// If key exists, value will append to slice.
func (ssm *strSliceMap) Add(key, value string) {
ssm.data[key] = append(ssm.data[key], value)
}
// Set key and value to stringSliceMap.
// If key exists, existed value will drop and new value will set.
func (ssm *strSliceMap) Set(key, value string) {
ssm.data[key] = []string{value}
}
// Del delete the given key.
func (ssm *strSliceMap) Del(key string) {
delete(ssm.data, key)
}
// Get get the value pair to given key.
// You can pass index to assign which value to get, when there are multiple values.
func (ssm *strSliceMap) Get(key string, index ...int) string {
if ssm.data == nil {
return ""
}
ssmValue := ssm.data[key]
if len(ssmValue) == 0 {
return ""
}
if index != nil {
return ssmValue[index[0]]
}
return ssmValue[0]
}
// URLEncode encodes the values into ``URL encoded'' form
// ("bar=baz&foo=qux") sorted by key.
func (ssm *strSliceMap) URLEncode() string {
if ssm.data == nil {
return ""
}
var buf strings.Builder
keys := make([]string, 0, len(ssm.data))
for k := range ssm.data {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
ssmValue := ssm.data[k]
keyEscaped := url.QueryEscape(k)
for _, v := range ssmValue {
if buf.Len() > 0 {
buf.WriteByte('&')
}
buf.WriteString(keyEscaped)
buf.WriteByte('=')
buf.WriteString(url.QueryEscape(v))
}
}
return buf.String()
}
// Params is url params you want to join to url, as parameter in Request method.
// You should init it by using NewParams like this:
// params := dw.NewParams(
// "key1", "value1",
// "key2", "value2",
// )
// Note: mid symbol is comma.
type Params struct {
strSliceMap
}
// NewParams new a Params type.
//
// You can set key-value pair when you init it by sent parameters. Just like this:
// params := NewParams(
// "key1", "value1",
// "key2", "value2",
// )
// But be careful, between the key and value is a comma.
// And if the number of parameters is not a multiple of 2, it will panic.
func NewParams(keyValue ...string) *Params {
var p = &Params{}
p.New(keyValue...)
return p
}
// RequestOption interface method, bind request option to request.
func (options *Params) bindRequest(request *Request) error {
request.Params = options
u, err := url.Parse(request.URL)
if err != nil {
return WrapErrf(err, "URL error")
}
// check whether parameters is existed in url.
if u.RawQuery == "" && u.ForceQuery == false {
request.URL = request.URL + "?" + request.Params.URLEncode()
} else if u.RawQuery == "" && u.ForceQuery == true {
request.URL = request.URL + request.Params.URLEncode()
} else {
request.URL = request.URL + "&" + request.Params.URLEncode()
}
return nil
}
// PostForm is the form you want to post, as parameter in Request method.
// You should init it by using NewPostForm like this:
// postForm := dw.NewPostForm(
// "key1", "value1",
// "key2", "value2",
// )
// Note: mid symbol is comma.
type PostForm struct {
strSliceMap
}
// NewPostForm new a PostForm type.
//
// You can set key-value pair when you init it by sent parameters. Just like this:
// postForm := NewPostForm(
// "key1", "value1",
// "key2", "value2",
// )
// But be careful, between the key and value is a comma.
// And if the number of parameters is not a multiple of 2, it will panic.
func NewPostForm(keyValue ...string) *PostForm {
var p = &PostForm{}
p.New(keyValue...)
return p
}
// RequestOption interface method, bind request option to request.
func (options *PostForm) bindRequest(request *Request) error {
request.PostForm = options
return nil
}
type Headers struct {
http.Header
}
// RequestOption interface method, bind request option to request.
func (options Headers) bindRequest(request *Request) error {
request.Headers = options.Header
return nil
}
// NewHeaders new a http.Header type.
//
// You can set key-value pair when you init it by sent parameters. Just like this:
// headers := NewHeaders(
// "key1", "value1",
// "key2", "value2",
// )
// But be careful, between the key and value is a comma.
// And if the number of parameters is not a multiple of 2, it will panic.
func NewHeaders(keyValue ...string) *Headers {
h := new(Headers)
h.Header = http.Header{}
if keyValue != nil {
if len(keyValue)%2 != 0 {
panic("key and value must be part")
}
for i := 0; i < len(keyValue)/2; i++ {
key := keyValue[i*2]
value := keyValue[i*2+1]
h.Add(key, value)
}
}
return h
}
// Cookies is request cookies, as parameter in Request method.
// You should init it by using NewCookies like this:
// cookies := dw.NewCookies(
// "key1", "value1",
// "key2", "value2",
// )
// Note: mid symbol is comma.
type Cookies []*http.Cookie
// NewCookies new a Cookies type.
//
// You can set key-value pair when you init it by sent parameters. Just like this:
// cookies := NewCookies(
// "key1", "value1",
// "key2", "value2",
// )
// But be careful, between the key and value is a comma.
// And if the number of parameters is not a multiple of 2, it will panic.
func NewCookies(keyValue ...string) Cookies {
c := make(Cookies, 0)
if keyValue != nil {
if len(keyValue)%2 != 0 {
panic("key and value must be part")
}
for i := 0; i < len(keyValue)/2; i++ {
key := keyValue[i*2]
value := keyValue[i*2+1]
cookie := &http.Cookie{Name: key, Value: value}
c = append(c, cookie)
}
}
return c
}
// RequestOption interface method, bind request option to request.
func (c Cookies) bindRequest(request *Request) error {
request.Cookies = c
return nil
}
// Add append a new cookie to Cookies.
func (c Cookies) Add(key, value string) {
c = append(c, &http.Cookie{Name: key, Value: value})
}