|
| 1 | +package com.qiniu.linking; |
| 2 | + |
| 3 | +import com.qiniu.common.Constants; |
| 4 | +import com.qiniu.common.QiniuException; |
| 5 | +import com.qiniu.http.Client; |
| 6 | +import com.qiniu.http.Response; |
| 7 | +import com.qiniu.linking.model.*; |
| 8 | +import com.qiniu.util.*; |
| 9 | + |
| 10 | +public class LinkingDeviceManager { |
| 11 | + |
| 12 | + |
| 13 | + private final Auth auth; |
| 14 | + private final String host; |
| 15 | + private final Client client; |
| 16 | + |
| 17 | + |
| 18 | + public LinkingDeviceManager(Auth auth) { |
| 19 | + this(auth, "http://linking.qiniuapi.com"); |
| 20 | + } |
| 21 | + |
| 22 | + public LinkingDeviceManager(Auth auth, String host) { |
| 23 | + this(auth, host, new Client()); |
| 24 | + } |
| 25 | + |
| 26 | + public LinkingDeviceManager(Auth auth, String host, Client client) { |
| 27 | + this.auth = auth; |
| 28 | + this.host = host; |
| 29 | + this.client = client; |
| 30 | + } |
| 31 | + |
| 32 | + public void createDevice(String appid, String deviceName) throws QiniuException { |
| 33 | + StringMap params = new StringMap().put("device", deviceName); |
| 34 | + String url = String.format("%s/v1/apps/%s/devices", host, appid); |
| 35 | + Response res = post(url, params); |
| 36 | + res.close(); |
| 37 | + } |
| 38 | + |
| 39 | + public void deleteDevice(String appid, String deviceName) throws QiniuException { |
| 40 | + String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName); |
| 41 | + String url = String.format("%s/v1/apps/%s/devices/%s", host, appid, encodedDeviceName); |
| 42 | + StringMap headers = auth.authorizationV2(url, "DELETE", null, null); |
| 43 | + Response res = client.delete(url, headers); |
| 44 | + if (!res.isOK()) { |
| 45 | + throw new QiniuException(res); |
| 46 | + } |
| 47 | + res.close(); |
| 48 | + } |
| 49 | + |
| 50 | + public DeviceListing listDevice(String appid, String prefix, |
| 51 | + String marker, int limit, boolean online) throws QiniuException { |
| 52 | + StringMap map = new StringMap().putNotEmpty("marker", marker) |
| 53 | + .putNotEmpty("prefix", prefix).putWhen("limit", limit, limit > 0) |
| 54 | + .putWhen("online", online, online); |
| 55 | + String queryString = map.formString(); |
| 56 | + if (map.size()>0){ |
| 57 | + queryString="?"+queryString; |
| 58 | + } |
| 59 | + String url = String.format("%s/v1/apps/%s/devices%s", host, appid, queryString); |
| 60 | + Response res = get(url); |
| 61 | + DeviceListing ret = res.jsonToObject(DeviceListing.class); |
| 62 | + res.close(); |
| 63 | + return ret; |
| 64 | + } |
| 65 | + |
| 66 | + private Response get(String url) throws QiniuException { |
| 67 | + StringMap headers = auth.authorizationV2(url, "GET", null, null); |
| 68 | + Response res = client.get(url, headers); |
| 69 | + if (!res.isOK()) { |
| 70 | + throw new QiniuException(res); |
| 71 | + } |
| 72 | + return res; |
| 73 | + } |
| 74 | + |
| 75 | + public Device getDevice(String appid, String deviceName) throws QiniuException { |
| 76 | + String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName); |
| 77 | + String url = String.format("%s/v1/apps/%s/devices/%s", host, appid, encodedDeviceName); |
| 78 | + Response res = get(url); |
| 79 | + Device ret = res.jsonToObject(Device.class); |
| 80 | + res.close(); |
| 81 | + return ret; |
| 82 | + } |
| 83 | + |
| 84 | + public Device getDeviceByAccessKey(String deviceAccessKey) throws QiniuException { |
| 85 | + String url = String.format("%s/v1/keys/%s", host, deviceAccessKey); |
| 86 | + Response res = get(url); |
| 87 | + Device ret = res.jsonToObject(Device.class); |
| 88 | + res.close(); |
| 89 | + return ret; |
| 90 | + } |
| 91 | + |
| 92 | + public Device updateDevice(String appid, String deviceName, PatchOperation[] operations) throws QiniuException { |
| 93 | + String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName); |
| 94 | + StringMap params = new StringMap().put("operations", operations); |
| 95 | + String url = String.format("%s/v1/apps/%s/devices/%s", host, appid, encodedDeviceName); |
| 96 | + byte[] body = Json.encode(params).getBytes(Constants.UTF_8); |
| 97 | + StringMap headers = auth.authorizationV2(url, "PATCH", body, Client.JsonMime); |
| 98 | + Response res = client.patch(url, body, headers, Client.JsonMime); |
| 99 | + if (!res.isOK()) { |
| 100 | + throw new QiniuException(res); |
| 101 | + } |
| 102 | + Device ret = res.jsonToObject(Device.class); |
| 103 | + res.close(); |
| 104 | + return ret; |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + public DeviceHistoryListing listDeviceHistory(String appid, String deviceName, |
| 109 | + long start, long end, String marker, int limit) throws QiniuException { |
| 110 | + String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName); |
| 111 | + StringMap map = new StringMap().putNotEmpty("marker", marker). |
| 112 | + put("start", start).put("end", end).putWhen("limit", limit, limit > 0); |
| 113 | + String queryString = map.formString(); |
| 114 | + String url = String.format("%s/v1/apps/%s/devices/%s/historyactivity?%s", |
| 115 | + host, appid, encodedDeviceName, queryString); |
| 116 | + Response res = get(url); |
| 117 | + DeviceHistoryListing ret = res.jsonToObject(DeviceHistoryListing.class); |
| 118 | + res.close(); |
| 119 | + return ret; |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | + private class DeviceKeyRet { |
| 124 | + DeviceKey[] keys; |
| 125 | + } |
| 126 | + |
| 127 | + public DeviceKey[] addDeviceKey(String appid, String deviceName) throws QiniuException { |
| 128 | + String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName); |
| 129 | + String url = String.format("%s/v1/apps/%s/devices/%s/keys", host, appid, encodedDeviceName); |
| 130 | + Response res = post(url, null); |
| 131 | + DeviceKeyRet ret = res.jsonToObject(DeviceKeyRet.class); |
| 132 | + res.close(); |
| 133 | + if (ret != null) { |
| 134 | + return ret.keys; |
| 135 | + } |
| 136 | + return null; |
| 137 | + } |
| 138 | + |
| 139 | + public DeviceKey[] queryDeviceKey(String appid, String deviceName) throws QiniuException { |
| 140 | + String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName); |
| 141 | + String url = String.format("%s/v1/apps/%s/devices/%s/keys", host, appid, encodedDeviceName); |
| 142 | + Response res = get(url); |
| 143 | + DeviceKeyRet ret = res.jsonToObject(DeviceKeyRet.class); |
| 144 | + res.close(); |
| 145 | + if (ret != null) { |
| 146 | + return ret.keys; |
| 147 | + } |
| 148 | + return null; |
| 149 | + } |
| 150 | + |
| 151 | + public void deleteDeviceKey(String appid, String deviceName, String accessKey) throws QiniuException { |
| 152 | + String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName); |
| 153 | + String url = String.format("%s/v1/apps/%s/devices/%s/keys/%s", host, appid, encodedDeviceName, accessKey); |
| 154 | + StringMap headers = auth.authorizationV2(url, "DELETE", null, null); |
| 155 | + Response res = client.delete(url, headers); |
| 156 | + if (!res.isOK()) { |
| 157 | + throw new QiniuException(res); |
| 158 | + } |
| 159 | + DeviceKeyRet ret = res.jsonToObject(DeviceKeyRet.class); |
| 160 | + res.close(); |
| 161 | + } |
| 162 | + |
| 163 | + public void updateDeviceKeyState(String appid, String deviceName, |
| 164 | + String accessKey, int state) throws QiniuException { |
| 165 | + String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName); |
| 166 | + StringMap params = new StringMap().put("state", state); |
| 167 | + String url = String.format("%s/v1/apps/%s/devices/%s/keys/%s/state", host, appid, encodedDeviceName, accessKey); |
| 168 | + Response res = post(url, params); |
| 169 | + res.close(); |
| 170 | + } |
| 171 | + |
| 172 | + public DeviceKey[] cloneDeviceKey(String appid, String fromDeviceName, |
| 173 | + String toDeviceName, boolean cleanSelfKeys, |
| 174 | + boolean deleteDevice, String deviceAccessKey) throws QiniuException { |
| 175 | + String encodedFromDeviceName = UrlSafeBase64.encodeToString(fromDeviceName); |
| 176 | + String encodedToDeviceName = UrlSafeBase64.encodeToString(toDeviceName); |
| 177 | + String url = String.format("%s/v1/apps/%s/devices/%s/keys/clone", host, appid, encodedToDeviceName); |
| 178 | + StringMap params = new StringMap().put("fromDevice", fromDeviceName).put("cleanSelfKeys", cleanSelfKeys). |
| 179 | + put("deleteDevice", deleteDevice).put("deviceAccessKey", deviceAccessKey); |
| 180 | + |
| 181 | + Response res = post(url, params); |
| 182 | + DeviceKeyRet ret = res.jsonToObject(DeviceKeyRet.class); |
| 183 | + res.close(); |
| 184 | + if (ret != null) { |
| 185 | + return ret.keys; |
| 186 | + |
| 187 | + } |
| 188 | + return null; |
| 189 | + } |
| 190 | + |
| 191 | + private Response post(String url, StringMap params) throws QiniuException { |
| 192 | + byte[] body; |
| 193 | + String contentType = null; |
| 194 | + if (params == null) { |
| 195 | + body = null; |
| 196 | + } else { |
| 197 | + contentType = Client.JsonMime; |
| 198 | + body = Json.encode(params).getBytes(Constants.UTF_8); |
| 199 | + } |
| 200 | + StringMap headers = auth.authorizationV2(url, "POST", body, contentType); |
| 201 | + Response res = client.post(url, body, headers, Client.JsonMime); |
| 202 | + if (!res.isOK()) { |
| 203 | + throw new QiniuException(res); |
| 204 | + } |
| 205 | + return res; |
| 206 | + } |
| 207 | +} |
0 commit comments