Skip to content

Commit 4ed57fb

Browse files
authored
Merge pull request #55 from devicehive/development
Updated error parsing; Now getDevice returns DHResponse<Device> object;
2 parents f6c7a4d + 58dbc3c commit 4ed57fb

File tree

13 files changed

+112
-43
lines changed

13 files changed

+112
-43
lines changed

client/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
<parent>
99
<groupId>com.github.devicehive</groupId>
1010
<artifactId>devicehive-java</artifactId>
11-
<version>3.0.3</version>
11+
<version>3.0.4</version>
1212
<relativePath>../pom.xml</relativePath>
1313
</parent>
1414
<name>devicehive-java</name>
15-
<version>3.0.3</version>
15+
<version>3.0.4</version>
1616
<prerequisites>
1717
<maven>2.2.0</maven>
1818
</prerequisites>

client/src/main/java/com/github/devicehive/client/api/MainDeviceHive.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public interface MainDeviceHive {
6565

6666
DHResponse<Void> removeDevice(String id);
6767

68-
Device getDevice(String id) ;
68+
DHResponse<Device> getDevice(String id) ;
6969

7070
DHResponse<Void> putDevice(String id, String name) ;
7171
}

client/src/main/java/com/github/devicehive/client/service/BaseService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.github.devicehive.rest.model.JsonStringWrapper;
3434
import com.github.devicehive.rest.model.JwtAccessToken;
3535
import com.github.devicehive.rest.model.JwtRefreshToken;
36+
import com.github.devicehive.websocket.model.repsonse.ErrorResponse;
3637
import com.google.gson.Gson;
3738
import com.google.gson.JsonObject;
3839

@@ -45,6 +46,7 @@
4546

