Skip to content

Commit 5bf3f94

Browse files
author
Javen
committed
Ongoing: add Device API support.
1 parent 61f2482 commit 5bf3f94

File tree

6 files changed

+248
-0
lines changed

6 files changed

+248
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package cn.jpush.api.common;
2+
3+
public class NormalResult extends BaseResult {
4+
5+
6+
}

src/cn/jpush/api/common/ServiceHelper.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package cn.jpush.api.common;
22

33
import java.util.Random;
4+
import java.util.Set;
45
import java.util.regex.Pattern;
56

67
import cn.jpush.api.utils.Base64;
78
import cn.jpush.api.utils.StringUtils;
89

10+
import com.google.gson.JsonArray;
11+
import com.google.gson.JsonPrimitive;
12+
913
public class ServiceHelper {
1014

1115
private final static Pattern PUSH_PATTERNS = Pattern.compile("[^a-zA-Z0-9]");
@@ -48,5 +52,15 @@ public static void checkBasic(String appKey, String masterSecret) {
4852
+ "Please confirm that they are coming from JPush Web Portal.");
4953
}
5054
}
55+
56+
public static JsonArray fromSet(Set<String> sets) {
57+
JsonArray array = new JsonArray();
58+
if (null != sets && sets.size() > 0) {
59+
for (String item : sets) {
60+
array.add(new JsonPrimitive(item));
61+
}
62+
}
63+
return array;
64+
}
5165

