]> git.basschouten.com Git - openhab-addons.git/blob
4cadd3f96af207a32f658884bb71f4d7fdb33e9a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.api.ShellyApiJsonDTO.*;
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
37     public ShellyApiResult() {
38     }
39
40     public ShellyApiResult(String method, String url) {
41         this.method = method;
42         this.url = url;
43     }
44
45     public ShellyApiResult(ContentResponse contentResponse) {
46         fillFromResponse(contentResponse);
47     }
48
49     public String getUrl() {
50         return !url.isEmpty() ? method + " " + url : "";
51     }
52
53     public String getHttpResponse() {
54         return response;
55     }
56
57     @Override
58     public String toString() {
59         return getUrl() + " > " + getHttpResponse();
60     }
61
62     public boolean isHttpOk() {
63         return httpCode == OK_200;
64     }
65
66     public boolean isNotFound() {
67         return httpCode == NOT_FOUND_404;
68     }
69
70     public boolean isHttpAccessUnauthorized() {
71         return (httpCode == UNAUTHORIZED_401 || response.contains(SHELLY_APIERR_UNAUTHORIZED));
72     }
73
74     public boolean isHttpTimeout() {
75         return httpCode == -1 || response.toUpperCase().contains(SHELLY_APIERR_TIMEOUT.toLowerCase());
76     }
77
78     public boolean isHttpServerError() {
79         return httpCode == INTERNAL_SERVER_ERROR_500;
80     }
81
82     public boolean isNotCalibrtated() {
83         return getHttpResponse().contains(SHELLY_APIERR_NOT_CALIBRATED);
84     }
85
86     private void fillFromResponse(@Nullable ContentResponse contentResponse) {
87         if (contentResponse != null) {
88             String r = contentResponse.getContentAsString();
89             response = r != null ? r : "";
90             httpCode = contentResponse.getStatus();
91             httpReason = contentResponse.getReason();
92
93             Request request = contentResponse.getRequest();
94             if (request != null) {
95                 url = request.getURI().toString();
96                 method = request.getMethod();
97             }
98         }
99     }
100 }