-
Notifications
You must be signed in to change notification settings - Fork 3
/
client_options.go
132 lines (113 loc) · 3.32 KB
/
client_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
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
package reqKit
import (
"github.com/imroc/req/v3"
"github.com/richelieu-yang/chimera/v3/src/core/errorKit"
"github.com/richelieu-yang/chimera/v3/src/log/zapKit"
"time"
)
const (
// DefaultTimeout 默认超时时间
DefaultTimeout = time.Second * 30
)
type (
clientOptions struct {
Dev bool
BaseURL string
// Timeout 超时时间(发送请求的整个周期,includes connection time, any redirects, and reading the response body)
Timeout time.Duration
// InsecureSkipVerify
/*
true: 跳过证书验证
false: 不跳过证书验证(默认; 更加安全)
*/
InsecureSkipVerify bool
// Logger 日志输出
/*
nil: 不输出
*/
Logger req.Logger
/* 下面两者一般搭配起来用,参考: https://req.cool/zh/docs/prologue/quickstart/#%E6%9B%B4%E9%AB%98%E7%BA%A7%E7%9A%84-get-%E8%AF%B7%E6%B1%82 */
CommonErrorResult interface{}
OnAfterResponse req.ResponseMiddleware
}
ClientOption func(*clientOptions)
)
func loadOptions(options ...ClientOption) *clientOptions {
logger := zapKit.NewLogger(nil).Sugar()
opts := &clientOptions{
Dev: false,
BaseURL: "",
Timeout: 0, // 默认值在下面
InsecureSkipVerify: true,
// imroc/req默认: 输出到 os.Stdout
Logger: logger,
CommonErrorResult: nil,
OnAfterResponse: nil, // 默认值在下面
}
for _, option := range options {
option(opts)
}
/* 默认值s */
if opts.Timeout <= 0 {
opts.Timeout = DefaultTimeout
}
if opts.OnAfterResponse == nil {
opts.OnAfterResponse = func(client *req.Client, resp *req.Response) error {
if resp.Err != nil { // There is an underlying error, e.g. network error or unmarshal error.
return nil
}
//if errMsg, ok := resp.ErrorResult().(*ErrorMessage); ok {
// resp.Err = errMsg // Convert api error into go error
// return nil
//}
/* 处理不成功的http状态码 */
if !resp.IsSuccessState() {
// Neither a success response nor a error response, record details to help troubleshooting
//resp.Err = fmt.Errorf("bad status: %s\nraw content:\n%s", resp.Status, resp.Dump())
bodyStr, err := resp.ToString()
if err != nil {
resp.Err = errorKit.Newf("bad status: %s, fail to get body string: %s", resp.Status, err.Error())
} else {
resp.Err = errorKit.Newf("bad status: %s, body string: %s", resp.Status, bodyStr)
}
}
return nil
}
}
return opts
}
func WithDev() ClientOption {
return func(options *clientOptions) {
options.Dev = true
}
}
func WithTimeout(timeout time.Duration) ClientOption {
return func(options *clientOptions) {
options.Timeout = timeout
}
}
func WithInsecureSkipVerify(insecureSkipVerify bool) ClientOption {
return func(options *clientOptions) {
options.InsecureSkipVerify = insecureSkipVerify
}
}
// WithLogger
/*
@param logger (1) 可以是 *zap.SugaredLogger 实例 || *logrus.Logger 实例
(2) 可以为nil(disable log, 禁用输出)
*/
func WithLogger(logger req.Logger) ClientOption {
return func(options *clientOptions) {
options.Logger = logger
}
}
func WithCommonErrorResult(result interface{}) ClientOption {
return func(options *clientOptions) {
options.CommonErrorResult = result
}
}
func WithOnAfterResponse(middleware req.ResponseMiddleware) ClientOption {
return func(options *clientOptions) {
options.OnAfterResponse = middleware
}
}