4647
public class BaseService {
4748
public static final String ERROR_MESSAGE_KEY = "message";
49+
public static final String FAILED_CONNECT_TO_SERVER = "Failed connect to server";
4850
ApiClient apiClient;
4951

5052
BaseService() {
@@ -102,6 +104,9 @@ final <T> DHResponse<T> execute(Call<T> call) {
102104
} catch (IOException e) {
103105
FailureData failureData = createFailData(FailureData.NO_CODE, e.getMessage());
104106
return new DHResponse<T>(null, failureData);
107+
} catch (NullPointerException e) {
108+
return new DHResponse<T>(null, FailureData.create(
109+
ErrorResponse.create(FAILED_CONNECT_TO_SERVER)));
105110
}
106111
}
107112

client/src/main/java/com/github/devicehive/client/service/DeviceHive.java

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,45 @@
2424
import com.github.devicehive.client.api.MainDeviceHive;
2525
import com.github.devicehive.client.callback.ResponseCallback;
2626
import com.github.devicehive.client.exceptions.IncorrectUrlException;
27-
import com.github.devicehive.client.model.*;
27+
import com.github.devicehive.client.model.CommandFilter;
28+
import com.github.devicehive.client.model.DHResponse;
29+
import com.github.devicehive.client.model.DeviceCommandsCallback;
30+
import com.github.devicehive.client.model.DeviceFilter;
2831
import com.github.devicehive.client.model.DeviceNotification;
32+
import com.github.devicehive.client.model.DeviceNotificationsCallback;
33+
import com.github.devicehive.client.model.FailureData;
34+
import com.github.devicehive.client.model.NetworkFilter;
35+
import com.github.devicehive.client.model.NotificationFilter;
36+
import com.github.devicehive.client.model.TokenAuth;
37+
import com.github.devicehive.client.model.UserFilter;
2938
import com.github.devicehive.rest.api.AuthApi;
30-
import com.github.devicehive.rest.model.*;
39+
import com.github.devicehive.rest.model.ApiInfo;
40+
import com.github.devicehive.rest.model.ClusterConfig;
41+
import com.github.devicehive.rest.model.Configuration;
42+
import com.github.devicehive.rest.model.JsonStringWrapper;
43+
import com.github.devicehive.rest.model.JwtAccessToken;
44+
import com.github.devicehive.rest.model.JwtRequest;
45+
import com.github.devicehive.rest.model.JwtToken;
46+
import com.github.devicehive.rest.model.NetworkVO;
47+
import com.github.devicehive.rest.model.RoleEnum;
48+
import com.github.devicehive.rest.model.StatusEnum;
3149
import com.github.devicehive.websocket.api.CommandWS;
3250
import com.github.devicehive.websocket.api.NotificationWS;
3351
import com.github.devicehive.websocket.api.WebSocketClient;
3452
import com.github.devicehive.websocket.listener.CommandListener;
3553
import com.github.devicehive.websocket.listener.NotificationListener;
36-
import com.github.devicehive.websocket.model.repsonse.*;
37-
import org.joda.time.DateTime;
54+
import com.github.devicehive.websocket.model.repsonse.CommandGetResponse;
55+
import com.github.devicehive.websocket.model.repsonse.CommandInsertResponse;
56+
import com.github.devicehive.websocket.model.repsonse.CommandListResponse;
57+
import com.github.devicehive.websocket.model.repsonse.CommandSubscribeResponse;
58+
import com.github.devicehive.websocket.model.repsonse.ErrorResponse;
59+
import com.github.devicehive.websocket.model.repsonse.NotificationGetResponse;
60+
import com.github.devicehive.websocket.model.repsonse.NotificationInsertResponse;
61+
import com.github.devicehive.websocket.model.repsonse.NotificationListResponse;
62+
import com.github.devicehive.websocket.model.repsonse.NotificationSubscribeResponse;
63+
import com.github.devicehive.websocket.model.repsonse.ResponseAction;
3864

39-
import okhttp3.logging.HttpLoggingInterceptor;
40-
import retrofit2.Call;
41-
import retrofit2.Callback;
42-
import retrofit2.Response;
65+
import org.joda.time.DateTime;
4366

4467
import java.io.IOException;
4568
import java.net.URI;
@@ -48,6 +71,11 @@
4871
import java.util.Collections;
4972
import java.util.List;
5073

74+
import okhttp3.logging.HttpLoggingInterceptor;
75+
import retrofit2.Call;
76+
import retrofit2.Callback;
77+
import retrofit2.Response;
78+
5179
public class DeviceHive implements MainDeviceHive {
5280

5381
private static final String WEBSOCKET_PATH = "/api/websocket";
@@ -408,7 +436,7 @@ public DHResponse<Void> removeDevice(String id) {
408436
return deviceService.removeDevice(id);
409437
}
410438

411-
public Device getDevice(String id) {
439+
public DHResponse<Device> getDevice(String id) {
412440
return deviceService.getDevice(id);
413441
}
414442

client/src/main/java/com/github/devicehive/client/service/DeviceService.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,31 +48,31 @@ DHResponse<Void> createDevice(String deviceId, String name) {
4848
}
4949
}
5050

51-
Device getDevice(String deviceId) {
51+
DHResponse<Device> getDevice(String deviceId) {
5252
deviceApi = createService(DeviceApi.class);
5353
DHResponse<Device> response;
5454
DHResponse<com.github.devicehive.rest.model.Device> result;
5555

5656
result = execute(deviceApi.get(deviceId));
5757
response = new DHResponse<Device>(Device.create(result.getData()), result.getFailureData());
5858
if (response.isSuccessful()) {
59-
return response.getData();
59+
return response;
6060
} else if (response.getFailureData().getCode() == 401) {
6161
authorize();
6262
deviceApi = createService(DeviceApi.class);
6363
result = execute(deviceApi.get(deviceId));
6464
response = new DHResponse<Device>(Device.create(result.getData()), result.getFailureData());
6565
if (response.isSuccessful()) {
66-
return response.getData();
66+
return response;
6767
} else if (response.getFailureData().getCode() == 404 || response.getFailureData().getCode() == 403) {
6868
return createAndGetDevice(deviceId);
6969
} else {
70-
return null;
70+
return response;
7171
}
7272
} else if (response.getFailureData().getCode() == 404 || response.getFailureData().getCode() == 403) {
7373
return createAndGetDevice(deviceId);
7474
} else {
75-
return null;
75+
return response;
7676
}
7777
}
7878

@@ -114,11 +114,10 @@ DHResponse<Void> removeDevice(String deviceId) {
114114
}
115115
}
116116

117-
private Device createAndGetDevice(String id) {
117+
private DHResponse<Device> createAndGetDevice(String id) {
118118
createDevice(id, id);
119119
DHResponse<com.github.devicehive.rest.model.Device> result = execute(deviceApi.get(id));
120-
DHResponse<Device> response = new DHResponse<Device>(Device.create(result.getData()), result.getFailureData());
121-
return response.isSuccessful() ? response.getData() : null;
120+
return new DHResponse<>(Device.create(result.getData()), result.getFailureData());
122121

123122
}
124123
}

client/src/main/java/example/Echo.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
package example;
2323

2424
import com.github.devicehive.client.model.CommandFilter;
25+
import com.github.devicehive.client.model.DHResponse;
2526
import com.github.devicehive.client.model.DeviceCommandsCallback;
2627
import com.github.devicehive.client.model.FailureData;
2728
import com.github.devicehive.client.model.TokenAuth;
2829
import com.github.devicehive.client.service.Device;
2930
import com.github.devicehive.client.service.DeviceCommand;
3031
import com.github.devicehive.client.service.DeviceHive;
32+
3133
import org.joda.time.DateTime;
3234

3335
import java.util.Collections;
@@ -46,7 +48,12 @@ public class Echo {
4648
public static void main(String[] args) {
4749
final DeviceHive deviceHive = DeviceHive.getInstance().init(URL, new TokenAuth(refreshToken, accessToken));
4850
final String deviceId = UUID.randomUUID().toString();
49-
final Device device = deviceHive.getDevice(deviceId);
51+
DHResponse<Device> deviceResponse = deviceHive.getDevice(deviceId);
52+
if (!deviceResponse.isSuccessful()) {
53+
System.out.println(deviceResponse);
54+
return;
55+
}
56+
Device device = deviceResponse.getData();
5057
CommandFilter filter = getFilter();
5158
deviceHive.subscribeCommands(Collections.singletonList(deviceId), filter, new DeviceCommandsCallback() {
5259
@Override

client/src/main/java/example/Main.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package example;
22

33
import com.github.devicehive.client.model.CommandFilter;
4+
import com.github.devicehive.client.model.DHResponse;
45
import com.github.devicehive.client.model.DeviceCommandsCallback;
56
import com.github.devicehive.client.model.DeviceNotification;
67
import com.github.devicehive.client.model.DeviceNotificationsCallback;
@@ -47,7 +48,12 @@ public static void main(String[] args) {
4748
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
4849
String DEVICE_ID = "123456-example";
4950
//Device Initiating
50-
final Device device = deviceHive.getDevice(DEVICE_ID);
51+
DHResponse<Device> deviceResponse = deviceHive.getDevice(DEVICE_ID);
52+
if (!deviceResponse.isSuccessful()) {
53+
System.out.println(deviceResponse);
54+
return;
55+
}
56+
final Device device = deviceResponse.getData();
5157
//Creating filter to listen commands from the server
5258
CommandFilter commandFilter = new CommandFilter();
5359
commandFilter.setCommandNames(PING);
@@ -65,8 +71,8 @@ public void onSuccess(List<DeviceCommand> commands) {
6571

6672
if (params != null) {
6773
//Getting param value
68-
Gson gson=new Gson();
69-
JsonObject jsonObject =gson.fromJson(params.getJsonString(),JsonObject.class);
74+
Gson gson = new Gson();
75+
JsonObject jsonObject = gson.fromJson(params.getJsonString(), JsonObject.class);
7076

7177
boolean needToSend = false;
7278
if (jsonObject.has(PRODUCE_NOTIFICATION)) {

client/src/test/java/com/github/devicehive/client/CommandTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.github.devicehive.client.service.DeviceCommand;
2828
import com.github.devicehive.client.service.DeviceHive;
2929
import com.github.devicehive.rest.model.JsonStringWrapper;
30+
3031
import org.junit.Assert;
3132
import org.junit.Test;
3233

@@ -41,11 +42,12 @@ public class CommandTest {
4142

4243
private DeviceHive deviceHive = DeviceHive.getInstance().init(URL, WS_URL, new TokenAuth(refreshToken, accessToken));
4344

44-
private Device device = deviceHive.getDevice(DEVICE_ID);
45+
private DHResponse<Device> deviceResponse = deviceHive.getDevice(DEVICE_ID);
4546

4647
@Test
4748
public void createAndUpdate() throws InterruptedException {
48-
Assert.assertNotNull(device);
49+
Assert.assertTrue(deviceResponse.isSuccessful());
50+
Device device = deviceResponse.getData();
4951
DHResponse<DeviceCommand> response = device.sendCommand(COM_A, null);
5052
Assert.assertTrue(response.isSuccessful());
5153
DeviceCommand command = response.getData();

client/src/test/java/com/github/devicehive/client/DeviceTest.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,21 @@
2121

2222
package com.github.devicehive.client;
2323

24-
import com.github.devicehive.client.model.*;
24+
import com.github.devicehive.client.model.CommandFilter;
25+
import com.github.devicehive.client.model.DHResponse;
26+
import com.github.devicehive.client.model.DeviceCommandsCallback;
27+
import com.github.devicehive.client.model.DeviceNotification;
28+
import com.github.devicehive.client.model.DeviceNotificationsCallback;
29+
import com.github.devicehive.client.model.FailureData;
30+
import com.github.devicehive.client.model.NotificationFilter;
31+
import com.github.devicehive.client.model.Parameter;
32+
import com.github.devicehive.client.model.TokenAuth;
2533
import com.github.devicehive.client.service.Device;
2634
import com.github.devicehive.client.service.DeviceCommand;
2735
import com.github.devicehive.client.service.DeviceHive;
36+
2837
import org.joda.time.DateTime;
2938
import org.junit.Assert;
30-
import org.junit.Ignore;
3139
import org.junit.Test;
3240

3341
import java.io.IOException;
@@ -61,17 +69,19 @@ public class DeviceTest {
6169
@Test
6270
public void createDevice() throws IOException {
6371
String deviceId = DEVICE_PREFIX + UUID.randomUUID().toString();
64-
Device device = deviceHive.getDevice(deviceId);
65-
Assert.assertNotNull(device);
72+
DHResponse<Device> deviceResponse = deviceHive.getDevice(deviceId);
73+
Assert.assertTrue(deviceResponse.isSuccessful());
74+
final Device device = deviceResponse.getData();
6675
Assert.assertTrue(device != null);
6776
Assert.assertTrue(deviceHive.removeDevice(deviceId).isSuccessful());
6877
}
6978

7079
@Test
7180
public void getCommands() throws IOException {
7281
String deviceId = DEVICE_PREFIX + UUID.randomUUID().toString();
73-
final Device device = deviceHive.getDevice(deviceId);
74-
Assert.assertNotNull(device);
82+
DHResponse<Device> deviceResponse = deviceHive.getDevice(deviceId);
83+
Assert.assertTrue(deviceResponse.isSuccessful());
84+
final Device device = deviceResponse.getData();
7585
ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
7686
service.schedule(new Thread(new Runnable() {
7787
public void run() {
@@ -94,8 +104,9 @@ public void run() {
94104
@Test
95105
public void subscribeCommands() throws InterruptedException {
96106
String deviceId = DEVICE_PREFIX + UUID.randomUUID().toString();
97-
final Device device = deviceHive.getDevice(deviceId);
98-
Assert.assertNotNull(device);
107+
DHResponse<Device> deviceResponse = deviceHive.getDevice(deviceId);
108+
Assert.assertTrue(deviceResponse.isSuccessful());
109+
final Device device = deviceResponse.getData();
99110
final ScheduledExecutorService service = Executors.newScheduledThreadPool(2);
100111
final String commandName1 = COM_A + new Random().nextInt();
101112
final String commandName2 = COM_B + new Random().nextInt();
@@ -169,8 +180,9 @@ public void onFail(FailureData failureData) {
169180
public void subscribeNotifications() throws IOException, InterruptedException {
170181
String deviceId = DEVICE_PREFIX + UUID.randomUUID().toString();
171182

172-
final Device device = deviceHive.getDevice(deviceId);
173-
Assert.assertNotNull(device);
183+
DHResponse<Device> deviceResponse = deviceHive.getDevice(deviceId);
184+
Assert.assertTrue(deviceResponse.isSuccessful());
185+
final Device device = deviceResponse.getData();
174186
final CountDownLatch latch = new CountDownLatch(3);
175187
final String notificationName1 = NOTIFICATION_A + new Random().nextInt();
176188
final String notificationName2 = NOTIFICATION_B + new Random().nextInt();
@@ -244,8 +256,9 @@ public void run() {
244256
@Test
245257
public void getNotification() throws IOException {
246258
String deviceId = UUID.randomUUID().toString();
247-
final Device device = deviceHive.getDevice(deviceId);
248-
Assert.assertNotNull(device);
259+
DHResponse<Device> deviceResponse = deviceHive.getDevice(deviceId);
260+
Assert.assertTrue(deviceResponse.isSuccessful());
261+
final Device device = deviceResponse.getData();
249262
ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
250263
service.schedule(new Thread(new Runnable() {
251264
public void run() {
@@ -267,7 +280,9 @@ public void run() {
267280
@Test
268281
public void sendNotification() throws IOException {
269282
String deviceId = DEVICE_PREFIX + UUID.randomUUID().toString();
270-
Device device = deviceHive.getDevice(deviceId);
283+
DHResponse<Device> deviceResponse = deviceHive.getDevice(deviceId);
284+
Assert.assertTrue(deviceResponse.isSuccessful());
285+
final Device device = deviceResponse.getData();
271286
Assert.assertNotNull(device);
272287
List<Parameter> parameters = new ArrayList<Parameter>();
273288

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<artifactId>devicehive-java</artifactId>
66
<packaging>pom</packaging>
77
<name>devicehive-java</name>
8-
<version>3.0.3</version>
8+
<version>3.0.4</version>
99
<prerequisites>
1010
<maven>2.2.0</maven>
1111
</prerequisites>

0 commit comments

Comments
 (0)