Commit fbed56555f42a3534695d3d76c8ba6f6f92ed995

Authored by Yevhen Bondarenko
Committed by GitHub
1 parent 23919b3d

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
@@ -1690,82 +1690,72 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable { @@ -1690,82 +1690,72 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable {
1690 return RestJsonConverter.toTimeseries(timeseries); 1690 return RestJsonConverter.toTimeseries(timeseries);
1691 } 1691 }
1692 1692
1693 - public List<AttributeKvEntry> saveDeviceAttributes(String deviceId, String scope, JsonNode request) {  
1694 - List<JsonNode> attributes = restTemplate.exchange(  
1695 - baseURL + "/api/plugins/telemetry/{deviceId}/{scope}",  
1696 - HttpMethod.POST,  
1697 - new HttpEntity<>(request),  
1698 - new ParameterizedTypeReference<List<JsonNode>>() {  
1699 - },  
1700 - deviceId,  
1701 - scope).getBody();  
1702 -  
1703 - return RestJsonConverter.toAttributes(attributes);  
1704 - }  
1705 -  
1706 - public List<AttributeKvEntry> saveEntityAttributesV1(EntityId entityId, String scope, JsonNode request) {  
1707 - List<JsonNode> attributes = restTemplate.exchange(  
1708 - baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/{scope}",  
1709 - HttpMethod.POST,  
1710 - new HttpEntity<>(request),  
1711 - new ParameterizedTypeReference<List<JsonNode>>() {  
1712 - },  
1713 - entityId.getEntityType().name(),  
1714 - entityId.getId().toString(),  
1715 - scope).getBody();  
1716 -  
1717 - return RestJsonConverter.toAttributes(attributes);  
1718 - }  
1719 -  
1720 - public List<AttributeKvEntry> saveEntityAttributesV2(EntityId entityId, String scope, JsonNode request) {  
1721 - List<JsonNode> attributes = restTemplate.exchange(  
1722 - baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/attributes/{scope}",  
1723 - HttpMethod.POST,  
1724 - new HttpEntity<>(request),  
1725 - new ParameterizedTypeReference<List<JsonNode>>() {  
1726 - },  
1727 - entityId.getEntityType().name(),  
1728 - entityId.getId().toString(),  
1729 - scope).getBody();  
1730 -  
1731 - return RestJsonConverter.toAttributes(attributes);  
1732 - }  
1733 -  
1734 - public List<TsKvEntry> saveEntityTelemetry(EntityId entityId, String scope, String requestBody) {  
1735 - Map<String, List<JsonNode>> timeseries = restTemplate.exchange(  
1736 - baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/timeseries/{scope}",  
1737 - HttpMethod.POST,  
1738 - new HttpEntity<>(requestBody),  
1739 - new ParameterizedTypeReference<Map<String, List<JsonNode>>>() {  
1740 - },  
1741 - entityId.getEntityType().name(),  
1742 - entityId.getId().toString(),  
1743 - scope).getBody();  
1744 -  
1745 - return RestJsonConverter.toTimeseries(timeseries);  
1746 - }  
1747 -  
1748 - public List<TsKvEntry> saveEntityTelemetryWithTTL(EntityId entityId, String scope, Long ttl, String requestBody) {  
1749 - Map<String, List<JsonNode>> timeseries = restTemplate.exchange(  
1750 - baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/timeseries/{scope}/{ttl}",  
1751 - HttpMethod.POST,  
1752 - new HttpEntity<>(requestBody),  
1753 - new ParameterizedTypeReference<Map<String, List<JsonNode>>>() {  
1754 - },  
1755 - entityId.getEntityType().name(),  
1756 - entityId.getId().toString(),  
1757 - scope,  
1758 - ttl).getBody();  
1759 -  
1760 - return RestJsonConverter.toTimeseries(timeseries);  
1761 - }  
1762 -  
1763 - public List<TsKvEntry> deleteEntityTimeseries(EntityId entityId,  
1764 - List<String> keys,  
1765 - boolean deleteAllDataForKeys,  
1766 - Long startTs,  
1767 - Long endTs,  
1768 - boolean rewriteLatestIfDeleted) { 1693 + public boolean saveDeviceAttributes(DeviceId deviceId, String scope, JsonNode request) {
  1694 + return restTemplate
  1695 + .postForEntity(baseURL + "/api/plugins/telemetry/{deviceId}/{scope}", request, Object.class, deviceId.getId().toString(), scope)
  1696 + .getStatusCode()
  1697 + .is2xxSuccessful();
  1698 + }
  1699 +
  1700 + public boolean saveEntityAttributesV1(EntityId entityId, String scope, JsonNode request) {
  1701 + return restTemplate
  1702 + .postForEntity(
  1703 + baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/{scope}",
  1704 + request,
  1705 + Object.class,
  1706 + entityId.getEntityType().name(),
  1707 + entityId.getId().toString(),
  1708 + scope)
  1709 + .getStatusCode()
  1710 + .is2xxSuccessful();
  1711 + }
  1712 +
  1713 + public boolean saveEntityAttributesV2(EntityId entityId, String scope, JsonNode request) {
  1714 + return restTemplate
  1715 + .postForEntity(
  1716 + baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/attributes/{scope}",
  1717 + request,
  1718 + Object.class,
  1719 + entityId.getEntityType().name(),
  1720 + entityId.getId().toString(),
  1721 + scope)
  1722 + .getStatusCode()
  1723 + .is2xxSuccessful();
  1724 + }
  1725 +
  1726 + public boolean saveEntityTelemetry(EntityId entityId, String scope, JsonNode request) {
  1727 + return restTemplate
  1728 + .postForEntity(
  1729 + baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/timeseries/{scope}",
  1730 + request,
  1731 + Object.class,
  1732 + entityId.getEntityType().name(),
  1733 + entityId.getId().toString(),
  1734 + scope)
  1735 + .getStatusCode()
  1736 + .is2xxSuccessful();
  1737 + }
  1738 +
  1739 + public boolean saveEntityTelemetryWithTTL(EntityId entityId, String scope, Long ttl, JsonNode request) {
  1740 + return restTemplate
  1741 + .postForEntity(
  1742 + baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/timeseries/{scope}/{ttl}",
  1743 + request,
  1744 + Object.class,
  1745 + entityId.getEntityType().name(),
  1746 + entityId.getId().toString(),
  1747 + scope,
  1748 + ttl)
  1749 + .getStatusCode()
  1750 + .is2xxSuccessful();
  1751 + }
  1752 +
  1753 + public boolean deleteEntityTimeseries(EntityId entityId,
  1754 + List<String> keys,
  1755 + boolean deleteAllDataForKeys,
  1756 + Long startTs,
  1757 + Long endTs,
  1758 + boolean rewriteLatestIfDeleted) {
1769 Map<String, String> params = new HashMap<>(); 1759 Map<String, String> params = new HashMap<>();
1770 params.put("entityType", entityId.getEntityType().name()); 1760 params.put("entityType", entityId.getEntityType().name());
1771 params.put("entityId", entityId.getId().toString()); 1761 params.put("entityId", entityId.getId().toString());
@@ -1775,44 +1765,46 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable { @@ -1775,44 +1765,46 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable {
1775 params.put("endTs", endTs.toString()); 1765 params.put("endTs", endTs.toString());
1776 params.put("rewriteLatestIfDeleted", String.valueOf(rewriteLatestIfDeleted)); 1766 params.put("rewriteLatestIfDeleted", String.valueOf(rewriteLatestIfDeleted));
1777 1767
1778 - Map<String, List<JsonNode>> timeseries = restTemplate.exchange(  
1779 - baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/timeseries/delete?keys={keys}&deleteAllDataForKeys={deleteAllDataForKeys}&startTs={startTs}&endTs={endTs}&rewriteLatestIfDeleted={rewriteLatestIfDeleted}",  
1780 - HttpMethod.DELETE,  
1781 - HttpEntity.EMPTY,  
1782 - new ParameterizedTypeReference<Map<String, List<JsonNode>>>() {  
1783 - },  
1784 - params).getBody();  
1785 -  
1786 - return RestJsonConverter.toTimeseries(timeseries);  
1787 - } 1768 + return restTemplate
  1769 + .exchange(
  1770 + baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/timeseries/delete?keys={keys}&deleteAllDataForKeys={deleteAllDataForKeys}&startTs={startTs}&endTs={endTs}&rewriteLatestIfDeleted={rewriteLatestIfDeleted}",
  1771 + HttpMethod.DELETE,
  1772 + HttpEntity.EMPTY,
  1773 + Object.class,
  1774 + params)
  1775 + .getStatusCode()
  1776 + .is2xxSuccessful();
  1777 +
  1778 + }
  1779 +
  1780 + public boolean deleteEntityAttributes(DeviceId deviceId, String scope, List<String> keys) {
  1781 + return restTemplate
  1782 + .exchange(
  1783 + baseURL + "/api/plugins/telemetry/{deviceId}/{scope}?keys={keys}",
  1784 + HttpMethod.DELETE,
  1785 + HttpEntity.EMPTY,
  1786 + Object.class,
  1787 + deviceId.getId().toString(),
  1788 + scope,
  1789 + listToString(keys))
  1790 + .getStatusCode()
  1791 + .is2xxSuccessful();
  1792 + }
  1793 +
  1794 + public boolean deleteEntityAttributes(EntityId entityId, String scope, List<String> keys) {
  1795 + return restTemplate
  1796 + .exchange(
  1797 + baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/{scope}?keys={keys}",
  1798 + HttpMethod.DELETE,
  1799 + HttpEntity.EMPTY,
  1800 + Object.class,
  1801 + entityId.getEntityType().name(),
  1802 + entityId.getId().toString(),
  1803 + scope,
  1804 + listToString(keys))
  1805 + .getStatusCode()
  1806 + .is2xxSuccessful();
1788 1807
1789 - public List<AttributeKvEntry> deleteEntityAttributes(String deviceId, String scope, List<String> keys) {  
1790 - List<JsonNode> attributes = restTemplate.exchange(  
1791 - baseURL + "/api/plugins/telemetry/{deviceId}/{scope}?keys={keys}",  
1792 - HttpMethod.DELETE,  
1793 - HttpEntity.EMPTY,  
1794 - new ParameterizedTypeReference<List<JsonNode>>() {  
1795 - },  
1796 - deviceId,  
1797 - scope,  
1798 - listToString(keys)).getBody();  
1799 -  
1800 - return RestJsonConverter.toAttributes(attributes);  
1801 - }  
1802 -  
1803 - public List<AttributeKvEntry> deleteEntityAttributes(EntityId entityId, String scope, List<String> keys) {  
1804 - List<JsonNode> attributes = restTemplate.exchange(  
1805 - baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/{scope}?keys={keys}",  
1806 - HttpMethod.DELETE,  
1807 - HttpEntity.EMPTY,  
1808 - new ParameterizedTypeReference<List<JsonNode>>() {  
1809 - },  
1810 - entityId.getEntityType().name(),  
1811 - entityId.getId().toString(),  
1812 - scope,  
1813 - listToString(keys)).getBody();  
1814 -  
1815 - return RestJsonConverter.toAttributes(attributes);  
1816 } 1808 }
1817 1809
1818 public Optional<Tenant> getTenantById(String tenantId) { 1810 public Optional<Tenant> getTenantById(String tenantId) {