-
Notifications
You must be signed in to change notification settings - Fork 10
/
client.go
321 lines (288 loc) · 8.54 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package jpush
import (
"errors"
"fmt"
"net/http"
"strings"
"github.com/levigross/grequests"
)
var (
PushHost = "https://api.jpush.cn/v3"
DeviceHost = "https://device.jpush.cn/v3"
)
type JpushClient struct {
*grequests.Session
host string
appKey string
appSecret string
}
func NewJpushClient(appKey, appSecret string) *JpushClient {
return &JpushClient{
host: PushHost,
appKey: appKey,
appSecret: appSecret,
Session: grequests.NewSession(&grequests.RequestOptions{
UserAgent: "go-jpush/v0.1.2",
Auth: []string{appKey, appSecret},
Headers: map[string]string{
"Accept": "application/json",
},
}),
}
}
func New(appKey, appSecret string) *JpushClient {
return NewJpushClient(appKey, appSecret)
}
func (j *JpushClient) Url(path string) string {
return j.host + path
}
// cid 是用于防止 api 调用端重试造成服务端的重复推送而定义的一个推送参数。
// 用户使用一个 cid 推送后,再次使用相同的 cid 进行推送,则会直接返回第一次成功推送的结果,不会再次进行推送。
// count 不传则默认为1。范围为[1, 1000]
// type: 取值:push(默认), schedule
func (j *JpushClient) PushCid(count int, typ string) (cidList []string, err error) {
if count == 0 {
count = 1
}
if typ == "" {
typ = "push"
}
params := map[string]string{
"count": fmt.Sprint(count),
"type": typ,
}
var out struct {
CidList []string `json:"cidlist"`
}
err = j.Do(http.MethodGet, "/push/cid", params, &out)
return out.CidList, err
}
// 该 API 只用于验证推送调用是否能够成功,与推送 API 的区别在于:不向用户发送任何消息
func (j *JpushClient) PushValidate(push *Payload) (msgId string, err error) {
var out struct {
SendNo string `json:"sendno"`
MsgId string `json:"msg_id"`
}
err = j.Do(http.MethodPost, "/push/validate", push, &out)
return out.MsgId, err
}
// 向某单个设备或者某设备列表推送一条通知、或者消息。
// 推送的内容只能是 JSON 表示的一个推送对象。
func (j *JpushClient) Push(push *Payload) (msgId string, err error) {
var out struct {
SendNo string `json:"sendno"`
MsgId string `json:"msg_id"`
}
err = j.Do(http.MethodPost, "/push", push, &out)
return out.MsgId, err
}
// 创建计划任务
func (j *JpushClient) ScheduleCreate(push *SchedulePayload) (scheduleId string, err error) {
var out struct {
ScheduleId string `json:"schedule_id"`
Name string `json:"name"`
}
err = j.Do(http.MethodPost, "/schedules", push, &out)
if err != nil {
return "", err
}
return out.ScheduleId, nil
}
// 修改指定的计划任务
// 更新操作可为 "name","enabled"、"trigger"或"push" 四项中的一项或多项。
// 不支持部分更新, 需要更新一整块
func (j *JpushClient) ScheduleUpdate(push *SchedulePayload) (out *SchedulePayload, err error) {
err = j.Do(http.MethodPut, "/schedules", push, &out)
return
}
// 删除制定的计划任务
func (j *JpushClient) ScheduleDelete(scheduleId string) error {
return j.Do(http.MethodDelete, "/schedules/"+scheduleId, nil, nil)
}
// 获取有效的计划任务列表
func (j *JpushClient) ScheduleList(pageNo int) (list []SchedulePayload, err error) {
var out struct {
TotalCount int `json:"total_count"`
TotalPages int `json:"total_pages"`
Page int `json:"page"`
Schedules []SchedulePayload `json:"schedules"`
}
err = j.Do(http.MethodGet, "/schedules?page="+fmt.Sprint(pageNo), nil, &out)
return out.Schedules, err
}
// 获取指定的计划任务
func (j *JpushClient) ScheduleGet(scheduleId string) (schedule SchedulePayload, err error) {
err = j.Do(http.MethodGet, "/schedules/"+scheduleId, nil, &schedule)
return
}
// 获取定时任务对应的所有 msg_id
func (j *JpushClient) ScheduleGetMsgIds(scheduleId string) (msgIds []string, err error) {
var out struct {
MsgIds []struct {
MsgId string `json:"msg_id"`
} `json:"msgids"`
}
err = j.Do(http.MethodGet, "/schedules/"+scheduleId+"/msg_ids", nil, &out)
if err != nil {
return nil, err
}
for _, item := range out.MsgIds {
if item.MsgId != "" {
msgIds = append(msgIds, item.MsgId)
}
}
return
}
type DeviceInfo struct {
Tags []string `json:"tags"`
Alias string `json:"alias"`
Mobile string `json:"mobile"`
}
// 查询设备的别名与标签
func (j *JpushClient) DeviceGet(regId string) (info DeviceInfo, err error) {
err = j.Do(http.MethodGet, DeviceHost+"/devices/"+regId, nil, &info)
return
}
type TagSet struct {
Add []string `json:"add,omitempty"`
Remove []string `json:"remove,omitempty"`
}
// tags: 支持add, remove 或者空字符串。当tags参数为空字符串的时候,表示清空所有的 tags;add/remove 下是增加或删除指定的 tag;
// 一次 add/remove tag 的上限均为 100 个,且总长度均不能超过 1000 字节。
// 可以多次调用 API 设置,一个注册 id tag 上限为1000个,应用 tag 总数没有限制
type DeviceUpdateSet struct {
Tags interface{} `json:"tags,omitempty"`
Alias string `json:"alias,omitempty"`
Mobile string `json:"mobile,omitempty"`
}
// 设置设备的别名与标签
func (j *JpushClient) DeviceSet(regId string, setInfo *DeviceUpdateSet) error {
return j.Do(http.MethodPost, DeviceHost+"/devices/"+regId, setInfo, nil)
}
// 查询别名
func (j *JpushClient) AliasGet(alias string) (regIds []string, err error) {
var out struct {
RegistrationIds []string `json:"registration_ids"`
}
err = j.Do(http.MethodGet, "/aliases/"+alias, nil, &out)
if err != nil {
return nil, err
}
return out.RegistrationIds, nil
}
// 删除别名
func (j *JpushClient) AliasDelete(alias string) error {
return j.Do(http.MethodDelete, "/aliases/"+alias, nil, nil)
}
// 查询标签列表
func (j *JpushClient) TagList() (tags []string, err error) {
var out struct {
Tags []string `json:"tags"`
}
err = j.Do(http.MethodGet, "/tags/", nil, &out)
if err != nil {
return nil, err
}
return out.Tags, nil
}
// 判断设备与标签绑定关系
func (j *JpushClient) IsTag(regId, tagId string) (ok bool, err error) {
path := fmt.Sprintf("/tags/%s/registration_ids/%s", regId, tagId)
var out struct {
Result bool `json:"result"`
}
err = j.Do(http.MethodGet, path, nil, &out)
return out.Result, err
}
type RegistrationIdSet struct {
Add []string `json:"add,omitempty"`
Remove []string `json:"remove,omitempty"`
}
type TagUpdateSet struct {
RegistrationIds RegistrationIdSet `json:"registration_ids"`
}
// 更新标签
func (j *JpushClient) TagUpdate(tag string, set *TagUpdateSet) (err error) {
return j.Do(http.MethodPost, "/tags/"+tag, set, nil)
}
// 删除标签
func (j *JpushClient) TagDelete(tag string) error {
return j.Do(http.MethodDelete, "/tags/"+tag, nil, nil)
}
func (j *JpushClient) Do(method, path string, inp, out interface{}) error {
var resp *grequests.Response
var err error
url := j.Url(path)
if strings.HasPrefix(path, "http") {
url = path
}
if method == http.MethodGet {
var params = make(map[string]string)
if val, ok := inp.(map[string]string); ok {
params = val
}
resp, err = j.Get(url, &grequests.RequestOptions{
Params: params,
})
} else if method == http.MethodPost {
if val, ok := inp.(map[string]string); ok {
resp, err = j.Post(url, &grequests.RequestOptions{
Data: val,
})
} else if inp != nil {
resp, err = j.Post(url, &grequests.RequestOptions{
JSON: inp,
})
} else {
resp, err = j.Post(url, nil)
}
} else if method == http.MethodPut {
if val, ok := inp.(map[string]string); ok {
resp, err = j.Put(url, &grequests.RequestOptions{
Data: val,
})
} else if inp != nil {
resp, err = j.Put(url, &grequests.RequestOptions{
JSON: inp,
})
} else {
resp, err = j.Put(url, nil)
}
} else if method == http.MethodDelete {
if val, ok := inp.(map[string]string); ok {
resp, err = j.Delete(url, &grequests.RequestOptions{
Data: val,
})
} else if inp != nil {
resp, err = j.Delete(url, &grequests.RequestOptions{
JSON: inp,
})
} else {
resp, err = j.Delete(url, nil)
}
}
if err != nil {
return err
}
// println(resp.String(), resp.StatusCode)
if resp.StatusCode >= 300 || resp.StatusCode < 200 {
if strings.Contains(resp.String(), "error") {
var er struct {
Error struct {
Code int `json:"code"`
Message string `json:"message"`
} `json:"error"`
}
err = resp.JSON(&er)
if err != nil {
return err
}
return fmt.Errorf("%d - %s", er.Error.Code, er.Error.Message)
}
return errors.New(resp.RawResponse.Status)
}
if out != nil {
return resp.JSON(&out)
}
return nil
}