This repository has been archived by the owner on Oct 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
jpush.go
175 lines (147 loc) · 5.37 KB
/
jpush.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
package jpush
import (
"fmt"
"strconv"
"strings"
"github.com/DeanThompson/jpush-api-go-client/common"
"github.com/DeanThompson/jpush-api-go-client/device"
"github.com/DeanThompson/jpush-api-go-client/httplib"
"github.com/DeanThompson/jpush-api-go-client/push"
"github.com/DeanThompson/jpush-api-go-client/report"
)
// JPush 的 Golang 推送客户端
// 详情: http://docs.jpush.io/server/rest_api_v3_push/
type JPushClient struct {
appKey string
masterSecret string
headers map[string]string
http *httplib.HTTPClient
}
func NewJPushClient(appKey string, masterSecret string) *JPushClient {
client := JPushClient{
appKey: appKey,
masterSecret: masterSecret,
}
headers := make(map[string]string)
headers["User-Agent"] = "jpush-api-go-client"
headers["Connection"] = "keep-alive"
headers["Authorization"] = common.BasicAuth(appKey, masterSecret)
client.headers = headers
client.http = httplib.NewClient()
return &client
}
// 设置调试模式,调试模式下,会输出日志
func (jpc *JPushClient) SetDebug(debug bool) {
jpc.http.SetDebug(debug)
}
// 推送 API
func (jpc *JPushClient) Push(payload *push.PushObject) (*push.PushResult, error) {
return jpc.doPush(common.PUSH_URL, payload)
}
// 推送校验 API, 只用于验证推送调用是否能够成功,与推送 API 的区别在于:不向用户发送任何消息。
func (jpc *JPushClient) PushValidate(payload *push.PushObject) (*push.PushResult, error) {
return jpc.doPush(common.PUSH_VALIDATE_URL, payload)
}
func (jpc *JPushClient) doPush(url string, payload *push.PushObject) (*push.PushResult, error) {
resp, err := jpc.http.PostJson(url, payload, jpc.headers)
if err != nil {
return nil, err
}
result := &push.PushResult{}
err = result.FromResponse(resp)
return result, err
}
// 查询设备(设备的别名与标签)
func (jpc *JPushClient) QueryDevice(registrationId string) (*device.QueryDeviceResult, error) {
url := fmt.Sprintf(common.DEVICE_URL, registrationId)
resp, err := jpc.http.Get(url, nil, jpc.headers)
if err != nil {
return nil, err
}
result := &device.QueryDeviceResult{}
err = result.FromResponse(resp)
return result, err
}
// 更新设备 (设置的别名与标签)
func (jpc *JPushClient) UpdateDevice(registrationId string, payload *device.DeviceUpdate) (*common.ResponseBase, error) {
url := fmt.Sprintf(common.DEVICE_URL, registrationId)
resp, err := jpc.http.PostJson(url, payload, jpc.headers)
return common.ResponseOrError(resp, err)
}
// 查询标签列表
func (jpc *JPushClient) GetTags() (*device.GetTagsResult, error) {
resp, err := jpc.http.Get(common.QUERY_TAGS_URL, nil, jpc.headers)
if err != nil {
return nil, err
}
result := &device.GetTagsResult{}
err = result.FromResponse(resp)
return result, err
}
// 判断设备与标签的绑定
func (jpc *JPushClient) CheckTagUserExists(tag string, registrationId string) (*device.CheckTagUserExistsResult, error) {
url := fmt.Sprintf(common.CHECK_TAG_USER_EXISTS_URL, tag, registrationId)
resp, err := jpc.http.Get(url, nil, jpc.headers)
if err != nil {
return nil, err
}
result := &device.CheckTagUserExistsResult{}
err = result.FromResponse(resp)
return result, err
}
// 更新标签 (与设备的绑定的关系)
func (jpc *JPushClient) UpdateTagUsers(tag string, payload *device.UpdateTagUsersArgs) (*common.ResponseBase, error) {
url := fmt.Sprintf(common.UPDATE_TAG_USERS_URL, tag)
resp, err := jpc.http.PostJson(url, payload, jpc.headers)
return common.ResponseOrError(resp, err)
}
// 删除标签 (与设备的绑定关系)
func (jpc *JPushClient) DeleteTag(tag string, platforms []string) (*common.ResponseBase, error) {
url := fmt.Sprintf(common.DELETE_TAG_URL, tag)
params := addPlatformsToParams(platforms)
resp, err := jpc.http.Delete(url, params, jpc.headers)
return common.ResponseOrError(resp, err)
}
// 查询别名 (与设备的绑定关系)
func (jpc *JPushClient) GetAliasUsers(alias string, platforms []string) (*device.GetAliasUsersResult, error) {
url := fmt.Sprintf(common.QUERY_ALIAS_URL, alias)
params := addPlatformsToParams(platforms)
resp, err := jpc.http.Get(url, params, jpc.headers)
if err != nil {
return nil, err
}
result := &device.GetAliasUsersResult{}
err = result.FromResponse(resp)
return result, err
}
// 删除别名 (与设备的绑定关系)
func (jpc *JPushClient) DeleteAlias(alias string, platforms []string) (*common.ResponseBase, error) {
url := fmt.Sprintf(common.DELETE_ALIAS_URL, alias)
params := addPlatformsToParams(platforms)
resp, err := jpc.http.Delete(url, params, jpc.headers)
return common.ResponseOrError(resp, err)
}
// 送达统计
func (jpc *JPushClient) GetReceivedReport(msgIds []uint64) (*report.ReceiveReport, error) {
ids := make([]string, 0, len(msgIds))
for _, msgId := range msgIds {
ids = append(ids, strconv.FormatUint(msgId, 10))
}
params := map[string]interface{}{"msg_ids": strings.Join(ids, ",")}
resp, err := jpc.http.Get(common.RECEIVED_REPORT_URL, params, jpc.headers)
if err != nil {
return nil, err
}
result := &report.ReceiveReport{}
err = result.FromResponse(resp)
return result, err
}
////////////////////////////////////////////////////////////////////////////////
func addPlatformsToParams(platforms []string) map[string]interface{} {
if platforms == nil {
return nil
}
params := make(map[string]interface{})
params["platform"] = strings.Join(platforms, ",")
return params
}