Commit e7dfe75e410207b2495a1502e3bf4e5939492a8f

Authored by Yevhen Bondarenko
Committed by GitHub
1 parent a670b772

Feature/rest client (#2428)

* refactored and improvement Rest Client

* refactored Rest Client, made old methods deprecated
@@ -145,6 +145,10 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable { @@ -145,6 +145,10 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable {
145 return response; 145 return response;
146 } 146 }
147 147
  148 + public RestTemplate getRestTemplate() {
  149 + return restTemplate;
  150 + }
  151 +
148 public String getToken() { 152 public String getToken() {
149 return token; 153 return token;
150 } 154 }
@@ -174,170 +178,6 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable { @@ -174,170 +178,6 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable {
174 restTemplate.getInterceptors().add(this); 178 restTemplate.getInterceptors().add(this);
175 } 179 }
176 180
177 - public Optional<Device> findDevice(String name) {  
178 - Map<String, String> params = new HashMap<String, String>();  
179 - params.put("deviceName", name);  
180 - try {  
181 - ResponseEntity<Device> deviceEntity = restTemplate.getForEntity(baseURL + "/api/tenant/devices?deviceName={deviceName}", Device.class, params);  
182 - return Optional.of(deviceEntity.getBody());  
183 - } catch (HttpClientErrorException exception) {  
184 - if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {  
185 - return Optional.empty();  
186 - } else {  
187 - throw exception;  
188 - }  
189 - }  
190 - }  
191 -  
192 - public Optional<Customer> findCustomer(String title) {  
193 - Map<String, String> params = new HashMap<String, String>();  
194 - params.put("customerTitle", title);  
195 - try {  
196 - ResponseEntity<Customer> customerEntity = restTemplate.getForEntity(baseURL + "/api/tenant/customers?customerTitle={customerTitle}", Customer.class, params);  
197 - return Optional.of(customerEntity.getBody());  
198 - } catch (HttpClientErrorException exception) {  
199 - if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {  
200 - return Optional.empty();  
201 - } else {  
202 - throw exception;  
203 - }  
204 - }  
205 - }  
206 -  
207 - public Optional<Asset> findAsset(String name) {  
208 - Map<String, String> params = new HashMap<String, String>();  
209 - params.put("assetName", name);  
210 - try {  
211 - ResponseEntity<Asset> assetEntity = restTemplate.getForEntity(baseURL + "/api/tenant/assets?assetName={assetName}", Asset.class, params);  
212 - return Optional.of(assetEntity.getBody());  
213 - } catch (HttpClientErrorException exception) {  
214 - if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {  
215 - return Optional.empty();  
216 - } else {  
217 - throw exception;  
218 - }  
219 - }  
220 - }  
221 -  
222 - public Optional<JsonNode> getAttributes(String accessToken, String clientKeys, String sharedKeys) {  
223 - Map<String, String> params = new HashMap<>();  
224 - params.put("accessToken", accessToken);  
225 - params.put("clientKeys", clientKeys);  
226 - params.put("sharedKeys", sharedKeys);  
227 - try {  
228 - ResponseEntity<JsonNode> telemetryEntity = restTemplate.getForEntity(baseURL + "/api/v1/{accessToken}/attributes?clientKeys={clientKeys}&sharedKeys={sharedKeys}", JsonNode.class, params);  
229 - return Optional.of(telemetryEntity.getBody());  
230 - } catch (HttpClientErrorException exception) {  
231 - if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {  
232 - return Optional.empty();  
233 - } else {  
234 - throw exception;  
235 - }  
236 - }  
237 - }  
238 -  
239 - public Customer createCustomer(Customer customer) {  
240 - return restTemplate.postForEntity(baseURL + "/api/customer", customer, Customer.class).getBody();  
241 - }  
242 -  
243 - public Customer createCustomer(String title) {  
244 - Customer customer = new Customer();  
245 - customer.setTitle(title);  
246 - return restTemplate.postForEntity(baseURL + "/api/customer", customer, Customer.class).getBody();  
247 - }  
248 -  
249 - public DeviceCredentials updateDeviceCredentials(DeviceId deviceId, String token) {  
250 - DeviceCredentials deviceCredentials = getCredentials(deviceId);  
251 - deviceCredentials.setCredentialsType(DeviceCredentialsType.ACCESS_TOKEN);  
252 - deviceCredentials.setCredentialsId(token);  
253 - return saveDeviceCredentials(deviceCredentials);  
254 - }  
255 -  
256 - public Device createDevice(String name, String type) {  
257 - Device device = new Device();  
258 - device.setName(name);  
259 - device.setType(type);  
260 - return doCreateDevice(device, null);  
261 - }  
262 -  
263 - public Device createDevice(Device device) {  
264 - return doCreateDevice(device, null);  
265 - }  
266 -  
267 - public Device createDevice(Device device, String accessToken) {  
268 - return doCreateDevice(device, accessToken);  
269 - }  
270 -  
271 - private Device doCreateDevice(Device device, String accessToken) {  
272 - Map<String, String> params = new HashMap<>();  
273 - String deviceCreationUrl = "/api/device";  
274 - if (!StringUtils.isEmpty(accessToken)) {  
275 - deviceCreationUrl = deviceCreationUrl + "?accessToken={accessToken}";  
276 - params.put("accessToken", accessToken);  
277 - }  
278 - return restTemplate.postForEntity(baseURL + deviceCreationUrl, device, Device.class, params).getBody();  
279 - }  
280 -  
281 - public Asset createAsset(Asset asset) {  
282 - return restTemplate.postForEntity(baseURL + "/api/asset", asset, Asset.class).getBody();  
283 - }  
284 -  
285 - public Asset createAsset(String name, String type) {  
286 - Asset asset = new Asset();  
287 - asset.setName(name);  
288 - asset.setType(type);  
289 - return restTemplate.postForEntity(baseURL + "/api/asset", asset, Asset.class).getBody();  
290 - }  
291 -  
292 - public Alarm createAlarm(Alarm alarm) {  
293 - return restTemplate.postForEntity(baseURL + "/api/alarm", alarm, Alarm.class).getBody();  
294 - }  
295 -  
296 - public Device assignDevice(CustomerId customerId, DeviceId deviceId) {  
297 - return restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/device/{deviceId}", null, Device.class,  
298 - customerId.toString(), deviceId.toString()).getBody();  
299 - }  
300 -  
301 - public Asset assignAsset(CustomerId customerId, AssetId assetId) {  
302 - return restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/asset/{assetId}", HttpEntity.EMPTY, Asset.class,  
303 - customerId.toString(), assetId.toString()).getBody();  
304 - }  
305 -  
306 - public EntityRelation makeRelation(String relationType, EntityId idFrom, EntityId idTo) {  
307 - EntityRelation relation = new EntityRelation();  
308 - relation.setFrom(idFrom);  
309 - relation.setTo(idTo);  
310 - relation.setType(relationType);  
311 - return restTemplate.postForEntity(baseURL + "/api/relation", relation, EntityRelation.class).getBody();  
312 - }  
313 -  
314 - public Dashboard createDashboard(Dashboard dashboard) {  
315 - return restTemplate.postForEntity(baseURL + "/api/dashboard", dashboard, Dashboard.class).getBody();  
316 - }  
317 -  
318 - public List<DashboardInfo> findTenantDashboards() {  
319 - try {  
320 - ResponseEntity<TextPageData<DashboardInfo>> dashboards =  
321 - restTemplate.exchange(baseURL + "/api/tenant/dashboards?limit=100000", HttpMethod.GET, null, new ParameterizedTypeReference<TextPageData<DashboardInfo>>() {  
322 - });  
323 - return dashboards.getBody().getData();  
324 - } catch (HttpClientErrorException exception) {  
325 - if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {  
326 - return Collections.emptyList();  
327 - } else {  
328 - throw exception;  
329 - }  
330 - }  
331 - }  
332 -  
333 - public DeviceCredentials getCredentials(DeviceId id) {  
334 - return restTemplate.getForEntity(baseURL + "/api/device/" + id.getId().toString() + "/credentials", DeviceCredentials.class).getBody();  
335 - }  
336 -  
337 - public RestTemplate getRestTemplate() {  
338 - return restTemplate;  
339 - }  
340 -  
341 public Optional<AdminSettings> getAdminSettings(String key) { 181 public Optional<AdminSettings> getAdminSettings(String key) {
342 try { 182 try {
343 ResponseEntity<AdminSettings> adminSettings = restTemplate.getForEntity(baseURL + "/api/admin/settings/{key}", AdminSettings.class, key); 183 ResponseEntity<AdminSettings> adminSettings = restTemplate.getForEntity(baseURL + "/api/admin/settings/{key}", AdminSettings.class, key);
@@ -467,6 +307,11 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable { @@ -467,6 +307,11 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable {
467 } 307 }
468 } 308 }
469 309
  310 + @Deprecated
  311 + public Alarm createAlarm(Alarm alarm) {
  312 + return restTemplate.postForEntity(baseURL + "/api/alarm", alarm, Alarm.class).getBody();
  313 + }
  314 +
470 public Optional<Asset> getAssetById(AssetId assetId) { 315 public Optional<Asset> getAssetById(AssetId assetId) {
471 try { 316 try {
472 ResponseEntity<Asset> asset = restTemplate.getForEntity(baseURL + "/api/asset/{assetId}", Asset.class, assetId.getId()); 317 ResponseEntity<Asset> asset = restTemplate.getForEntity(baseURL + "/api/asset/{assetId}", Asset.class, assetId.getId());
@@ -603,6 +448,41 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable { @@ -603,6 +448,41 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable {
603 }).getBody(); 448 }).getBody();
604 } 449 }
605 450
  451 + @Deprecated
  452 + public Optional<Asset> findAsset(String name) {
  453 + Map<String, String> params = new HashMap<String, String>();
  454 + params.put("assetName", name);
  455 + try {
  456 + ResponseEntity<Asset> assetEntity = restTemplate.getForEntity(baseURL + "/api/tenant/assets?assetName={assetName}", Asset.class, params);
  457 + return Optional.of(assetEntity.getBody());
  458 + } catch (HttpClientErrorException exception) {
  459 + if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
  460 + return Optional.empty();
  461 + } else {
  462 + throw exception;
  463 + }
  464 + }
  465 + }
  466 +
  467 + @Deprecated
  468 + public Asset createAsset(Asset asset) {
  469 + return restTemplate.postForEntity(baseURL + "/api/asset", asset, Asset.class).getBody();
  470 + }
  471 +
  472 + @Deprecated
  473 + public Asset createAsset(String name, String type) {
  474 + Asset asset = new Asset();
  475 + asset.setName(name);
  476 + asset.setType(type);
  477 + return restTemplate.postForEntity(baseURL + "/api/asset", asset, Asset.class).getBody();
  478 + }
  479 +
  480 + @Deprecated
  481 + public Asset assignAsset(CustomerId customerId, AssetId assetId) {
  482 + return restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/asset/{assetId}", HttpEntity.EMPTY, Asset.class,
  483 + customerId.toString(), assetId.toString()).getBody();
  484 + }
  485 +
