]> git.basschouten.com Git - openhab-addons.git/commitdiff
[hydrawise] Fixes occasional connection issues (#15177)
authorDan Cunningham <dan@digitaldan.com>
Tue, 4 Jul 2023 19:29:03 +0000 (12:29 -0700)
committerGitHub <noreply@github.com>
Tue, 4 Jul 2023 19:29:03 +0000 (21:29 +0200)
* 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>
bundles/org.openhab.binding.hydrawise/src/main/java/org/openhab/binding/hydrawise/internal/api/graphql/HydrawiseGraphQLClient.java

index a647317b29b683537a298d75e2f752bf4d0ae019..a3a5c754206c745c633d526131537b76e039e00e 100644 (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);