[hydrawise] Fixes occasional connection issues (#15177)

* 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 #15170

Signed-off-by: Dan Cunningham <dan@digitaldan.com>
This commit is contained in:
Dan Cunningham 2023-07-04 12:29:03 -07:00 committed by GitHub
parent 3f6b2b7231
commit d83084849d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -30,6 +30,7 @@ import org.eclipse.jetty.client.api.ContentResponse;
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;
@ -59,6 +60,7 @@ import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;
/**
*
@ -107,15 +109,18 @@ public class HydrawiseGraphQLClient {
*/
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);
}
/***
@ -262,16 +267,20 @@ public class HydrawiseGraphQLClient {
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);
}
}
@ -301,6 +310,11 @@ public class HydrawiseGraphQLClient {
}).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);