Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.bigboxer23</groupId>
<artifactId>switchbotapi-java</artifactId>
<version>1.1.4</version>
<version>1.1.5</version>

<name>switchbotapi-java</name>
<url>https://github.com/bigboxer23/switchbotapi-java</url>
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/com/bigboxer23/switch_bot/SwitchBotApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import lombok.Getter;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -78,17 +79,17 @@ protected RequestBuilderCallback addAuth() {
* @param apiResponse the API response
* @return true if error occurs
*/
protected boolean checkForError(IApiResponse apiResponse) {
return Optional.ofNullable(apiResponse)
.map(response -> {
if (response.getStatusCode() != 100) {
logger.error("error code: " + response.getStatusCode() + " : " + response.getMessage());
protected boolean checkForError(Response response, Optional<IApiResponse> apiResponse) {
return apiResponse
.map(api -> {
if (api.getStatusCode() != 100) {
logger.error("error code: " + api.getStatusCode() + " : " + api.getMessage());
return false;
}
return true;
})
.orElseGet(() -> {
logger.error("null api response");
logger.error("Error calling switchbot api: " + response.code() + " " + response.message());
return false;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;

import okhttp3.RequestBody;
import okhttp3.Response;
import org.slf4j.Logger;
Expand Down Expand Up @@ -78,7 +77,7 @@ public void sendDeviceControlCommands(String deviceId, DeviceCommand command) th
*/
private <T extends IApiResponse> T parseResponse(Response response, Class<T> clazz) throws IOException {
Optional<T> apiResponse = OkHttpUtil.getBody(response, clazz);
if (apiResponse.isEmpty() || !provider.checkForError(apiResponse.get())) {
if (!provider.checkForError(response, (Optional<IApiResponse>) apiResponse)) {
throw new IOException(response.code() + " " + response.message());
}
return apiResponse.get();
Expand Down