-
Notifications
You must be signed in to change notification settings - Fork 16
/
pushclient.go
176 lines (144 loc) · 4.89 KB
/
pushclient.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package jpush
import (
"encoding/json"
"errors"
"strconv"
"strings"
"time"
)
type JPushClient struct {
AppKey string // app key
MasterSecret string // master secret
}
const (
SUCCESS_FLAG = "msg_id"
SMS = "https://api.sms.jpush.cn/v1/messages"
HOST_PUSH = "https://api.jpush.cn/v3/push"
HOST_SCHEDULE = "https://api.jpush.cn/v3/schedules"
HOST_REPORT = "https://report.jpush.cn/v3/received"
HOST_CID = "https://api.jpush.cn/v3/push/cid"
HOST_IMAGES = "https://api.jpush.cn/v3/images"
)
// NewJPushClient returns a new JPushClient
func NewJPushClient(appKey string, masterSecret string) *JPushClient {
return &JPushClient{AppKey: appKey, MasterSecret: masterSecret}
}
// GetAuthorization returns the authorization string
func (j *JPushClient) GetAuthorization() string {
return j.AppKey + ":" + j.MasterSecret
}
// GetCid returns the cid list as byte array
func (j *JPushClient) GetCid(count int, push_type string) ([]byte, error) {
req := Get(HOST_CID)
req.SetTimeout(DEFAULT_CONNECT_TIMEOUT*time.Second, DEFAULT_READ_WRITE_TIMEOUT*time.Second)
req.SetHeader("Connection", "Keep-Alive")
req.SetHeader("Charset", CHARSET)
req.SetBasicAuth(j.AppKey, j.MasterSecret)
req.SetHeader("Content-Type", CONTENT_TYPE_JSON)
req.SetProtocolVersion("HTTP/1.1")
req.SetQueryParam("count", strconv.Itoa(count))
req.SetQueryParam("type", push_type)
return req.Bytes()
}
// 发送短信
func (j *JPushClient) SendSms(data []byte) (string, error) {
return j.sendSmsBytes(data)
}
func (j *JPushClient) sendSmsBytes(content []byte) (string, error) {
ret, err := SendPostBytes2(SMS, content, j.AppKey, j.MasterSecret)
if err != nil {
return "", err
}
if strings.Contains(ret, SUCCESS_FLAG) {
return ret, nil
}
return "", errors.New(ret)
}
// Push 推送消息
func (j *JPushClient) Push(data []byte) (string, error) {
return j.sendPushBytes(data)
}
// CreateSchedule 创建推送计划
func (j *JPushClient) CreateSchedule(data []byte) (string, error) {
return j.sendScheduleBytes(data)
}
// DeleteSchedule 删除推送计划
func (j *JPushClient) DeleteSchedule(id string) (string, error) {
return j.sendDeleteScheduleRequest(id)
}
// GetSchedule 获取推送计划
func (j *JPushClient) GetSchedule(id string) (string, error) {
return j.sendGetScheduleRequest(id)
}
// SendPushString sends a push request and returns the response body as string
func (j *JPushClient) sendPushString(content string) (string, error) {
ret, err := SendPostString(HOST_PUSH, content, j.AppKey, j.MasterSecret)
if err != nil {
return "", err
}
if strings.Contains(ret, SUCCESS_FLAG) {
return ret, nil
}
return "", errors.New(ret)
}
// SendPushBytes sends a push request and returns the response body as string
func (j *JPushClient) sendPushBytes(content []byte) (string, error) {
ret, err := SendPostBytes2(HOST_PUSH, content, j.AppKey, j.MasterSecret)
if err != nil {
return "", err
}
if strings.Contains(ret, SUCCESS_FLAG) {
return ret, nil
}
return "", errors.New(ret)
}
// SendScheduleBytes sends a schedule request and returns the response body as string
func (j *JPushClient) sendScheduleBytes(content []byte) (string, error) {
ret, err := SendPostBytes2(HOST_SCHEDULE, content, j.AppKey, j.MasterSecret)
if err != nil {
return "", err
}
if strings.Contains(ret, "schedule_id") {
return ret, nil
}
return "", errors.New(ret)
}
// SendGetScheduleRequest sends a get schedule request and returns the response body as string
func (j *JPushClient) sendGetScheduleRequest(schedule_id string) (string, error) {
req := Get(HOST_SCHEDULE)
req.SetTimeout(DEFAULT_CONNECT_TIMEOUT*time.Second, DEFAULT_READ_WRITE_TIMEOUT*time.Second)
req.SetHeader("Connection", "Keep-Alive")
req.SetHeader("Charset", CHARSET)
req.SetBasicAuth(j.AppKey, j.MasterSecret)
req.SetHeader("Content-Type", CONTENT_TYPE_JSON)
req.SetProtocolVersion("HTTP/1.1")
req.SetQueryParam("schedule_id", schedule_id)
return req.String()
}
// SendDeleteScheduleRequest sends a delete schedule request and returns the response body as string
func (j *JPushClient) sendDeleteScheduleRequest(schedule_id string) (string, error) {
req := Delete(HOST_SCHEDULE)
req.SetTimeout(DEFAULT_CONNECT_TIMEOUT*time.Second, DEFAULT_READ_WRITE_TIMEOUT*time.Second)
req.SetHeader("Connection", "Keep-Alive")
req.SetHeader("Charset", CHARSET)
req.SetBasicAuth(j.AppKey, j.MasterSecret)
req.SetHeader("Content-Type", CONTENT_TYPE_JSON)
req.SetProtocolVersion("HTTP/1.1")
req.SetQueryParam("schedule_id", schedule_id)
return req.String()
}
// UnmarshalResponse unmarshals the response body to the map
func UnmarshalResponse(resp string) (map[string]interface{}, error) {
var ret map[string]interface{}
if len(strings.TrimSpace(resp)) == 0 {
return ret, errors.New("empty response")
}
err := json.Unmarshal([]byte(resp), &ret)
if err != nil {
return nil, err
}
if _, ok := ret["error"]; ok {
return nil, errors.New(resp)
}
return ret, nil
}