2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.shelly.internal.api;
15 import static org.eclipse.jetty.http.HttpStatus.*;
16 import static org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.*;
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;
24 * The {@link ShellyApiResult} wraps up the API result and provides some more information like url, http code, received
27 * @author Markus Michels - Initial contribution
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 = "";
38 public ShellyApiResult() {
41 public ShellyApiResult(String method, String url) {
46 public ShellyApiResult(ContentResponse contentResponse) {
47 fillFromResponse(contentResponse);
50 public String getUrl() {
51 return !url.isEmpty() ? method + " " + url : "";
54 public String getHttpResponse() {
59 public String toString() {
60 return getUrl() + " > " + getHttpResponse();
63 public boolean isHttpOk() {
64 return httpCode == OK_200;
67 public boolean isNotFound() {
68 return httpCode == NOT_FOUND_404;
71 public boolean isHttpAccessUnauthorized() {
72 return (httpCode == UNAUTHORIZED_401 || response.contains(SHELLY_APIERR_UNAUTHORIZED));
75 public boolean isHttpTimeout() {
76 return httpCode == -1 || response.toUpperCase().contains(SHELLY_APIERR_TIMEOUT.toLowerCase());
79 public boolean isHttpServerError() {
80 return httpCode == INTERNAL_SERVER_ERROR_500;
83 public boolean isNotCalibrtated() {
84 return getHttpResponse().contains(SHELLY_APIERR_NOT_CALIBRATED);
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();
94 Request request = contentResponse.getRequest();
95 if (request != null) {
96 url = request.getURI().toString();
97 method = request.getMethod();