Skip to content

Commit 2dc32e8

Browse files
author
施凯伦
committed
init xiaomi push golang sdk
1 parent 26a2ae8 commit 2dc32e8

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

client.go

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,44 @@ func NewClient(appSecret, packageName string) *MiPush {
2727

2828
func (m *MiPush) Push(msg *Message, regIDList []string) (*Result, error) {
2929
params := m.assemblePushParams(msg, regIDList)
30-
return m.doPost(m.host+RegURL, params)
30+
bytes, err := m.doPost(m.host+RegURL, params)
31+
if err != nil {
32+
return nil, err
33+
}
34+
var result Result
35+
err = json.Unmarshal(bytes, &result)
36+
if err != nil {
37+
return nil, err
38+
}
39+
return &result, nil
3140
}
3241

33-
func (m *MiPush) Stats(start, end string) (*Result, error) {
42+
func (m *MiPush) Stats(start, end string) (*StatsResult, error) {
3443
params := m.assembleStatsParams(start, end)
35-
return m.doGet(m.host+StatsURL, params)
44+
bytes, err := m.doGet(m.host+StatsURL, params)
45+
if err != nil {
46+
return nil, err
47+
}
48+
var result StatsResult
49+
err = json.Unmarshal(bytes, &result)
50+
if err != nil {
51+
return nil, err
52+
}
53+
return &result, nil
3654
}
3755

38-
func (m *MiPush) Status(msgID string) (*Result, error) {
56+
func (m *MiPush) Status(msgID string) (*StatusResult, error) {
3957
params := m.assembleStatusParams(msgID)
40-
return m.doGet(m.host+MessageStatusURL, params)
58+
bytes, err := m.doGet(m.host+MessageStatusURL, params)
59+
if err != nil {
60+
return nil, err
61+
}
62+
var result StatusResult
63+
err = json.Unmarshal(bytes, &result)
64+
if err != nil {
65+
return nil, err
66+
}
67+
return &result, nil
4168
}
4269

4370
func (m *MiPush) assemblePushParams(msg *Message, regIDList []string) map[string]string {
@@ -87,19 +114,19 @@ func (m *MiPush) assembleStatusParams(msgID string) string {
87114
return "?" + form.Encode()
88115
}
89116

90-
func (m *MiPush) handlePushResponse(response *http.Response) (*Result, error) {
117+
func (m *MiPush) handleResponse(response *http.Response) ([]byte, error) {
91118
defer func() {
92119
_ = response.Body.Close()
93120
}()
94121
data, err := ioutil.ReadAll(response.Body)
95122
if err != nil {
96123
return nil, err
97124
}
98-
return getResultFromJSON(data)
125+
return data, nil
99126
}
100127

101-
func (m *MiPush) doPost(url string, params map[string]string) (*Result, error) {
102-
var result *Result
128+
func (m *MiPush) doPost(url string, params map[string]string) ([]byte, error) {
129+
var result []byte
103130
var req *http.Request
104131
var resp *http.Response
105132
var err error
@@ -114,16 +141,15 @@ func (m *MiPush) doPost(url string, params map[string]string) (*Result, error) {
114141
if err != nil {
115142
return nil, err
116143
}
117-
defer func() { _ = resp.Body.Close() }()
118-
result, err = m.handlePushResponse(resp)
144+
result, err = m.handleResponse(resp)
119145
if err != nil {
120146
return nil, err
121147
}
122148
return result, nil
123149
}
124150

125-
func (m *MiPush) doGet(url string, params string) (*Result, error) {
126-
var result *Result
151+
func (m *MiPush) doGet(url string, params string) ([]byte, error) {
152+
var result []byte
127153
var req *http.Request
128154
var resp *http.Response
129155
var err error
@@ -136,8 +162,7 @@ func (m *MiPush) doGet(url string, params string) (*Result, error) {
136162
if err != nil {
137163
return nil, err
138164
}
139-
defer func() { _ = resp.Body.Close() }()
140-
result, err = m.handlePushResponse(resp)
165+
result, err = m.handleResponse(resp)
141166
if err != nil {
142167
return nil, err
143168
}

result.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package xiaomipush
22

3-
import "encoding/json"
4-
53
type Result struct {
64
MessageID string `json:"trace_id"`
75
Code int64 `json:"code"`
@@ -33,12 +31,3 @@ type StatusResult struct {
3331
ClickRate string
3432
} `json:"data,omitempty"`
3533
}
36-
37-
func getResultFromJSON(data []byte) (*Result, error) {
38-
var result Result
39-
err := json.Unmarshal(data, &result)
40-
if err != nil {
41-
return nil, err
42-
}
43-
return &result, err
44-
}

0 commit comments

Comments
 (0)