|
| 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