]> git.basschouten.com Git - openhab-addons.git/blob
b1a2bc143098d0449b85fb74ddbf37fccc23bd19
[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 isHttpAccessUnauthorized() {
67         return (httpCode == UNAUTHORIZED_401 || response.contains(SHELLY_APIERR_UNAUTHORIZED));
68     }
69
70     public boolean isHttpTimeout() {
71         return httpCode == -1 || response.toUpperCase().contains(SHELLY_APIERR_TIMEOUT.toLowerCase());
72     }
73
74     public boolean isHttpServerError() {
75         return httpCode == INTERNAL_SERVER_ERROR_500;
76     }
77
78     public boolean isNotCalibrtated() {
79         return getHttpResponse().contains(SHELLY_APIERR_NOT_CALIBRATED);
80     }
81
82     private void fillFromResponse(@Nullable ContentResponse contentResponse) {
83         if (contentResponse != null) {
84             String r = contentResponse.getContentAsString();
85             response = r != null ? r : "";
86             httpCode = contentResponse.getStatus();
87             httpReason = contentResponse.getReason();
88
89             Request request = contentResponse.getRequest();
90             if (request != null) {
91                 url = request.getURI().toString();
92                 method = request.getMethod();
93             }
94         }
95     }
96 }