-
Notifications
You must be signed in to change notification settings - Fork 0
/
push-client.go
138 lines (123 loc) · 3.28 KB
/
push-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
package u_push
import (
"bytes"
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"strconv"
"strings"
"time"
)
const (
// The user agent
USER_AGENT = "Mozilla/5.0"
// The host
host = "http://msg.umeng.com"
// The upload path
//uploadPath = "/upload"
// The post path
postPath = "/api/send"
// The upload path
uploadPath = "/upload"
)
const HttpReqTimeout = 20 * time.Second
type PushClient struct {
timeout time.Duration
}
func (t *PushClient) SetTimeout(timeout time.Duration) {
t.timeout = timeout
}
func (t *PushClient) Send(msg fieldSetter) error {
timestamp := strconv.FormatInt(time.Now().Unix(), 10)
msg.SetPredefinedKeyValue("timestamp", timestamp)
url := host + postPath
postBody, err := msg.getPostBody()
if err != nil {
return err
}
secretStr := "POST" + url + string(postBody) + msg.getAppMasterSecret()
w := md5.New()
_, err = io.WriteString(w, secretStr)
if err != nil {
return err
}
sign := strings.ToLower(hex.EncodeToString(w.Sum(nil)))
url = url + "?sign=" + sign
body := bytes.NewBuffer(postBody)
headers := make(map[string]string)
headers["User-Agent"] = USER_AGENT
if t.timeout < time.Second*5 {
t.timeout = HttpReqTimeout
}
byte, err := httpPostJSON(url, headers, body, t.timeout)
if err != nil {
var resp map[string]interface{}
err = json.Unmarshal(byte, &resp)
if err != nil {
return err
}
if ret := resp["ret"]; ret == "FAIL" {
if data, ok := resp["data"]; ok {
if d, ok := data.(map[string]interface{}); ok {
return errors.New("Failed to send the notification!:" + d["error_code"].(string) + d["error_msg"].(string))
}
}
}
return errors.New("Failed to send the notification!: unknow error")
}
return nil
}
func (t *PushClient) UploadContents(appkey, appMasterSecret, contents string) (string, error) {
// Construct the json string
uploadJson := make(map[string]interface{})
uploadJson["appkey"] = appkey
timestamp := strconv.FormatInt(time.Now().Unix(), 10)
uploadJson["timestamp"] = timestamp
uploadJson["content"] = contents
// Construct the request
url := host + uploadPath
postBody, err := json.Marshal(uploadJson)
if err != nil {
return "", err
}
secretStr := "POST" + url + string(postBody) + appMasterSecret
w := md5.New()
_, err = io.WriteString(w, secretStr)
if err != nil {
return "", err
}
sign := strings.ToLower(hex.EncodeToString(w.Sum(nil)))
url = url + "?sign=" + sign
body := bytes.NewBuffer(postBody)
headers := make(map[string]string)
headers["User-Agent"] = USER_AGENT
if t.timeout < time.Second*5 {
t.timeout = HttpReqTimeout
}
byte, err := httpPostJSON(url, headers, body, t.timeout)
var resp map[string]interface{}
err = json.Unmarshal(byte, &resp)
if err != nil {
return "", err
}
if ret := resp["ret"]; ret == "SUCCESS" {
if data, ok := resp["data"]; ok {
if d, ok := data.(map[string]interface{}); ok {
if fileId, ok := d["file_id"]; ok {
return fmt.Sprintf("%v", fileId), nil
}
}
}
return "", errors.New("Failed to upload file!: can not get file_id")
} else {
if data, ok := resp["data"]; ok {
if d, ok := data.(map[string]interface{}); ok {
return "", errors.New("Failed to upload file!:" + d["error_code"].(string) + d["error_msg"].(string))
}
}
return "", errors.New("Failed to upload file!: unknow error")
}
}