-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_to_batch.go
46 lines (37 loc) · 1.36 KB
/
push_to_batch.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
package getui
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
)
// PushToBatcher 一次推送多个单推消息
type PushToBatcher interface {
// PushToBatchSingle 将多个单推消息一次推送
PushToBatchSingle(PushToBatchSingleParam) ([]byte, error)
// PushToBatchSingleContext 批量单推,并携带上下文
PushToBatchSingleContext(context.Context, PushToBatchSingleParam) ([]byte, error)
}
// PushToBatchSingleParam 批量单推参数
// 将一批推送个人信息合并在一个接口,一次请求进行推送
type PushToBatchSingleParam struct {
MsgList []PushToSingleParam `json:"msg_list"`
NeedDetail bool `json:"need_detail,omitempty"`
}
// PushToBatchSingle 批量单推
func (g *Getui) PushToBatchSingle(p PushToBatchSingleParam) ([]byte, error) {
return g.PushToBatchSingleContext(context.Background(), p)
}
// PushToBatchSingleContext 批量单推,并携带上下文
func (g *Getui) PushToBatchSingleContext(ctx context.Context, p PushToBatchSingleParam) ([]byte, error) {
if err := g.RefreshTokenContext(ctx); nil != err {
return nil, err
}
url := fmt.Sprintf(`%s/%s/push_single_batch`, APIServer, g.AppID)
body, err := json.Marshal(p)
if nil != err {
return nil, errors.Wrap(err, "encode push_single_batch param to json bytes")
}
return SendContext(ctx, url, g.Token, bytes.NewBuffer(body))
}