diff --git a/pom.xml b/pom.xml index e3c7110..0715001 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.bigboxer23 switchbotapi-java - 1.1.9 + 1.2.0 switchbotapi-java https://github.com/bigboxer23/switchbotapi-java diff --git a/src/main/java/com/bigboxer23/switch_bot/SwitchBotApi.java b/src/main/java/com/bigboxer23/switch_bot/SwitchBotApi.java index 0d8769c..f50c7f4 100644 --- a/src/main/java/com/bigboxer23/switch_bot/SwitchBotApi.java +++ b/src/main/java/com/bigboxer23/switch_bot/SwitchBotApi.java @@ -1,5 +1,6 @@ package com.bigboxer23.switch_bot; +import com.bigboxer23.switch_bot.data.BadApiResponse; import com.bigboxer23.switch_bot.data.IApiResponse; import com.bigboxer23.utils.http.RequestBuilderCallback; import com.squareup.moshi.Moshi; @@ -78,18 +79,19 @@ protected RequestBuilderCallback addAuth() { * @param apiResponse the API response * @return true if error occurs */ - protected boolean checkForError(Response response, Optional apiResponse) { + protected IApiResponse checkForError(Response response, Optional apiResponse) { return apiResponse .map(api -> { if (api.getStatusCode() != 100) { log.error("error code: " + api.getStatusCode() + " : " + api.getMessage()); - return false; + api.setSuccess(false); + return api; } - return true; + return api; }) .orElseGet(() -> { log.error("Error calling switchbot api: " + response.code() + " " + response.message()); - return false; + return new BadApiResponse(response.code(), response.message()); }); } } diff --git a/src/main/java/com/bigboxer23/switch_bot/SwitchBotDeviceApi.java b/src/main/java/com/bigboxer23/switch_bot/SwitchBotDeviceApi.java index a11d098..94886ac 100644 --- a/src/main/java/com/bigboxer23/switch_bot/SwitchBotDeviceApi.java +++ b/src/main/java/com/bigboxer23/switch_bot/SwitchBotDeviceApi.java @@ -106,10 +106,11 @@ public IApiResponse sendDeviceControlCommands(String deviceId, DeviceCommand com * @throws IOException */ private T parseResponse(Response response, Class clazz) throws IOException { - Optional apiResponse = OkHttpUtil.getBody(response, clazz); - if (!provider.checkForError(response, (Optional) apiResponse)) { - throw new IOException(response.code() + " " + response.message()); + IApiResponse apiResponse = + provider.checkForError(response, (Optional) OkHttpUtil.getBody(response, clazz)); + if (!apiResponse.isSuccess()) { + throw new IOException(apiResponse.getStatusCode() + " " + apiResponse.getMessage()); } - return apiResponse.get(); + return (T) apiResponse; } } diff --git a/src/main/java/com/bigboxer23/switch_bot/data/ApiResponse.java b/src/main/java/com/bigboxer23/switch_bot/data/ApiResponse.java index a585d89..169a136 100644 --- a/src/main/java/com/bigboxer23/switch_bot/data/ApiResponse.java +++ b/src/main/java/com/bigboxer23/switch_bot/data/ApiResponse.java @@ -10,4 +10,6 @@ public class ApiResponse implements IApiResponse { private ApiResponseBody body; private String message; + + private boolean success = true; } diff --git a/src/main/java/com/bigboxer23/switch_bot/data/BadApiResponse.java b/src/main/java/com/bigboxer23/switch_bot/data/BadApiResponse.java new file mode 100644 index 0000000..a17dea7 --- /dev/null +++ b/src/main/java/com/bigboxer23/switch_bot/data/BadApiResponse.java @@ -0,0 +1,18 @@ +package com.bigboxer23.switch_bot.data; + +import lombok.Data; + +/** */ +@Data +public class BadApiResponse implements IApiResponse { + private int statusCode; + + private String message; + + private boolean success = false; + + public BadApiResponse(int statusCode, String message) { + this.statusCode = statusCode; + this.message = message; + } +} diff --git a/src/main/java/com/bigboxer23/switch_bot/data/DeviceApiResponse.java b/src/main/java/com/bigboxer23/switch_bot/data/DeviceApiResponse.java index 7d5243e..3174c10 100644 --- a/src/main/java/com/bigboxer23/switch_bot/data/DeviceApiResponse.java +++ b/src/main/java/com/bigboxer23/switch_bot/data/DeviceApiResponse.java @@ -10,4 +10,6 @@ public class DeviceApiResponse implements IApiResponse { private Device body; private String message; + + private boolean success = true; } diff --git a/src/main/java/com/bigboxer23/switch_bot/data/IApiResponse.java b/src/main/java/com/bigboxer23/switch_bot/data/IApiResponse.java index ca9fbc6..8b7a1e5 100644 --- a/src/main/java/com/bigboxer23/switch_bot/data/IApiResponse.java +++ b/src/main/java/com/bigboxer23/switch_bot/data/IApiResponse.java @@ -4,5 +4,13 @@ public interface IApiResponse { int getStatusCode(); + void setStatusCode(int statusCode); + String getMessage(); + + void setMessage(String message); + + boolean isSuccess(); + + void setSuccess(boolean success); }