606 public TimePageData<AuditLog> getAuditLogsByCustomerId(CustomerId customerId, TimePageLink pageLink, List<ActionType> actionTypes) { 486 public TimePageData<AuditLog> getAuditLogsByCustomerId(CustomerId customerId, TimePageLink pageLink, List<ActionType> actionTypes) {
607 Map<String, String> params = new HashMap<>(); 487 Map<String, String> params = new HashMap<>();
608 params.put("customerId", customerId.getId().toString()); 488 params.put("customerId", customerId.getId().toString());
@@ -826,6 +706,34 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable { @@ -826,6 +706,34 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable {
826 } 706 }
827 } 707 }
828 708
  709 + @Deprecated
  710 + public Optional<Customer> findCustomer(String title) {
  711 + Map<String, String> params = new HashMap<>();
  712 + params.put("customerTitle", title);
  713 + try {
  714 + ResponseEntity<Customer> customerEntity = restTemplate.getForEntity(baseURL + "/api/tenant/customers?customerTitle={customerTitle}", Customer.class, params);
  715 + return Optional.of(customerEntity.getBody());
  716 + } catch (HttpClientErrorException exception) {
  717 + if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
  718 + return Optional.empty();
  719 + } else {
  720 + throw exception;
  721 + }
  722 + }
  723 + }
  724 +
  725 + @Deprecated
  726 + public Customer createCustomer(Customer customer) {
  727 + return restTemplate.postForEntity(baseURL + "/api/customer", customer, Customer.class).getBody();
  728 + }
  729 +
  730 + @Deprecated
  731 + public Customer createCustomer(String title) {
  732 + Customer customer = new Customer();
  733 + customer.setTitle(title);
  734 + return restTemplate.postForEntity(baseURL + "/api/customer", customer, Customer.class).getBody();
  735 + }
  736 +
