Skip to content

feat: clean up the error response so we get the underling status code… #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2025
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.9</version>
<version>1.2.0</version>

<name>switchbotapi-java</name>
<url>https://github.com/bigboxer23/switchbotapi-java</url>
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/bigboxer23/switch_bot/SwitchBotApi.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -78,18 +79,19 @@ protected RequestBuilderCallback addAuth() {
* @param apiResponse the API response
* @return true if error occurs
*/
protected boolean checkForError(Response response, Optional<IApiResponse> apiResponse) {
protected IApiResponse checkForError(Response response, Optional<IApiResponse> 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());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@ public IApiResponse sendDeviceControlCommands(String deviceId, DeviceCommand com
* @throws IOException
*/
private <T extends IApiResponse> T parseResponse(Response response, Class<T> clazz) throws IOException {
Optional<T> apiResponse = OkHttpUtil.getBody(response, clazz);
if (!provider.checkForError(response, (Optional<IApiResponse>) apiResponse)) {
throw new IOException(response.code() + " " + response.message());
IApiResponse apiResponse =
provider.checkForError(response, (Optional<IApiResponse>) OkHttpUtil.getBody(response, clazz));
if (!apiResponse.isSuccess()) {
throw new IOException(apiResponse.getStatusCode() + " " + apiResponse.getMessage());
}
return apiResponse.get();
return (T) apiResponse;
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/bigboxer23/switch_bot/data/ApiResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public class ApiResponse implements IApiResponse {
private ApiResponseBody body;

private String message;

private boolean success = true;
}
18 changes: 18 additions & 0 deletions src/main/java/com/bigboxer23/switch_bot/data/BadApiResponse.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public class DeviceApiResponse implements IApiResponse {
private Device body;

private String message;

private boolean success = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}