Skip to content

Commit d728146

Browse files
author
施凯伦
committed
tracer about api supported
1 parent 2aabafe commit d728146

File tree

3 files changed

+233
-30
lines changed

3 files changed

+233
-30
lines changed

client.go

Lines changed: 131 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,57 @@ func (m *MiPush) MultiTopicBroadcast(msg *Message, topics []string, topicOP Topi
194194
return &result, nil
195195
}
196196

197-
func (m *MiPush) Stats(start, end string) (*StatsResult, error) {
198-
params := m.assembleStatsParams(start, end)
197+
// 检测定时消息的任务是否存在。
198+
func (m *MiPush) CheckScheduleJobExist(msgID string) (*Result, error) {
199+
params := m.assembleCheckScheduleJobParams(msgID)
200+
bytes, err := m.doPost(m.host+ScheduleJobExistURL, params)
201+
if err != nil {
202+
return nil, err
203+
}
204+
var result Result
205+
err = json.Unmarshal(bytes, &result)
206+
if err != nil {
207+
return nil, err
208+
}
209+
return &result, nil
210+
}
211+
212+
// 删除指定的定时消息
213+
func (m *MiPush) DeleteScheduleJob(msgID string) (*Result, error) {
214+
params := m.assembleDeleteScheduleJobParams(msgID)
215+
bytes, err := m.doPost(m.host+ScheduleJobDeleteURL, params)
216+
if err != nil {
217+
return nil, err
218+
}
219+
var result Result
220+
err = json.Unmarshal(bytes, &result)
221+
if err != nil {
222+
return nil, err
223+
}
224+
return &result, nil
225+
}
226+
227+
// 删除指定的定时消息
228+
func (m *MiPush) DeleteScheduleJobByJobKey(jobKey string) (*Result, error) {
229+
params := m.assembleDeleteScheduleJobByJobKeyParams(jobKey)
230+
bytes, err := m.doPost(m.host+ScheduleJobDeleteByJobKeyURL, params)
231+
if err != nil {
232+
return nil, err
233+
}
234+
var result Result
235+
err = json.Unmarshal(bytes, &result)
236+
if err != nil {
237+
return nil, err
238+
}
239+
return &result, nil
240+
}
241+
242+
// 获取指定日期范围内的日统计数据(如果日期范围包含今日,则今日数据为从今天00:00开始到现在的累计量)。
243+
// packageName:
244+
// Android设备,传入App的包名
245+
// IOS设备,传入App的Bundle Id
246+
func (m *MiPush) Stats(start, end, packageName string) (*StatsResult, error) {
247+
params := m.assembleStatsParams(start, end, packageName)
199248
bytes, err := m.doGet(m.host+StatsURL, params)
200249
if err != nil {
201250
return nil, err
@@ -208,7 +257,8 @@ func (m *MiPush) Stats(start, end string) (*StatsResult, error) {
208257
return &result, nil
209258
}
210259

211-
func (m *MiPush) Status(msgID string) (*StatusResult, error) {
260+
// 获取指定ID的消息状态
261+
func (m *MiPush) GetMessageStatusByMsgID(msgID string) (*StatusResult, error) {
212262
params := m.assembleStatusParams(msgID)
213263
bytes, err := m.doGet(m.host+MessageStatusURL, params)
214264
if err != nil {
@@ -222,22 +272,34 @@ func (m *MiPush) Status(msgID string) (*StatusResult, error) {
222272
return &result, nil
223273
}
224274

225-
func (m *MiPush) assembleMultiTopicBroadcastParams(msg *Message, topics []string, topicOP TopicOP) url.Values {
226-
form := m.defaultForm(msg)
227-
form.Add("topic_op", string(topicOP))
228-
form.Add("topics", strings.Join(topics, ";$;"))
229-
return form
230-
}
231-
232-
func (m *MiPush) assembleBroadcastParams(msg *Message, topic string) url.Values {
233-
form := m.defaultForm(msg)
234-
form.Add("topic", topic)
235-
return form
275+
// 获取某个时间间隔内所有消息的状态。
276+
func (m *MiPush) GetMessageStatusByJobKey(jobKey string) (*StatusResult, error) {
277+
params := m.assembleStatusByJobKeyParams(jobKey)
278+
bytes, err := m.doGet(m.host+MessagesStatusURL, params)
279+
if err != nil {
280+
return nil, err
281+
}
282+
var result StatusResult
283+
err = json.Unmarshal(bytes, &result)
284+
if err != nil {
285+
return nil, err
286+
}
287+
return &result, nil
236288
}
237289

238-
func (m *MiPush) assembleBroadcastAllParams(msg *Message) url.Values {
239-
form := m.defaultForm(msg)
240-
return form
290+
// 获取某个时间间隔内所有消息的状态。
291+
func (m *MiPush) GetMessageStatusPeriod(beginTime, endTime int64) (*StatusResult, error) {
292+
params := m.assembleStatusPeriodParams(beginTime, endTime)
293+
bytes, err := m.doGet(m.host+MessagesStatusURL, params)
294+
if err != nil {
295+
return nil, err
296+
}
297+
var result StatusResult
298+
err = json.Unmarshal(bytes, &result)
299+
if err != nil {
300+
return nil, err
301+
}
302+
return &result, nil
241303
}
242304

243305
func (m *MiPush) assembleSendParams(msg *Message, regID string) url.Values {
@@ -283,10 +345,47 @@ func (m *MiPush) assembleSendToUserAccountParams(msg *Message, userAccount strin
283345
return form
284346
}
285347

286-
func (m *MiPush) assembleStatsParams(start, end string) string {
348+
func (m *MiPush) assembleBroadcastParams(msg *Message, topic string) url.Values {
349+
form := m.defaultForm(msg)
350+
form.Add("topic", topic)
351+
return form
352+
}
353+
354+
func (m *MiPush) assembleBroadcastAllParams(msg *Message) url.Values {
355+
form := m.defaultForm(msg)
356+
return form
357+
}
358+
359+
func (m *MiPush) assembleMultiTopicBroadcastParams(msg *Message, topics []string, topicOP TopicOP) url.Values {
360+
form := m.defaultForm(msg)
361+
form.Add("topic_op", string(topicOP))
362+
form.Add("topics", strings.Join(topics, ";$;"))
363+
return form
364+
}
365+
366+
func (m *MiPush) assembleCheckScheduleJobParams(msgID string) url.Values {
367+
form := url.Values{}
368+
form.Add("job_id", msgID)
369+
return form
370+
}
371+
372+
func (m *MiPush) assembleDeleteScheduleJobParams(msgID string) url.Values {
373+
form := url.Values{}
374+
form.Add("job_id", msgID)
375+
return form
376+
}
377+
378+
func (m *MiPush) assembleDeleteScheduleJobByJobKeyParams(jobKey string) url.Values {
379+
form := url.Values{}
380+
form.Add("job_key", jobKey)
381+
return form
382+
}
383+
384+
func (m *MiPush) assembleStatsParams(start, end, packageName string) string {
287385
form := url.Values{}
288386
form.Add("start_date", start)
289387
form.Add("end_date", end)
388+
form.Add("restricted_package_name", packageName)
290389
return "?" + form.Encode()
291390
}
292391

@@ -296,6 +395,19 @@ func (m *MiPush) assembleStatusParams(msgID string) string {
296395
return "?" + form.Encode()
297396
}
298397

398+
func (m *MiPush) assembleStatusByJobKeyParams(jobKey string) string {
399+
form := url.Values{}
400+
form.Add("job_key", jobKey)
401+
return "?" + form.Encode()
402+
}
403+
404+
func (m *MiPush) assembleStatusPeriodParams(beginTime, endTime int64) string {
405+
form := url.Values{}
406+
form.Add("begin_time", strconv.FormatInt(int64(beginTime), 10))
407+
form.Add("end_time", strconv.FormatInt(int64(endTime), 10))
408+
return "?" + form.Encode()
409+
}
410+
299411
func (m *MiPush) handleResponse(response *http.Response) ([]byte, error) {
300412
defer func() {
301413
_ = response.Body.Close()
@@ -366,6 +478,7 @@ func (m *MiPush) doGet(url string, params string) ([]byte, error) {
366478
if err != nil {
367479
return nil, err
368480
}
481+
fmt.Println("get result=", string(result))
369482
return result, nil
370483
}
371484

client_test.go

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

33
import (
4-
"fmt"
54
"testing"
5+
"time"
66
)
77

88
var packageName string = "com.xiaomi.mipushdemo"
@@ -26,53 +26,139 @@ var topic2 string = "topic2"
2626

2727
func TestMiPush_Send(t *testing.T) {
2828
result, err := client.Send(msg1, regID1)
29-
fmt.Println("result1, err1", result, err)
29+
if err != nil {
30+
t.Errorf("TestMiPush_Send failed :%v\n", err)
31+
}
32+
t.Log("result=", result)
3033
}
3134

3235
func TestMiPush_SendToList(t *testing.T) {
3336
result, err := client.SendToList(msg1, []string{regID1, regID2})
34-
fmt.Println("result1, err1", result, err)
37+
if err != nil {
38+
t.Errorf("TestMiPush_SendToList failed :%v\n", err)
39+
}
40+
t.Log("result=", result)
3541
}
3642

3743
// Not Finished
3844
func TestMiPush_SendTargetMessageList(t *testing.T) {
3945
msgList := []*TargetedMessage{NewTargetedMessage(msg1, regID1, TargetTypeRegID), NewTargetedMessage(msg2, regID2, TargetTypeRegID)}
4046
result, err := client.SendTargetMessageList(msgList)
41-
fmt.Println("result1, err1", result, err)
47+
if err != nil {
48+
t.Errorf("TestMiPush_SendTargetMessageList failed :%v\n", err)
49+
}
50+
t.Log("result=", result)
4251
}
4352

4453
func TestMiPush_SendToAlias(t *testing.T) {
4554
result, err := client.SendToAlias(msg1, alias1)
46-
fmt.Println("result1, err1", result, err)
55+
if err != nil {
56+
t.Errorf("TestMiPush_SendToAlias failed :%v\n", err)
57+
}
58+
t.Log("result=", result)
4759
}
4860

4961
func TestMiPush_SendToAliasList(t *testing.T) {
5062
result, err := client.SendToAliasList(msg1, []string{alias1, alias2})
51-
fmt.Println("result1, err1", result, err)
63+
if err != nil {
64+
t.Errorf("TestMiPush_SendToAliasList failed :%v\n", err)
65+
}
66+
t.Log("result=", result)
5267
}
5368

5469
func TestMiPush_SendToUserAccount(t *testing.T) {
5570
result, err := client.SendToUserAccount(msg1, account1)
56-
fmt.Println("result1, err1", result, err)
71+
if err != nil {
72+
t.Errorf("TestMiPush_SendToUserAccount failed :%v\n", err)
73+
}
74+
t.Log("result=", result)
5775
}
5876

5977
func TestMiPush_SendToUserAccountList(t *testing.T) {
6078
result, err := client.SendToUserAccountList(msg1, []string{account1, account2})
61-
fmt.Println("result1, err1", result, err)
79+
if err != nil {
80+
t.Errorf("TestMiPush_SendToUserAccountList failed :%v\n", err)
81+
}
82+
t.Log("result=", result)
6283
}
6384

6485
// Not Finished
6586
func TestMiPush_Broadcast(t *testing.T) {
6687
result, err := client.Broadcast(msg1, topic1)
67-
fmt.Println("result1, err1", result, err)
88+
if err != nil {
89+
t.Errorf("TestMiPush_Broadcast failed :%v\n", err)
90+
}
91+
t.Log("result=", result)
6892
}
6993

7094
func TestMiPush_BroadcastAll(t *testing.T) {
7195
result, err := client.BroadcastAll(msg1)
72-
fmt.Println("result1, err1", result, err)
96+
if err != nil {
97+
t.Errorf("TestMiPush_BroadcastAll failed :%v\n", err)
98+
}
99+
t.Log("result=", result)
73100
}
74101

75102
func TestMiPush_MultiTopicBroadcast(t *testing.T) {
76103
result, err := client.MultiTopicBroadcast(msg1, []string{topic1, topic2}, INTERSECTION)
77-
fmt.Println("result1, err1", result, err)
104+
if err != nil {
105+
t.Errorf("TestMiPush_MultiTopicBroadcast failed :%v\n", err)
106+
}
107+
t.Log("result=", result)
108+
}
109+
110+
func TestMiPush_CheckScheduleJobExist(t *testing.T) {
111+
result, err := client.CheckScheduleJobExist("Xcm45b21474513716292EL")
112+
if err != nil {
113+
t.Errorf("TestMiPush_CheckScheduleJobExist failed :%v\n", err)
114+
}
115+
t.Log("result=", result)
116+
}
117+
118+
func TestMiPush_DeleteScheduleJob(t *testing.T) {
119+
result, err := client.DeleteScheduleJob("Xcm45b21474513716292EL")
120+
if err != nil {
121+
t.Errorf("TestMiPush_DeleteScheduleJob failed :%v\n", err)
122+
}
123+
t.Log("result=", result)
124+
}
125+
126+
func TestMiPush_DeleteScheduleJobByJobKey(t *testing.T) {
127+
result, err := client.DeleteScheduleJobByJobKey("Xcm45b21474513716292EL")
128+
if err != nil {
129+
t.Errorf("TestMiPush_DeleteScheduleJobByJobKey failed :%v\n", err)
130+
}
131+
t.Log("result=", result)
132+
}
133+
134+
func TestMiPush_Stats(t *testing.T) {
135+
result, err := client.Stats("20160921", "20160922", packageName)
136+
if err != nil {
137+
t.Errorf("TestMiPush_Stats failed :%v\n", err)
138+
}
139+
t.Log("result=", result)
140+
}
141+
142+
func TestMiPush_GetMessageStatusByMsgID(t *testing.T) {
143+
result, err := client.GetMessageStatusByMsgID("Xcm49b95474515503850Sn")
144+
if err != nil {
145+
t.Errorf("TestMiPush_GetMessageStatusByMsgID failed :%v\n", err)
146+
}
147+
t.Log("result=", result)
148+
}
149+
150+
func TestMiPush_GetMessageStatusByJobKey(t *testing.T) {
151+
result, err := client.GetMessageStatusByMsgID("xxxxx")
152+
if err != nil {
153+
t.Errorf("TestMiPush_GetMessageStatusByJobKey failed :%v\n", err)
154+
}
155+
t.Log("result=", result)
156+
}
157+
158+
func TestMiPush_GetMessageStatusPeriod(t *testing.T) {
159+
result, err := client.GetMessageStatusPeriod(time.Now().Add(-time.Hour*24).Unix()*1000, time.Now().Unix()*1000)
160+
if err != nil {
161+
t.Errorf("TestMiPush_GetMessageStatusPeriod failed :%v\n", err)
162+
}
163+
t.Log("result=", result)
78164
}

constants.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ const (
1717
MessageMultiTopicURL = "/v2/message/multi_topic" // 根据topic,发送消息到指定一组设备上
1818
MultiPackageNameMessageAllURL = "/v3/message/all" // 向所有设备推送某条消息
1919
MessageAllURL = "/v2/message/all" // 向所有设备推送某条消息
20+
MultiTopicURL = "/v3/message/multi_topic" // 向多个topic广播消息
21+
ScheduleJobExistURL = "/v2/schedule_job/exist" // 检测定时消息的任务是否存在。
22+
ScheduleJobDeleteURL = "/v2/schedule_job/delete" // 删除指定的定时消息。
23+
ScheduleJobDeleteByJobKeyURL = "/v3/schedule_job/delete" // 删除指定的定时消息。
2024
StatsURL = "/v1/stats/message/counters" // 统计push
2125
MessageStatusURL = "/v1/trace/message/status" // 获取指定ID的消息状态
22-
MultiTopicURL = "/v3/message/multi_topic"
26+
MessagesStatusURL = "/v1/trace/messages/status" // 获取某个时间间隔内所有消息的状态
2327
)
2428

2529
// for future targeted push

0 commit comments

Comments
 (0)