829 public Long getServerTime() { 737 public Long getServerTime() {
830 return restTemplate.getForObject(baseURL + "/api/dashboard/serverTime", Long.class); 738 return restTemplate.getForObject(baseURL + "/api/dashboard/serverTime", Long.class);
831 } 739 }
@@ -994,6 +902,27 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable { @@ -994,6 +902,27 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable {
994 }, params).getBody(); 902 }, params).getBody();
995 } 903 }
996 904
  905 + @Deprecated
  906 + public Dashboard createDashboard(Dashboard dashboard) {
  907 + return restTemplate.postForEntity(baseURL + "/api/dashboard", dashboard, Dashboard.class).getBody();
  908 + }
  909 +
  910 + @Deprecated
  911 + public List<DashboardInfo> findTenantDashboards() {
  912 + try {
  913 + ResponseEntity<TextPageData<DashboardInfo>> dashboards =
  914 + restTemplate.exchange(baseURL + "/api/tenant/dashboards?limit=100000", HttpMethod.GET, null, new ParameterizedTypeReference<TextPageData<DashboardInfo>>() {
  915 + });
  916 + return dashboards.getBody().getData();
  917 + } catch (HttpClientErrorException exception) {
  918 + if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
  919 + return Collections.emptyList();
  920 + } else {
  921 + throw exception;
  922 + }
  923 + }
  924 + }
  925 +
