]> git.basschouten.com Git - openhab-addons.git/blob
4768c67b6cdee2592989e7961d6be7b17a2ea640
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.shelly.internal.api;
14
15 import static org.eclipse.jetty.http.HttpStatus.*;
16 import static org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.*;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.eclipse.jetty.client.api.ContentResponse;
21 import org.eclipse.jetty.client.api.Request;
22
23 /**
24  * The {@link ShellyApiResult} wraps up the API result and provides some more information like url, http code, received
25  * response etc.
26  *
27  * @author Markus Michels - Initial contribution
28  */
29 @NonNullByDefault
30 public class ShellyApiResult {
31     public String url = "";
32     public String method = "";
33     public String response = "";
34     public int httpCode = -1;
35     public String httpReason = "";
36     public String authChallenge = "";
37
38     public ShellyApiResult() {
39     }
40
41     public ShellyApiResult(String method, String url) {
42         this.method = method;
43         this.url = url;
44     }
45
46     public ShellyApiResult(ContentResponse contentResponse) {
47         fillFromResponse(contentResponse);
48     }
49
50     public String getUrl() {
51         return !url.isEmpty() ? method + " " + url : "";
52     }
53
54     public String getHttpResponse() {
55         return response;
56     }
57
58     @Override
59     public String toString() {
60         return getUrl() + " > " + getHttpResponse();
61     }
62
63     public boolean isHttpOk() {
64         return httpCode == OK_200;
65     }
66
67     public boolean isNotFound() {
68         return httpCode == NOT_FOUND_404;
69     }
70
71     public boolean isHttpAccessUnauthorized() {
72         return (httpCode == UNAUTHORIZED_401 || response.contains(SHELLY_APIERR_UNAUTHORIZED));
73     }
74
75     public boolean isHttpTimeout() {
76         return httpCode == -1 || response.toUpperCase().contains(SHELLY_APIERR_TIMEOUT.toLowerCase());
77     }
78
79     public boolean isHttpServerError() {
80         return httpCode == INTERNAL_SERVER_ERROR_500;
81     }
82
83     public boolean isNotCalibrtated() {
84         return getHttpResponse().contains(SHELLY_APIERR_NOT_CALIBRATED);
85     }
86
87     private void fillFromResponse(@Nullable ContentResponse contentResponse) {
88         if (contentResponse != null) {
89             String r = contentResponse.getContentAsString();
90             response = r != null ? r : "";
91             httpCode = contentResponse.getStatus();
92             httpReason = contentResponse.getReason();
93
94             Request request = contentResponse.getRequest();
95             if (request != null) {
96                 url = request.getURI().toString();
97                 method = request.getMethod();
98             }
99         }
100     }
101 }