2 * Copyright (c) 2010-2022 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.api.ShellyApiJsonDTO.*;
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 = "";
37 public ShellyApiResult() {
40 public ShellyApiResult(String method, String url) {
45 public ShellyApiResult(ContentResponse contentResponse) {
46 fillFromResponse(contentResponse);
49 public String getUrl() {
50 return !url.isEmpty() ? method + " " + url : "";
53 public String getHttpResponse() {
58 public String toString() {
59 return getUrl() + " > " + getHttpResponse();
62 public boolean isHttpOk() {
63 return httpCode == OK_200;
66 public boolean isNotFound() {
67 return httpCode == NOT_FOUND_404;
70 public boolean isHttpAccessUnauthorized() {
71 return (httpCode == UNAUTHORIZED_401 || response.contains(SHELLY_APIERR_UNAUTHORIZED));
74 public boolean isHttpTimeout() {
75 return httpCode == -1 || response.toUpperCase().contains(SHELLY_APIERR_TIMEOUT.toLowerCase());
78 public boolean isHttpServerError() {
79 return httpCode == INTERNAL_SERVER_ERROR_500;
82 public boolean isNotCalibrtated() {
83 return getHttpResponse().contains(SHELLY_APIERR_NOT_CALIBRATED);
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();
93 Request request = contentResponse.getRequest();
94 if (request != null) {
95 url = request.getURI().toString();
96 method = request.getMethod();