-
Notifications
You must be signed in to change notification settings - Fork 0
/
opt.go
85 lines (73 loc) · 1.42 KB
/
opt.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
package nw
import (
"io"
"net/http"
"time"
)
type nwOption struct {
dataKeys []string
codeKeys []string
msgKeys []string
site string
client *http.Client
header map[string][]string
postReader io.Reader
log bool
}
type NwOption func(*nwOption)
func WithClient(client *http.Client) NwOption {
return func(o *nwOption) {
o.client = client
}
}
func WithLog(uselog bool) NwOption {
return func(o *nwOption) {
o.log = uselog
}
}
func WithPostData(reader io.Reader) NwOption {
return func(o *nwOption) {
o.postReader = reader
}
}
func WithHead(header map[string][]string) NwOption {
return func(o *nwOption) {
o.header = header
}
}
func WithSite(site string) NwOption {
return func(o *nwOption) {
o.site = site
}
}
func WithCodeKeys(keys ...string) NwOption {
return func(o *nwOption) {
o.codeKeys = keys
}
}
func WithMsgKeys(keys ...string) NwOption {
return func(o *nwOption) {
o.msgKeys = keys
}
}
func WithDataKeys(keys ...string) NwOption {
return func(o *nwOption) {
o.dataKeys = keys
}
}
func getDefaultOption(opts ...NwOption) *nwOption {
var o = &nwOption{
codeKeys: []string{"c", "Code", "code", "errcode"},
msgKeys: []string{"m", "Msg", "msg", "Message", "message", "errmsg"},
dataKeys: []string{"d", "Data", "data", ""},
}
for _, opt := range opts {
opt(o)
}
if o.client == nil {
o.client = &http.Client{
Timeout: 10 * time.Second,
}
}
return o
}