997 public Optional<Device> getDeviceById(DeviceId deviceId) { 926 public Optional<Device> getDeviceById(DeviceId deviceId) {
998 try { 927 try {
999 ResponseEntity<Device> device = restTemplate.getForEntity(baseURL + "/api/device/{deviceId}", Device.class, deviceId.getId()); 928 ResponseEntity<Device> device = restTemplate.getForEntity(baseURL + "/api/device/{deviceId}", Device.class, deviceId.getId());
@@ -1145,6 +1074,70 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable { @@ -1145,6 +1074,70 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable {
1145 restTemplate.delete(baseURL + "/api/customer/device/{deviceName}/claim", deviceName); 1074 restTemplate.delete(baseURL + "/api/customer/device/{deviceName}/claim", deviceName);
1146 } 1075 }
1147 1076
  1077 + @Deprecated
  1078 + public Device createDevice(String name, String type) {
  1079 + Device device = new Device();
  1080 + device.setName(name);
  1081 + device.setType(type);
  1082 + return doCreateDevice(device, null);
  1083 + }
  1084 +
  1085 + @Deprecated
  1086 + public Device createDevice(Device device) {
  1087 + return doCreateDevice(device, null);
  1088 + }
  1089 +
  1090 + @Deprecated
  1091 + public Device createDevice(Device device, String accessToken) {
  1092 + return doCreateDevice(device, accessToken);
  1093 + }
  1094 +
  1095 + @Deprecated
  1096 + private Device doCreateDevice(Device device, String accessToken) {
  1097 + Map<String, String> params = new HashMap<>();
  1098 + String deviceCreationUrl = "/api/device";
  1099 + if (!StringUtils.isEmpty(accessToken)) {
  1100 + deviceCreationUrl = deviceCreationUrl + "?accessToken={accessToken}";
  1101 + params.put("accessToken", accessToken);
  1102 + }
  1103 + return restTemplate.postForEntity(baseURL + deviceCreationUrl, device, Device.class, params).getBody();
  1104 + }
  1105 +
  1106 + @Deprecated
  1107 + public DeviceCredentials getCredentials(DeviceId id) {
  1108 + return restTemplate.getForEntity(baseURL + "/api/device/" + id.getId().toString() + "/credentials", DeviceCredentials.class).getBody();
  1109 + }
  1110 +
  1111 + @Deprecated
  1112 + public Optional<Device> findDevice(String name) {
  1113 + Map<String, String> params = new HashMap<>();
  1114 + params.put("deviceName", name);
  1115 + try {
  1116 + ResponseEntity<Device> deviceEntity = restTemplate.getForEntity(baseURL + "/api/tenant/devices?deviceName={deviceName}", Device.class, params);
  1117 + return Optional.of(deviceEntity.getBody());
  1118 + } catch (HttpClientErrorException exception) {
  1119 + if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
  1120 + return Optional.empty();
  1121 + } else {
  1122 + throw exception;
  1123 + }
  1124 + }
  1125 + }
  1126 +
  1127 + @Deprecated
  1128 + public DeviceCredentials updateDeviceCredentials(DeviceId deviceId, String token) {
  1129 + DeviceCredentials deviceCredentials = getCredentials(deviceId);
  1130 + deviceCredentials.setCredentialsType(DeviceCredentialsType.ACCESS_TOKEN);
  1131 + deviceCredentials.setCredentialsId(token);
  1132 + return saveDeviceCredentials(deviceCredentials);
  1133 + }
  1134 +
  1135 + @Deprecated
  1136 + public Device assignDevice(CustomerId customerId, DeviceId deviceId) {
  1137 + return restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/device/{deviceId}", null, Device.class,
  1138 + customerId.toString(), deviceId.toString()).getBody();
  1139 + }
  1140 +
1148 public void saveRelation(EntityRelation relation) { 1141 public void saveRelation(EntityRelation relation) {
1149 restTemplate.postForLocation(baseURL + "/api/relation", null); 1142 restTemplate.postForLocation(baseURL + "/api/relation", null);
1150 } 1143 }
@@ -1298,6 +1291,15 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable { @@ -1298,6 +1291,15 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable {
1298 }).getBody(); 1291 }).getBody();
1299 } 1292 }
1300 1293
  1294 + @Deprecated
  1295 + public EntityRelation makeRelation(String relationType, EntityId idFrom, EntityId idTo) {
  1296 + EntityRelation relation = new EntityRelation();
  1297 + relation.setFrom(idFrom);
  1298 + relation.setTo(idTo);
  1299 + relation.setType(relationType);
  1300 + return restTemplate.postForEntity(baseURL + "/api/relation", relation, EntityRelation.class).getBody();
  1301 + }
  1302 +
