Skip to content

Commit

Permalink
[hydrawise] Fixes occasional connection issues (openhab#15177)
Browse files Browse the repository at this point in the history
* The Hydrawise API can return sometimes return a non JSON response during service outages, like when they are updating thier software.  This treats this as a connection error and allows the client to try the poll again.
* Adds additional http status checking

Fixes openhab#15170

Signed-off-by: Dan Cunningham <dan@digitaldan.com>
Signed-off-by: Matt Myers <mmyers75@icloud.com>
  • Loading branch information
digitaldan authored and matchews committed Aug 9, 2023
1 parent 368892f commit 07e9cb2
Showing 1 changed file with 29 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.client.util.StringContentProvider;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.openhab.binding.hydrawise.internal.api.HydrawiseAuthenticationException;
import org.openhab.binding.hydrawise.internal.api.HydrawiseCommandException;
import org.openhab.binding.hydrawise.internal.api.HydrawiseConnectionException;
Expand Down Expand Up @@ -59,6 +60,7 @@
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;

/**
*
Expand Down Expand Up @@ -107,15 +109,18 @@ public HydrawiseGraphQLClient(HttpClient httpClient, OAuthClientService oAuthSer
*/
public @Nullable QueryResponse queryControllers()
throws HydrawiseConnectionException, HydrawiseAuthenticationException {
QueryRequest query;
try {
query = new QueryRequest(getQueryString());
QueryRequest query = new QueryRequest(getQueryString());
String queryJson = gson.toJson(query);
String response = sendGraphQLQuery(queryJson);
try {
return gson.fromJson(response, QueryResponse.class);
} catch (JsonSyntaxException e) {
throw new HydrawiseConnectionException("Invalid Response: " + response);
}
} catch (IOException e) {
throw new HydrawiseConnectionException(e);
}
String queryJson = gson.toJson(query);
String response = sendGraphQLQuery(queryJson);
return gson.fromJson(response, QueryResponse.class);
}

/***
Expand Down Expand Up @@ -262,16 +267,20 @@ private void sendGraphQLMutation(String content)
logger.debug("Sending Mutation {}", gson.toJson(mutation).toString());
String response = sendGraphQLRequest(gson.toJson(mutation).toString());
logger.debug("Mutation response {}", response);
MutationResponse mResponse = gson.fromJson(response, MutationResponse.class);
if (mResponse == null) {
throw new HydrawiseCommandException("Malformed response: " + response);
}
Optional<MutationResponseStatus> status = mResponse.data.values().stream().findFirst();
if (!status.isPresent()) {
throw new HydrawiseCommandException("Unknown response: " + response);
}
if (status.get().status != StatusCode.OK) {
throw new HydrawiseCommandException("Command Status: " + status.get().status.name());
try {
MutationResponse mResponse = gson.fromJson(response, MutationResponse.class);
if (mResponse == null) {
throw new HydrawiseCommandException("Malformed response: " + response);
}
Optional<MutationResponseStatus> status = mResponse.data.values().stream().findFirst();
if (!status.isPresent()) {
throw new HydrawiseCommandException("Unknown response: " + response);
}
if (status.get().status != StatusCode.OK) {
throw new HydrawiseCommandException("Command Status: " + status.get().status.name());
}
} catch (JsonSyntaxException e) {
throw new HydrawiseConnectionException("Invalid Response: " + response);
}
}

Expand Down Expand Up @@ -301,6 +310,11 @@ public void onFailure(@Nullable Response response, @Nullable Throwable failure)
}).send();
String stringResponse = response.getContentAsString();
logger.trace("Received Response: {}", stringResponse);
int statusCode = response.getStatus();
if (!HttpStatus.isSuccess(statusCode)) {
throw new HydrawiseConnectionException(
"Request failed with HTTP status code: " + statusCode + " response: " + stringResponse);
}
return stringResponse;
} catch (InterruptedException | TimeoutException | OAuthException | IOException e) {
logger.debug("Could not send request", e);
Expand Down

0 comments on commit 07e9cb2

Please sign in to comment.