2024-01-01 14:10:18 +08:00
|
|
|
package curlx
|
|
|
|
|
|
2024-01-21 21:02:05 +08:00
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"log"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
2024-01-01 14:10:18 +08:00
|
|
|
|
|
|
|
|
type clientOptions struct {
|
|
|
|
|
TimeOut time.Duration
|
|
|
|
|
InsecureSkipVerify bool
|
2024-01-21 21:02:05 +08:00
|
|
|
Logger OptionLogger
|
2024-01-01 14:10:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func defaultOptions() clientOptions {
|
|
|
|
|
return clientOptions{
|
2024-01-24 23:27:20 +08:00
|
|
|
TimeOut: time.Second * 120, // 默认超时120
|
|
|
|
|
Logger: defaultLogger{},
|
2024-01-01 14:10:18 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Option func(*clientOptions)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置超时时间
|
|
|
|
|
*/
|
2024-01-12 21:06:13 +08:00
|
|
|
func SetOptionTimeOut(t time.Duration) Option {
|
2024-01-01 14:10:18 +08:00
|
|
|
return func(options *clientOptions) {
|
|
|
|
|
options.TimeOut = t
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 不校验HTTPS证书
|
|
|
|
|
*/
|
2024-01-12 21:06:13 +08:00
|
|
|
func SetOptionTLSInsecureSkipVerify() Option {
|
2024-01-01 14:10:18 +08:00
|
|
|
return func(options *clientOptions) {
|
|
|
|
|
options.InsecureSkipVerify = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-21 21:02:05 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置日志输出
|
|
|
|
|
*/
|
|
|
|
|
func SetOptionLog(log OptionLogger) Option {
|
|
|
|
|
return func(options *clientOptions) {
|
|
|
|
|
options.Logger = log
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type OptionLogger interface {
|
|
|
|
|
Infof(ctx context.Context, format string, args ...interface{})
|
|
|
|
|
Errorf(ctx context.Context, format string, args ...interface{})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type defaultLogger struct{}
|
|
|
|
|
|
|
|
|
|
func (defaultLogger) Errorf(ctx context.Context, format string, args ...interface{}) {
|
|
|
|
|
// 输出日志
|
|
|
|
|
log.Printf(format, args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (defaultLogger) Infof(ctx context.Context, format string, args ...interface{}) {
|
|
|
|
|
// 输出日志
|
|
|
|
|
log.Printf(format, args...)
|
|
|
|
|
}
|