1301 public Optional<EntityView> getEntityViewById(EntityViewId entityViewId) { 1303 public Optional<EntityView> getEntityViewById(EntityViewId entityViewId) {
1302 try { 1304 try {
1303 ResponseEntity<EntityView> entityView = restTemplate.getForEntity(baseURL + "/api/entityView/{entityViewId}", EntityView.class, entityViewId.getId()); 1305 ResponseEntity<EntityView> entityView = restTemplate.getForEntity(baseURL + "/api/entityView/{entityViewId}", EntityView.class, entityViewId.getId());
@@ -1967,6 +1969,24 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable { @@ -1967,6 +1969,24 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable {
1967 } 1969 }
1968 } 1970 }
1969 1971
  1972 + @Deprecated
  1973 + public Optional<JsonNode> getAttributes(String accessToken, String clientKeys, String sharedKeys) {
  1974 + Map<String, String> params = new HashMap<>();
  1975 + params.put("accessToken", accessToken);
  1976 + params.put("clientKeys", clientKeys);
  1977 + params.put("sharedKeys", sharedKeys);
  1978 + try {
  1979 + ResponseEntity<JsonNode> telemetryEntity = restTemplate.getForEntity(baseURL + "/api/v1/{accessToken}/attributes?clientKeys={clientKeys}&sharedKeys={sharedKeys}", JsonNode.class, params);
  1980 + return Optional.of(telemetryEntity.getBody());
  1981 + } catch (HttpClientErrorException exception) {
  1982 + if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
  1983 + return Optional.empty();
  1984 + } else {
  1985 + throw exception;
  1986 + }
  1987 + }
  1988 + }
  1989 +
1970 private String getUrlParams(TimePageLink pageLink) { 1990 private String getUrlParams(TimePageLink pageLink) {
1971 String urlParams = "limit={limit}&ascOrder={ascOrder}"; 1991 String urlParams = "limit={limit}&ascOrder={ascOrder}";
1972 if (pageLink.getStartTime() != null) { 1992 if (pageLink.getStartTime() != null) {
@@ -2042,4 +2062,5 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable { @@ -2042,4 +2062,5 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable {
2042 service.shutdown(); 2062 service.shutdown();
2043 } 2063 }
2044 } 2064 }
  2065 +
2045 } 2066 }