Skip to content

Commit 2811255

Browse files
authored
feat: clean up the error response so we get the underling status code and message instead of the responses since that can be different within the SB API (#97)
1 parent 475bcae commit 2811255

File tree

7 files changed

+42
-9
lines changed

7 files changed

+42
-9
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.bigboxer23</groupId>
66
<artifactId>switchbotapi-java</artifactId>
7-
<version>1.1.9</version>
7+
<version>1.2.0</version>
88

99
<name>switchbotapi-java</name>
1010
<url>https://github.com/bigboxer23/switchbotapi-java</url>

src/main/java/com/bigboxer23/switch_bot/SwitchBotApi.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.bigboxer23.switch_bot;
22

3+
import com.bigboxer23.switch_bot.data.BadApiResponse;
34
import com.bigboxer23.switch_bot.data.IApiResponse;
45
import com.bigboxer23.utils.http.RequestBuilderCallback;
56
import com.squareup.moshi.Moshi;
@@ -78,18 +79,19 @@ protected RequestBuilderCallback addAuth() {
7879
* @param apiResponse the API response
7980
* @return true if error occurs
8081
*/
81-
protected boolean checkForError(Response response, Optional<IApiResponse> apiResponse) {
82+
protected IApiResponse checkForError(Response response, Optional<IApiResponse> apiResponse) {
8283
return apiResponse
8384
.map(api -> {
8485
if (api.getStatusCode() != 100) {
8586
log.error("error code: " + api.getStatusCode() + " : " + api.getMessage());
86-
return false;
87+
api.setSuccess(false);
88+
return api;
8789
}
88-
return true;
90+
return api;
8991
})
9092
.orElseGet(() -> {
9193
log.error("Error calling switchbot api: " + response.code() + " " + response.message());
92-
return false;
94+
return new BadApiResponse(response.code(), response.message());
9395
});
9496
}
9597
}

src/main/java/com/bigboxer23/switch_bot/SwitchBotDeviceApi.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ public IApiResponse sendDeviceControlCommands(String deviceId, DeviceCommand com
106106
* @throws IOException
107107
*/
108108
private <T extends IApiResponse> T parseResponse(Response response, Class<T> clazz) throws IOException {
109-
Optional<T> apiResponse = OkHttpUtil.getBody(response, clazz);
110-
if (!provider.checkForError(response, (Optional<IApiResponse>) apiResponse)) {
111-
throw new IOException(response.code() + " " + response.message());
109+
IApiResponse apiResponse =
110+
provider.checkForError(response, (Optional<IApiResponse>) OkHttpUtil.getBody(response, clazz));
111+
if (!apiResponse.isSuccess()) {
112+
throw new IOException(apiResponse.getStatusCode() + " " + apiResponse.getMessage());
112113
}
113-
return apiResponse.get();
114+
return (T) apiResponse;
114115
}
115116
}

src/main/java/com/bigboxer23/switch_bot/data/ApiResponse.java

+2
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ public class ApiResponse implements IApiResponse {
1010
private ApiResponseBody body;
1111

1212
private String message;
13+
14+
private boolean success = true;
1315
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.bigboxer23.switch_bot.data;
2+
3+
import lombok.Data;
4+
5+
/** */
6+
@Data
7+
public class BadApiResponse implements IApiResponse {
8+
private int statusCode;
9+
10+
private String message;
11+
12+
private boolean success = false;
13+
14+
public BadApiResponse(int statusCode, String message) {
15+
this.statusCode = statusCode;
16+
this.message = message;
17+
}
18+
}

src/main/java/com/bigboxer23/switch_bot/data/DeviceApiResponse.java

+2
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ public class DeviceApiResponse implements IApiResponse {
1010
private Device body;
1111

1212
private String message;
13+
14+
private boolean success = true;
1315
}

src/main/java/com/bigboxer23/switch_bot/data/IApiResponse.java

+8
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,13 @@
44
public interface IApiResponse {
55
int getStatusCode();
66

7+
void setStatusCode(int statusCode);
8+
79
String getMessage();
10+
11+
void setMessage(String message);
12+
13+
boolean isSuccess();
14+
15+
void setSuccess(boolean success);
816
}

0 commit comments

Comments
 (0)