Feature/rest client (#2368)

* refactored URLs

* refactored

* refactored

* refactored

* refactored

* refactored rest client

* changed executorService from RestClient

* refactored rest client and JsonConverter

* refactored rest client
This commit is contained in:
Yevhen Bondarenko 2020-01-29 17:25:55 +02:00 committed by GitHub
parent 23919b3d5f
commit fbed56555f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1690,77 +1690,67 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable {
return RestJsonConverter.toTimeseries(timeseries); return RestJsonConverter.toTimeseries(timeseries);
} }
public List<AttributeKvEntry> saveDeviceAttributes(String deviceId, String scope, JsonNode request) { public boolean saveDeviceAttributes(DeviceId deviceId, String scope, JsonNode request) {
List<JsonNode> attributes = restTemplate.exchange( return restTemplate
baseURL + "/api/plugins/telemetry/{deviceId}/{scope}", .postForEntity(baseURL + "/api/plugins/telemetry/{deviceId}/{scope}", request, Object.class, deviceId.getId().toString(), scope)
HttpMethod.POST, .getStatusCode()
new HttpEntity<>(request), .is2xxSuccessful();
new ParameterizedTypeReference<List<JsonNode>>() {
},
deviceId,
scope).getBody();
return RestJsonConverter.toAttributes(attributes);
} }
public List<AttributeKvEntry> saveEntityAttributesV1(EntityId entityId, String scope, JsonNode request) { public boolean saveEntityAttributesV1(EntityId entityId, String scope, JsonNode request) {
List<JsonNode> attributes = restTemplate.exchange( return restTemplate
.postForEntity(
baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/{scope}", baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/{scope}",
HttpMethod.POST, request,
new HttpEntity<>(request), Object.class,
new ParameterizedTypeReference<List<JsonNode>>() {
},
entityId.getEntityType().name(), entityId.getEntityType().name(),
entityId.getId().toString(), entityId.getId().toString(),
scope).getBody(); scope)
.getStatusCode()
return RestJsonConverter.toAttributes(attributes); .is2xxSuccessful();
} }
public List<AttributeKvEntry> saveEntityAttributesV2(EntityId entityId, String scope, JsonNode request) { public boolean saveEntityAttributesV2(EntityId entityId, String scope, JsonNode request) {
List<JsonNode> attributes = restTemplate.exchange( return restTemplate
.postForEntity(
baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/attributes/{scope}", baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/attributes/{scope}",
HttpMethod.POST, request,
new HttpEntity<>(request), Object.class,
new ParameterizedTypeReference<List<JsonNode>>() {
},
entityId.getEntityType().name(), entityId.getEntityType().name(),
entityId.getId().toString(), entityId.getId().toString(),
scope).getBody(); scope)
.getStatusCode()
return RestJsonConverter.toAttributes(attributes); .is2xxSuccessful();
} }
public List<TsKvEntry> saveEntityTelemetry(EntityId entityId, String scope, String requestBody) { public boolean saveEntityTelemetry(EntityId entityId, String scope, JsonNode request) {
Map<String, List<JsonNode>> timeseries = restTemplate.exchange( return restTemplate
.postForEntity(
baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/timeseries/{scope}", baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/timeseries/{scope}",
HttpMethod.POST, request,
new HttpEntity<>(requestBody), Object.class,
new ParameterizedTypeReference<Map<String, List<JsonNode>>>() {
},
entityId.getEntityType().name(), entityId.getEntityType().name(),
entityId.getId().toString(), entityId.getId().toString(),
scope).getBody(); scope)
.getStatusCode()
return RestJsonConverter.toTimeseries(timeseries); .is2xxSuccessful();
} }
public List<TsKvEntry> saveEntityTelemetryWithTTL(EntityId entityId, String scope, Long ttl, String requestBody) { public boolean saveEntityTelemetryWithTTL(EntityId entityId, String scope, Long ttl, JsonNode request) {
Map<String, List<JsonNode>> timeseries = restTemplate.exchange( return restTemplate
.postForEntity(
baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/timeseries/{scope}/{ttl}", baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/timeseries/{scope}/{ttl}",
HttpMethod.POST, request,
new HttpEntity<>(requestBody), Object.class,
new ParameterizedTypeReference<Map<String, List<JsonNode>>>() {
},
entityId.getEntityType().name(), entityId.getEntityType().name(),
entityId.getId().toString(), entityId.getId().toString(),
scope, scope,
ttl).getBody(); ttl)
.getStatusCode()
return RestJsonConverter.toTimeseries(timeseries); .is2xxSuccessful();
} }
public List<TsKvEntry> deleteEntityTimeseries(EntityId entityId, public boolean deleteEntityTimeseries(EntityId entityId,
List<String> keys, List<String> keys,
boolean deleteAllDataForKeys, boolean deleteAllDataForKeys,
Long startTs, Long startTs,
@ -1775,44 +1765,46 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable {
params.put("endTs", endTs.toString()); params.put("endTs", endTs.toString());
params.put("rewriteLatestIfDeleted", String.valueOf(rewriteLatestIfDeleted)); params.put("rewriteLatestIfDeleted", String.valueOf(rewriteLatestIfDeleted));
Map<String, List<JsonNode>> timeseries = restTemplate.exchange( return restTemplate
.exchange(
baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/timeseries/delete?keys={keys}&deleteAllDataForKeys={deleteAllDataForKeys}&startTs={startTs}&endTs={endTs}&rewriteLatestIfDeleted={rewriteLatestIfDeleted}", baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/timeseries/delete?keys={keys}&deleteAllDataForKeys={deleteAllDataForKeys}&startTs={startTs}&endTs={endTs}&rewriteLatestIfDeleted={rewriteLatestIfDeleted}",
HttpMethod.DELETE, HttpMethod.DELETE,
HttpEntity.EMPTY, HttpEntity.EMPTY,
new ParameterizedTypeReference<Map<String, List<JsonNode>>>() { Object.class,
}, params)
params).getBody(); .getStatusCode()
.is2xxSuccessful();
return RestJsonConverter.toTimeseries(timeseries);
} }
public List<AttributeKvEntry> deleteEntityAttributes(String deviceId, String scope, List<String> keys) { public boolean deleteEntityAttributes(DeviceId deviceId, String scope, List<String> keys) {
List<JsonNode> attributes = restTemplate.exchange( return restTemplate
.exchange(
baseURL + "/api/plugins/telemetry/{deviceId}/{scope}?keys={keys}", baseURL + "/api/plugins/telemetry/{deviceId}/{scope}?keys={keys}",
HttpMethod.DELETE, HttpMethod.DELETE,
HttpEntity.EMPTY, HttpEntity.EMPTY,
new ParameterizedTypeReference<List<JsonNode>>() { Object.class,
}, deviceId.getId().toString(),
deviceId,
scope, scope,
listToString(keys)).getBody(); listToString(keys))
.getStatusCode()
return RestJsonConverter.toAttributes(attributes); .is2xxSuccessful();
} }
public List<AttributeKvEntry> deleteEntityAttributes(EntityId entityId, String scope, List<String> keys) { public boolean deleteEntityAttributes(EntityId entityId, String scope, List<String> keys) {
List<JsonNode> attributes = restTemplate.exchange( return restTemplate
.exchange(
baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/{scope}?keys={keys}", baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/{scope}?keys={keys}",
HttpMethod.DELETE, HttpMethod.DELETE,
HttpEntity.EMPTY, HttpEntity.EMPTY,
new ParameterizedTypeReference<List<JsonNode>>() { Object.class,
},
entityId.getEntityType().name(), entityId.getEntityType().name(),
entityId.getId().toString(), entityId.getId().toString(),
scope, scope,
listToString(keys)).getBody(); listToString(keys))
.getStatusCode()
.is2xxSuccessful();
return RestJsonConverter.toAttributes(attributes);
} }
public Optional<Tenant> getTenantById(String tenantId) { public Optional<Tenant> getTenantById(String tenantId) {