-
Notifications
You must be signed in to change notification settings - Fork 1
/
baidu.go
313 lines (267 loc) · 8.54 KB
/
baidu.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
// Package baidu provides Baidu Push client implementation, and a push.Pusher
// implementation for skygear-server.
//
// Currently only push notification to Android is supported.
package baidupush
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path"
"runtime"
"strconv"
"strings"
"time"
)
var timeNow = func() time.Time { return time.Now().UTC() }
// DeviceType specifies the kind of device to send a notification.
// Currently only Android is supported. This type exists for documentation purpose.
type DeviceType int
// Types of device that can be sent a notification via baidu push. Note that
// DeviceTypeUnspecified is not a valid value in a push request.
const (
DeviceTypeUnspecified DeviceType = 0
DeviceTypeAndroid DeviceType = 3
DeviceTypeIOS DeviceType = 4
)
// PushAllDeviceRequest represents a push request to all devices registered
// to an account. It exists for testing purpose and no skygear's API is
// using it.
type PushAllDeviceRequest struct {
MsgType int
Msg AndroidNotificationMsg
MsgExpires int
DeployStatus int
SendTime int
}
// PushSingleDeviceRequest represents a push request to a single device.
// See http://push.baidu.com/doc/restapi/restapi#-post-rest-3-0-push-single_device.
type PushSingleDeviceRequest struct {
ChannelID string
MsgType int
Msg AndroidNotificationMsg
MsgExpires int // in seconds
DeployStatus int // only applicable in iOS
}
// NewPushSingleDeviceRequest returns a NewPushSingleDeviceRequest
func NewPushSingleDeviceRequest(channelID string, Msg AndroidNotificationMsg) PushSingleDeviceRequest {
return PushSingleDeviceRequest{
ChannelID: channelID,
Msg: Msg,
MsgType: 1,
}
}
// AndroidNotificationMsg represents a notification of Android devices.
type AndroidNotificationMsg struct {
Title string `json:"title"`
Description string `json:"description"`
NotificationBuilderID int `json:"notification_builder_id,omitempty"`
NotificationBasicStyle int `json:"notification_basic_style,omitempty"`
OpenType int `json:"open_type,omitempty"`
URL string `json:"url,omitempty"`
PkgContent string `json:"pkg_content,omitempty"`
CustomContent map[string]interface{} `json:"custom_content,omitempty"`
}
// NewAndroidNotificationMsg returns a new AndroidNotificationMsg.
func NewAndroidNotificationMsg(title, description string) AndroidNotificationMsg {
return AndroidNotificationMsg{
Title: title,
Description: description,
}
}
// PushResponse is the response returned from a successful push request (e.g.
// PushAllDeviceRequest & PushSingleDeviceRequest)
type PushResponse struct {
RequestID int `json:"request_id"`
ResponseParams PushResponseParams `json:"response_params"`
}
// PushResponseParams is the params returned from a successful push request.
type PushResponseParams struct {
MsgID string `json:"msg_id"`
SendTime int `json:"send_time"`
}
// An Error is an erroneous response returned by baidu push server.
type Error struct {
// Request ID that leads to this error.
RequestID int `json:"request_id"`
// See http://push.baidu.com/doc/restapi/error_code for a complete
// list of error codes.
Code int `json:"error_code"`
// A human readable message about the error in Simplified Chinese.
Msg string `json:"error_msg"`
}
func (e Error) Error() string {
return fmt.Sprintf(`%v: %v`, e.Code, e.Msg)
}
// A Client is a baidu push client.
type Client struct {
httpClient *http.Client
baseURL *url.URL
apiKey string
secretKey string
}
// NewClient returns a new Client given a baseURL, apiKey and secretyKey.
func NewClient(baseURL string, apiKey string, secretKey string) *Client {
parsed, err := url.Parse(baseURL)
if err != nil {
panic(err)
}
return &Client{
httpClient: &http.Client{},
baseURL: parsed,
apiKey: apiKey,
secretKey: secretKey,
}
}
func newClientWithURL(baseURL *url.URL, apiKey string, secretKey string) *Client {
return &Client{
httpClient: &http.Client{},
baseURL: baseURL,
apiKey: apiKey,
secretKey: secretKey,
}
}
// PushAllDevice issues a "push/all" request to baidu push server.
func (c *Client) PushAllDevice(req PushAllDeviceRequest) (*PushResponse, error) {
urlCopy := *c.baseURL
urlCopy.Path = path.Join(c.baseURL.Path, "push/all")
url := urlCopy.String()
timestamp := timeNow().Unix()
msgBytes, err := json.Marshal(req.Msg)
if err != nil {
return nil, fmt.Errorf("Couldn't encode push msg; err = %v", err)
}
params := map[string]string{
"msg_type": strconv.Itoa(req.MsgType),
"msg": string(msgBytes),
}
if req.MsgExpires != 0 {
params["msg_expires"] = strconv.Itoa(req.MsgExpires)
}
if req.DeployStatus != 0 {
params["deploy_status"] = strconv.Itoa(req.DeployStatus)
}
if req.SendTime != 0 {
params["send_time"] = strconv.Itoa(req.SendTime)
}
pushReq := request{
method: "POST",
url: url,
apiKey: c.apiKey,
timestamp: timestamp,
expires: 0,
deviceType: DeviceTypeAndroid,
params: params,
}
pushResp := PushResponse{}
if err := c.do(&pushReq, &pushResp); err != nil {
return nil, err
}
return &pushResp, nil
}
// PushSingleDevice issues a "push/single_device" request to baidu push server.
func (c *Client) PushSingleDevice(req PushSingleDeviceRequest) (*PushResponse, error) {
urlCopy := *c.baseURL
urlCopy.Path = path.Join(c.baseURL.Path, "push/single_device")
url := urlCopy.String()
timestamp := timeNow().Unix()
msgBytes, err := json.Marshal(req.Msg)
if err != nil {
return nil, fmt.Errorf("Couldn't encode push msg; err = %v", err)
}
params := map[string]string{
"channel_id": req.ChannelID,
"msg_type": strconv.Itoa(req.MsgType),
"msg": string(msgBytes),
}
if req.MsgExpires != 0 {
params["msg_expires"] = strconv.Itoa(req.MsgExpires)
}
if req.DeployStatus != 0 {
params["deploy_status"] = strconv.Itoa(req.DeployStatus)
}
pushReq := request{
method: "POST",
url: url,
apiKey: c.apiKey,
timestamp: timestamp,
expires: 0,
deviceType: DeviceTypeAndroid,
params: params,
}
pushResp := PushResponse{}
if err := c.do(&pushReq, &pushResp); err != nil {
return nil, err
}
return &pushResp, nil
}
func (c *Client) do(req *request, resp interface{}) error {
httpReq := httpRequest(*req, c.secretKey)
httpResp, err := c.httpClient.Do(httpReq)
if err != nil {
return err
}
body, err := ioutil.ReadAll(httpResp.Body)
if err != nil {
return fmt.Errorf("push/baidu: couldn't read resp body: err = %v", err)
}
if httpResp.StatusCode < 200 || httpResp.StatusCode > 299 {
e := Error{}
if err := json.Unmarshal(body, &e); err != nil {
return fmt.Errorf("push/baidu: couldn't decode error resp body: err = %v", err)
}
return e
}
if err := json.Unmarshal(body, resp); err != nil {
return fmt.Errorf("push/baidu: couldn't decode resp; err = %v", err)
}
return nil
}
// Private struct that fully represents a request to baidu push. It and
// Client.secretKey should completely defines a HTTP request to baidu push.
type request struct {
method string
url string
apiKey string
timestamp int64
expires int64
deviceType DeviceType
params map[string]string
}
func httpRequest(r request, secretKey string) *http.Request {
values := urlValues(r)
signature := signRequest(r.method, r.url, values, secretKey)
values.Set("sign", signature)
body := strings.NewReader(values.Encode())
req, err := http.NewRequest(r.method, r.url, body)
if err != nil {
// it must be programmatical error for NewRequest to err because
// body is strings.Reader, which never err on Read
panic(fmt.Sprintf("want http.NewRequest succeed, got err = %v", err))
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
req.Header.Set("User-Agent", uaString())
return req
}
func urlValues(r request) url.Values {
values := url.Values{}
// common params
values.Set("apikey", r.apiKey)
values.Set("timestamp", strconv.FormatInt(r.timestamp, 10))
if r.expires > 0 {
values.Set("expires", strconv.FormatInt(r.expires, 10))
}
if r.deviceType != 0 {
values.Set("device_type", strconv.Itoa(int(r.deviceType)))
}
// request-specific params
for key, value := range r.params {
values.Add(key, value)
}
return values
}
func uaString() string {
return fmt.Sprintf("BCCS_SDK/3.0 (%v; %v) Go/%v go-baidupush/%v)", runtime.GOOS, runtime.GOARCH, runtime.Version(), BaiduPushPackageVersion)
}