-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathoptions.go
63 lines (54 loc) · 1.31 KB
/
options.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
package opentaobao
import "net/http"
// Options 可选参数列表
type Options struct {
Router string // 环境请求地址
AppKey string // 应用Key
AppSecret string // 秘密
JSONSimplify bool // 是否开启JSON返回值的简化模式
httpClient *http.Client
}
// Option 为可选参数赋值的函数
type Option func(*Options)
// NewOptions 创建可选参数
func NewOptions(opts ...Option) Options {
opt := Options{
Router: "http://gw.api.taobao.com/router/rest",
JSONSimplify: true,
httpClient: &http.Client{},
}
for _, o := range opts {
o(&opt)
}
return opt
}
// WithRouter 设置环境请求地址
func WithRouter(router string) Option {
return func(o *Options) {
o.Router = router
}
}
// WithAppKey 设置应用Key
func WithAppKey(appKey string) Option {
return func(o *Options) {
o.AppKey = appKey
}
}
// WithAppSecret 设置秘密
func WithAppSecret(appSecret string) Option {
return func(o *Options) {
o.AppSecret = appSecret
}
}
// WithJSONSimplify 设置是否开启JSON返回值的简化模式
func WithJSONSimplify(jsonSimplify bool) Option {
return func(o *Options) {
o.JSONSimplify = jsonSimplify
}
}
// WithHTTPClient 设置HTTP客户端
func WithHTTPClient(httpClient *http.Client) Option {
return func(o *Options) {
o.httpClient = httpClient
}
}