From d83084849dde93820125a7efffb33dae73e0e34c Mon Sep 17 00:00:00 2001 From: Dan Cunningham Date: Tue, 4 Jul 2023 12:29:03 -0700 Subject: [PATCH] [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 --- .../api/graphql/HydrawiseGraphQLClient.java | 44 ++++++++++++------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/bundles/org.openhab.binding.hydrawise/src/main/java/org/openhab/binding/hydrawise/internal/api/graphql/HydrawiseGraphQLClient.java b/bundles/org.openhab.binding.hydrawise/src/main/java/org/openhab/binding/hydrawise/internal/api/graphql/HydrawiseGraphQLClient.java index a647317b29b..a3a5c754206 100644 --- a/bundles/org.openhab.binding.hydrawise/src/main/java/org/openhab/binding/hydrawise/internal/api/graphql/HydrawiseGraphQLClient.java +++ b/bundles/org.openhab.binding.hydrawise/src/main/java/org/openhab/binding/hydrawise/internal/api/graphql/HydrawiseGraphQLClient.java @@ -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 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 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);