Skip to content

Commit 26ecf5a

Browse files
author
liyangliang
committed
增加 tag, alias 相关接口
1 parent 5358ae0 commit 26ecf5a

File tree

13 files changed

+478
-227
lines changed

13 files changed

+478
-227
lines changed

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,6 @@ if err != nil {
185185
}
186186
```
187187

188-
## 4. 完整示例
188+
## 4. 更多示例
189189

190-
完整的示例可以直接运行,代码在这里:[examples/push.go](examples/push.go)
190+
更多例子可以看这里:[jpush_test.go](jpush_test.go)

Diff for: common/consts.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,27 @@ const (
77
rateLimitRemainingHeader = "X-Rate-Limit-Remaining"
88
rateLimitResetHeader = "X-Rate-Limit-Reset"
99

10-
PUSH_URL = "https://api.jpush.cn/v3/push"
11-
PUSH_VALIDATE_URL = "https://api.jpush.cn/v3/push/validate"
10+
push_host = "https://api.jpush.cn"
11+
device_host = "https://device.jpush.cn"
1212

13-
DEVICE_URL = "https://device.jpush.cn/v3/devices/%s"
13+
PUSH_URL = push_host + "/v3/push"
14+
PUSH_VALIDATE_URL = push_host + "/v3/push/validate"
15+
16+
// GET /v3/devices/{registration_id}
17+
DEVICE_URL = device_host + "/v3/devices/%s"
18+
19+
QUERY_TAGS_URL = device_host + "/v3/tags/"
20+
// GET /v3/tags/{tag_value}/registration_ids/{registration_id}
21+
CHECK_TAG_USER_EXISTS_URL = device_host + "/v3/tags/%s/registration_ids/%s"
22+
// POST /v3/tags/{tag_value}
23+
UPDATE_TAG_USERS_URL = device_host + "/v3/tags/%s"
24+
// DELETE /v3/tags/{tag_value}
25+
DELETE_TAG_URL = device_host + "/v3/tags/%s"
26+
27+
// GET /v3/aliases/{alias_value}
28+
QUERY_ALIAS_URL = device_host + "/v3/aliases/%s"
29+
// DELETE /v3/aliases/{alias_value}
30+
DELETE_ALIAS_URL = device_host + "/v3/aliases/%s"
1431
)
1532

1633
var (

Diff for: common/structures.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type ErrorResult struct {
1111
}
1212

1313
func (pe *ErrorResult) String() string {
14-
return fmt.Sprintf("{code: %d, message: %s}", pe.Code, pe.Message)
14+
return fmt.Sprintf("{code: %d, message: \"%s\"}", pe.Code, pe.Message)
1515
}
1616

1717
type RateLimitInfo struct {
@@ -50,10 +50,26 @@ type ResponseBase struct {
5050
}
5151

5252
func NewResponseBase(resp *http.Response) ResponseBase {
53-
return ResponseBase{
53+
rb := ResponseBase{
5454
StatusCode: resp.StatusCode,
5555
RateLimitInfo: NewRateLimitInfo(resp),
5656
}
57+
if !rb.Ok() {
58+
RespToJson(resp, &rb)
59+
}
60+
return rb
61+
}
62+
63+
// 根据请求返回的 HTTP 状态码判断推送是否成功
64+
// 规范:
65+
// - 200 一定是正确。所有异常都不使用 200 返回码
66+
// - 业务逻辑上的错误,有特别的错误码尽量使用 4xx,否则使用 400。
67+
// - 服务器端内部错误,无特别错误码使用 500。
68+
// - 业务异常时,返回内容使用 JSON 格式定义 error 信息。
69+
//
70+
// 更多细节: http://docs.jpush.io/server/http_status_code/
71+
func (rb *ResponseBase) Ok() bool {
72+
return rb.StatusCode == http.StatusOK
5773
}
5874

5975
func (rb *ResponseBase) String() string {

Diff for: common/util.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
func BasicAuth(username, password string) string {
1313
auth := username + ":" + password
14-
return base64.StdEncoding.EncodeToString([]byte(auth))
14+
return "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
1515
}
1616

1717
func RespToJson(resp *http.Response, dest interface{}) error {
@@ -20,10 +20,18 @@ func RespToJson(resp *http.Response, dest interface{}) error {
2020
if err != nil {
2121
return err
2222
}
23-
23+
println(string(body))
2424
return json.Unmarshal(body, &dest)
2525
}
2626

27+
func ResponseOrError(resp *http.Response, err error) (*ResponseBase, error) {
28+
if err != nil {
29+
return nil, err
30+
}
31+
ret := NewResponseBase(resp)
32+
return &ret, nil
33+
}
34+
2735
func GetIntHeader(resp *http.Response, key string) (int, error) {
2836
v := resp.Header.Get(key)
2937
return strconv.Atoi(v)

Diff for: device/alias.go

+26
Original file line numberDiff line numberDiff line change
@@ -1 +1,27 @@
11
package device
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
"github.com/DeanThompson/jpush-api-go-client/common"
8+
)
9+
10+
type GetAliasUsersResult struct {
11+
common.ResponseBase
12+
13+
RegistrationIds []string `json:"registration_ids"`
14+
}
15+
16+
func (result *GetAliasUsersResult) FromResponse(resp *http.Response) error {
17+
result.ResponseBase = common.NewResponseBase(resp)
18+
if !result.Ok() {
19+
return nil
20+
}
21+
return common.RespToJson(resp, &result)
22+
}
23+
24+
func (result *GetAliasUsersResult) String() string {
25+
return fmt.Sprintf("<GetAliasUsersResult> RegistrationIds: %v, %v",
26+
result.RegistrationIds, result.ResponseBase)
27+
}

Diff for: device/device.go

+22-17
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,24 @@ import (
88
"github.com/DeanThompson/jpush-api-go-client/common"
99
)
1010

11-
// 设备的所有属性,包含tags, alias
12-
type DeviceInfo struct {
11+
type QueryDeviceResult struct {
12+
common.ResponseBase
13+
14+
// 设备的所有属性,包含tags, alias
1315
Tags []string `json:"tags"`
1416
Alias string `json:"alias"`
1517
}
1618

17-
type DeviceInfoResult struct {
18-
common.ResponseBase
19-
DeviceInfo
20-
}
21-
22-
func (dir *DeviceInfoResult) FromResponse(resp *http.Response) error {
23-
err := common.RespToJson(resp, &dir)
24-
if err != nil {
25-
return err
26-
}
19+
func (dir *QueryDeviceResult) FromResponse(resp *http.Response) error {
2720
dir.ResponseBase = common.NewResponseBase(resp)
28-
return nil
21+
if !dir.Ok() {
22+
return nil
23+
}
24+
return common.RespToJson(resp, &dir)
2925
}
3026

31-
func (dir *DeviceInfoResult) String() string {
32-
return fmt.Sprintf("<DeviceInfoResult> tags: %v, alias: %s, %v",
27+
func (dir *QueryDeviceResult) String() string {
28+
return fmt.Sprintf("<QueryDeviceResult> tags: %v, alias: \"%s\", %v",
3329
dir.Tags, dir.Alias, dir.ResponseBase.String())
3430
}
3531

@@ -49,11 +45,15 @@ type DeviceUpdate struct {
4945

5046
// 更新设备的别名属性;当别名为空串时,删除指定设备的别名;
5147
Alias string
48+
49+
// 手机号码
50+
Mobile string
5251
}
5352

5453
type deviceUpdateWrapper struct {
55-
Tags interface{} `json:"tags"`
56-
Alias string `json:"alias"`
54+
Tags interface{} `json:"tags"`
55+
Alias string `json:"alias"`
56+
Mobile string `json:"mobile"`
5757
}
5858

5959
func NewDeviceUpdate() *DeviceUpdate {
@@ -70,6 +70,7 @@ func (du *DeviceUpdate) MarshalJSON() ([]byte, error) {
7070
wrapper.Tags = du.Tags
7171
}
7272
wrapper.Alias = du.Alias
73+
wrapper.Mobile = du.Mobile
7374
return json.Marshal(wrapper)
7475
}
7576

@@ -92,3 +93,7 @@ func (du *DeviceUpdate) ClearAllTags() {
9293
func (du *DeviceUpdate) SetAlias(alias string) {
9394
du.Alias = alias
9495
}
96+
97+
func (du *DeviceUpdate) SetMobile(mobile string) {
98+
du.Mobile = mobile
99+
}

Diff for: device/tag.go

+86
Original file line numberDiff line numberDiff line change
@@ -1 +1,87 @@
11
package device
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
"github.com/DeanThompson/jpush-api-go-client/common"
8+
)
9+
10+
// 查询标签列表请求结果
11+
type GetTagsResult struct {
12+
common.ResponseBase
13+
14+
Tags []string `json:"tags"`
15+
}
16+
17+
func (tir *GetTagsResult) FromResponse(resp *http.Response) error {
18+
tir.ResponseBase = common.NewResponseBase(resp)
19+
if !tir.Ok() {
20+
return nil
21+
}
22+
return common.RespToJson(resp, &tir)
23+
}
24+
25+
func (tir *GetTagsResult) String() string {
26+
return fmt.Sprintf("<GetTagsResult> tags: %v, %v", tir.Tags, tir.ResponseBase.String())
27+
}
28+
29+
// 判断设备与标签的绑定请求结果
30+
type CheckTagUserExistsResult struct {
31+
common.ResponseBase
32+
33+
Result bool `json:"result"`
34+
}
35+
36+
func (result *CheckTagUserExistsResult) FromResponse(resp *http.Response) error {
37+
result.ResponseBase = common.NewResponseBase(resp)
38+
if !result.Ok() {
39+
return nil
40+
}
41+
return common.RespToJson(resp, &result)
42+
}
43+
44+
func (result *CheckTagUserExistsResult) String() string {
45+
return fmt.Sprintf("<CheckTagUserExistsResult> result: %v, %v", result.Result, result.ResponseBase)
46+
}
47+
48+
// 更新标签(与设备的绑定的关系)请求参数
49+
const (
50+
actionAdd = "add"
51+
actionRemove = "remove"
52+
maxAddOrRemoveRegistrationIds = 1000
53+
)
54+
55+
type UpdateTagUsersArgs struct {
56+
RegistrationIds map[string][]string `json:"registration_ids"`
57+
}
58+
59+
func NewUpdateTagUsersArgs() *UpdateTagUsersArgs {
60+
return &UpdateTagUsersArgs{
61+
RegistrationIds: make(map[string][]string),
62+
}
63+
}
64+
65+
func (args *UpdateTagUsersArgs) AddRegistrationIds(ids ...string) {
66+
args.updateRegistrationIds(actionAdd, ids...)
67+
}
68+
69+
func (args *UpdateTagUsersArgs) RemoveRegistrationIds(ids ...string) {
70+
args.updateRegistrationIds(actionRemove, ids...)
71+
}
72+
73+
func (args *UpdateTagUsersArgs) updateRegistrationIds(action string, ids ...string) {
74+
if action != actionAdd && action != actionRemove {
75+
return
76+
}
77+
ids = common.UniqString(ids)
78+
current := args.RegistrationIds[action]
79+
if current == nil {
80+
current = make([]string, 0, len(ids))
81+
}
82+
merged := common.UniqString(append(current, ids...))
83+
if len(merged) > maxAddOrRemoveRegistrationIds {
84+
merged = merged[:maxAddOrRemoveRegistrationIds]
85+
}
86+
args.RegistrationIds[action] = merged
87+
}

Diff for: examples/device.go

-37
This file was deleted.

0 commit comments

Comments
 (0)