Merge pull request #480 from dmytro-landiak/master

added some funcs to rest client
This commit is contained in:
Andrew Shvayka 2017-12-18 10:52:00 +02:00 committed by GitHub
commit 23f81fe450
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -26,9 +26,16 @@ import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.support.HttpRequestWrapper;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.alarm.Alarm;
import org.thingsboard.server.common.data.alarm.AlarmSeverity;
import org.thingsboard.server.common.data.alarm.AlarmStatus;
import org.thingsboard.server.common.data.asset.Asset;
import org.thingsboard.server.common.data.id.AssetId;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.security.DeviceCredentials;
import java.io.IOException;
@ -71,18 +78,40 @@ public class RestClient implements ClientHttpRequestInterceptor {
}
}
public Device createDevice(String name) {
public Customer createCustomer(String title) {
Customer customer = new Customer();
customer.setTitle(title);
return restTemplate.postForEntity(baseURL + "/api/customer", customer, Customer.class).getBody();
}
public Device createDevice(String name, String type) {
Device device = new Device();
device.setName(name);
device.setType(type);
return restTemplate.postForEntity(baseURL + "/api/device", device, Device.class).getBody();
}
public Asset createAsset(String name, String type) {
Asset asset = new Asset();
asset.setName(name);
asset.setType(type);
return restTemplate.postForEntity(baseURL + "/api/asset", asset, Asset.class).getBody();
}
public Alarm createAlarm(Alarm alarm) {
return restTemplate.postForEntity(baseURL + "/api/alarm", alarm, Alarm.class).getBody();
}
public Device assignDevice(CustomerId customerId, DeviceId deviceId) {
return restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/device/{deviceId}", null, Device.class,
customerId.toString(), deviceId.toString()).getBody();
}
public Asset assignAsset(CustomerId customerId, AssetId assetId) {
return restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/asset/{assetId}", null, Asset.class,
customerId.toString(), assetId.toString()).getBody();
}
public DeviceCredentials getCredentials(DeviceId id) {
return restTemplate.getForEntity(baseURL + "/api/device/" + id.getId().toString() + "/credentials", DeviceCredentials.class).getBody();
}
@ -91,11 +120,14 @@ public class RestClient implements ClientHttpRequestInterceptor {
return restTemplate;
}
public String getToken() {
return token;
}
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] bytes, ClientHttpRequestExecution execution) throws IOException {
HttpRequest wrapper = new HttpRequestWrapper(request);
wrapper.getHeaders().set(JWT_TOKEN_HEADER_PARAM, "Bearer " + token);
return execution.execute(wrapper, bytes);
}
}