This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
137 lines (110 loc) · 2.73 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Package ding 钉钉机器人 API 实现
// https://developers.dingtalk.com/document/app/custom-robot-access
package ding
import (
"bytes"
"context"
"encoding/json"
"errors"
"net/http"
"strconv"
"sync"
"time"
)
func defaultNow() string {
timestamp := time.Now().UnixNano() / 1e6
return strconv.FormatInt(timestamp, 10)
}
const webhook = "https://oapi.dingtalk.com/robot/send"
var defaultHttpClient = &http.Client{Timeout: time.Second * 5}
// New 创建新的实例,hmkey 为 HMAC key 如没有可以不填
func New(token string, hmkey ...string) Ding {
var secret string
if len(hmkey) > 0 {
secret = hmkey[0]
}
return &clientimpl{
api: webhook,
tokens: []AccessToken{{Token: token, Key: secret}},
client: defaultHttpClient,
now: defaultNow,
}
}
// Multi 创建包含访问令牌的实例,每次请求轮换使用下一个访问令牌,防止限流造成的发送失败
func Multi(tokens []AccessToken) Ding {
impl := &clientimpl{
api: webhook,
tokens: tokens,
now: defaultNow,
client: defaultHttpClient,
}
return impl
}
type clientimpl struct {
client *http.Client
now func() string // 获取当前时间(毫秒)
api string
tokens []AccessToken
index int // current access tokens index
mux sync.Mutex
}
func (d *clientimpl) request(ctx context.Context, reqdata map[string]interface{}) error {
if GetSilenceMode() {
return nil
}
var reqbuf = bytes.NewBuffer(nil)
if err := json.NewEncoder(reqbuf).Encode(reqdata); err != nil {
return err
}
// 需要 1.13 以上版本支持
req, err := http.NewRequestWithContext(ctx, http.MethodPost, d.api, reqbuf)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
q := req.URL.Query()
accessToken, ok := d.nextAccessToken()
if !ok {
return errors.New("no access token")
}
q.Set("access_token", accessToken.Token)
if accessToken.Key != "" {
timestamp := d.now()
q.Set("timestamp", timestamp)
q.Set("sign", getsign(accessToken.Key, timestamp))
}
req.URL.RawQuery = q.Encode()
resp, err := d.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
var respdata struct {
Code int `json:"errcode"`
Msg string `json:"errmsg"`
}
if err := json.NewDecoder(resp.Body).Decode(&respdata); err != nil {
return err
}
if respdata.Code != 0 {
return errors.New(respdata.Msg)
}
return nil
}
// nextAccessToken 获取下一个可用的访问令牌
func (d *clientimpl) nextAccessToken() (AccessToken, bool) {
if len(d.tokens) == 0 {
return AccessToken{}, false
}
if len(d.tokens) == 1 {
return d.tokens[0], true
}
d.mux.Lock()
defer d.mux.Unlock()
if i := d.index; i < len(d.tokens) {
d.index++
return d.tokens[i], true
}
d.index = 1
return d.tokens[0], true
}