-
Notifications
You must be signed in to change notification settings - Fork 7
/
client.go
122 lines (103 loc) · 2.32 KB
/
client.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
package alimns
import (
"context"
"crypto/md5"
"encoding/base64"
"io"
"time"
"go.uber.org/zap"
"github.com/sirupsen/logrus"
"github.com/xiaojiaoyu100/cast"
)
// Client 存储了阿里云的相关信息
type Client struct {
config Config
before Before
after After
codec Codec
ca *cast.Cast
logger *zap.Logger
}
// NewClient 返回Client的实例
func NewClient(config Config) (*Client, error) {
logger, err := NewLogger()
if err != nil {
return nil, err
}
c, err := cast.New(
cast.WithHTTPClientTimeout(40*time.Second),
cast.WithBaseURL(config.Endpoint),
cast.AddRequestHook(withAuth(config.AccessKeyID, config.AccessKeySecret)),
cast.WithRetry(3),
cast.WithConstantBackoffStrategy(time.Millisecond*100),
cast.WithLogLevel(logrus.WarnLevel),
)
if err != nil {
return nil, err
}
cli := &Client{
config: config,
ca: c,
logger: logger,
}
cli.defaultCodec()
cli.defaultMakeContext()
cli.defaultClean()
return cli, nil
}
// AddLogHook add a log reporter.
func (c *Client) AddLogHook(f func(entry Entry) error) {
c.logger = c.logger.WithOptions(Hooks(f))
}
// SetBefore 设置环境
func (c *Client) SetBefore(before Before) {
c.before = before
}
// DefaultMakeContextIfNone 保证makeContext不为空
func (c *Client) defaultMakeContext() {
if c.before == nil {
c.before = func(m *M) (context.Context, error) {
return context.TODO(), nil
}
}
}
// defaultCodec 保证codec不为空,默认是json编解码
func (c *Client) defaultCodec() {
if c.codec == nil {
c.codec = JSONCodec{}
}
}
// EnableDebug enables debug info.
func (c *Client) EnableDebug() {
c.logger.Core().Enabled(zap.DebugLevel)
}
// SetQueuePrefix sets the query param for ListQueue.
func (c *Client) SetQueuePrefix(prefix string) {
c.config.QueuePrefix = prefix
}
// SetAfter 消息队里善后处理函数
func (c *Client) SetAfter(after After) {
c.after = after
}
func (c *Client) defaultClean() {
if c.after == nil {
c.after = func(ctx context.Context) {}
}
}
// Base64Md5 md5值用base64编码
func Base64Md5(s string) (string, error) {
h, err := Md5(s)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(h), nil
}
// Md5 md5
func Md5(s string) ([]byte, error) {
hash := md5.New()
_, err := io.WriteString(hash, s)
if err != nil {
return nil, err
}
return hash.Sum(nil), nil
}