/
tag.go
86 lines (75 loc) · 1.97 KB
/
tag.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
package push
import (
"encoding/json"
"strings"
"time"
)
type tagReq struct {
Appkey string `json:"appkey"`
Timestamp int64 `json:"timestamp"`
DeviceTokens string `json:"device_tokens"`
Tag string `json:"tag,omitempty"`
}
func (t *tagReq) setTags(tags []string) *tagReq {
t.Tag = strings.Join(tags, ",")
return t
}
func (t *tagReq) setDeviceToken(device string) *tagReq {
t.DeviceTokens = device
return t
}
func (a *App) buildTagReq() (req *tagReq) {
req = new(tagReq)
req.Appkey = a.AppKey
req.Timestamp = time.Now().Unix()
return req
}
func (a *App) ListTags(device string) (tags []string, err error) {
data := a.buildTagReq().setDeviceToken(device)
result, err := a.Request(Host+TagListPath, data)
if err != nil {
return
}
var v struct {
Ret string `json:"ret"`
Data struct {
Data struct {
Tags string `json:"tags"`
} `json:"data"`
} `json:"data"`
}
err = json.Unmarshal(result, &v)
if err != nil {
return
}
if v.Data.Data.Tags == "" {
return []string{}, nil
}
return strings.Split(v.Data.Data.Tags, ","), nil
}
func (a *App) AddTags(device string, tags []string) (err error) {
data := a.buildTagReq().setDeviceToken(device).setTags(tags)
_, err = a.Request(Host+TagAddPath, data)
return
}
func (a *App) AddTag(device string, tag string) (err error) {
return a.AddTags(device, []string{tag})
}
func (a *App) SetTags(device string, tags []string) (err error) {
data := a.buildTagReq().setDeviceToken(device).setTags(tags)
_, err = a.Request(Host+TagSetPath, data)
return
}
func (a *App) DeleteTags(device string, tags []string) (err error) {
data := a.buildTagReq().setDeviceToken(device).setTags(tags)
_, err = a.Request(Host+TagDeletePath, data)
return
}
func (a *App) DeleteTag(device string, tag string) (err error) {
return a.DeleteTags(device, []string{tag})
}
func (a *App) ClearTags(device string) (err error) {
data := a.buildTagReq().setDeviceToken(device)
_, err = a.Request(Host+TagClearPath, data)
return
}