5266
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.jpush.api.tags;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import cn.jpush.api.common.BaseResult;
7+
8+
import com.google.gson.annotations.Expose;
9+
10+
public class AliasDeviceListResult extends BaseResult {
11+
@Expose public List<RegistrationId> registration_ids = new ArrayList<RegistrationId>();
12+
13+
public static class RegistrationId {
14+
@Expose public String id;
15+
}
16+
17+
}
18+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cn.jpush.api.tags;
2+
3+
import java.util.List;
4+
5+
import cn.jpush.api.common.BaseResult;
6+
7+
import com.google.gson.annotations.Expose;
8+
9+
public class TagAliasResult extends BaseResult {
10+
11+
@Expose public List<Tag> tag;
12+
@Expose public String alias;
13+
14+
public static class Tag {
15+
@Expose public String tag;
16+
}
17+
18+
}
19+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cn.jpush.api.tags;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import cn.jpush.api.common.BaseResult;
7+
8+
import com.google.gson.annotations.Expose;
9+
10+
public class TagListResult extends BaseResult {
11+
12+
@Expose public List<TagPlatform> tags = new ArrayList<TagPlatform>();
13+
14+
public static class TagPlatform {
15+
@Expose public Android android;
16+
@Expose public Ios ios;
17+
}
18+
19+
public static class Android {
20+
@Expose public String tag;
21+
}
22+
23+
public static class Ios {
24+
@Expose public String tag;
25+
}
26+
27+
}
28+
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package cn.jpush.api.tags;
2+
3+
import java.util.Set;
4+
5+
import cn.jpush.api.common.APIConnectionException;
6+
import cn.jpush.api.common.APIRequestException;
7+
import cn.jpush.api.common.BaseResult;
8+
import cn.jpush.api.common.HttpProxy;
9+
import cn.jpush.api.common.IHttpClient;
10+
import cn.jpush.api.common.NativeHttpClient;
11+
import cn.jpush.api.common.NormalResult;
12+
import cn.jpush.api.common.ResponseWrapper;
13+
import cn.jpush.api.common.ServiceHelper;
14+
import cn.jpush.api.utils.StringUtils;
15+
16+
import com.google.gson.JsonArray;
17+
import com.google.gson.JsonObject;
18+
import com.google.gson.JsonPrimitive;
19+
20+
public class TagsClient {
21+
public static final String HOST_NAME_SSL = "https://devices.jpush.cn";
22+
public static final String DEVICE_PATH = "/v3/device";
23+
public static final String TAG_PATH = "/v3/tag";
24+
public static final String ALIAS_PATH = "/v3/alias";
25+
26+
private final NativeHttpClient _httpClient;
27+
28+
public TagsClient(String masterSecret, String appKey) {
29+
this(masterSecret, appKey, IHttpClient.DEFAULT_MAX_RETRY_TIMES);
30+
}
31+
32+
public TagsClient(String masterSecret, String appKey, int maxRetryTimes) {
33+
this(masterSecret, appKey, maxRetryTimes, null);
34+
}
35+
36+
public TagsClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) {
37+
ServiceHelper.checkBasic(appKey, masterSecret);
38+
39+
String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
40+
this._httpClient = new NativeHttpClient(authCode, maxRetryTimes, proxy);
41+
}
42+
43+
44+
// -------------- device
45+
46+
public TagAliasResult getDeviceTagAlias(String registrationId) throws APIConnectionException, APIRequestException {
47+
String url = HOST_NAME_SSL + DEVICE_PATH + "/" + registrationId;
48+
49+
ResponseWrapper response = _httpClient.sendGet(url);
50+
51+
return BaseResult.fromResponse(response, TagAliasResult.class);
52+
}
53+
54+
public NormalResult updateDeviceTagAlias(String registrationId, String alias, boolean clearTag,
55+
Set<String> tagsToAdd, Set<String> tagsToRemove) throws APIConnectionException, APIRequestException {
56+
String url = HOST_NAME_SSL + DEVICE_PATH + "/" + registrationId;
57+
58+
JsonObject top = new JsonObject();
59+
if (null != alias) {
60+
top.addProperty("alias", alias);
61+
}
62+
if (clearTag) {
63+
top.addProperty("tag", "");
64+
} else {
65+
JsonObject tagObject = new JsonObject();
66+
JsonArray tagsAdd = ServiceHelper.fromSet(tagsToAdd);
67+
if (tagsAdd.size() > 0) {
68+
tagObject.add("add", tagsAdd);
69+
}
70+
71+
JsonArray tagsRemove = ServiceHelper.fromSet(tagsToRemove);
72+
if (tagsRemove.size() > 0) {
73+
tagObject.add("remove", tagsRemove);
74+
}
75+
76+
if (tagObject.entrySet().size() > 0) {
77+
top.add("tag", tagObject);
78+
}
79+
}
80+
81+
ResponseWrapper response = _httpClient.sendPost(url, top.toString());
82+
83+
return BaseResult.fromResponse(response, NormalResult.class);
84+
}
85+
86+
87+
// ------------- tags
88+
89+
public TagListResult getTagList(String platform) throws APIConnectionException, APIRequestException {
90+
String url = HOST_NAME_SSL + TAG_PATH + "/list";
91+
if (!StringUtils.isEmpty(platform)) {
92+
url += "?platform=" + platform;
93+
}
94+
95+
ResponseWrapper response = _httpClient.sendGet(url);
96+
97+
return BaseResult.fromResponse(response, TagListResult.class);
98+
}
99+
100+
public NormalResult addRemoveDevicesFromTag(String theTag, Set<String> toAddUsers, Set<String> toRemoveUsers) throws APIConnectionException, APIRequestException {
101+
String url = HOST_NAME_SSL + TAG_PATH + "/" + theTag;
102+
103+
JsonObject top = new JsonObject();
104+
JsonObject registrationIds = new JsonObject();
105+
106+
if (null != toAddUsers && toAddUsers.size() > 0) {
107+
JsonArray array = new JsonArray();
108+
for (String user : toAddUsers) {
109+
array.add(new JsonPrimitive(user));
110+
}
111+
registrationIds.add("add", array);
112+
}
113+
if (null != toRemoveUsers && toRemoveUsers.size() > 0) {
114+
JsonArray array = new JsonArray();
115+
for (String user : toRemoveUsers) {
116+
array.add(new JsonPrimitive(user));
117+
}
118+
registrationIds.add("remove", array);
119+
}
120+
121+
top.add("registration_ids", registrationIds);
122+
123+
ResponseWrapper response = _httpClient.sendPost(url, top.toString());
124+
125+
return BaseResult.fromResponse(response, NormalResult.class);
126+
}
127+
128+
public NormalResult deleteTag(String theTag) throws APIConnectionException, APIRequestException {
129+
String url = HOST_NAME_SSL + TAG_PATH + "/" + theTag;
130+
131+
ResponseWrapper response = _httpClient.sendDelete(url);
132+
133+
return BaseResult.fromResponse(response, NormalResult.class);
134+
}
135+
136+
public NormalResult isDeviceInTag(String theTag, String registrationID) throws APIConnectionException, APIRequestException {
137+
String url = HOST_NAME_SSL + TAG_PATH + "/" + theTag + "/exist";
138+
139+
ResponseWrapper response = _httpClient.sendGet(url);
140+
141+
return BaseResult.fromResponse(response, NormalResult.class);
142+
}
143+
144+
145+
// ------------- alias
146+
147+
public AliasDeviceListResult getAliasDeviceList(String alias) throws APIConnectionException, APIRequestException {
148+
String url = HOST_NAME_SSL + ALIAS_PATH + "/" + alias;
149+
150+
ResponseWrapper response = _httpClient.sendGet(url);
151+
152+
return BaseResult.fromResponse(response, AliasDeviceListResult.class);
153+
}
154+
155+
public NormalResult deleteAlias(String alias) throws APIConnectionException, APIRequestException {
156+
String url = HOST_NAME_SSL + ALIAS_PATH + "/" + alias;
157+
158+
ResponseWrapper response = _httpClient.sendDelete(url);
159+
160+
return BaseResult.fromResponse(response, NormalResult.class);
161+
}
162+
163+
}

0 commit comments

Comments
 (0)