2 * Copyright (c) 2010-2020 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 isHttpAccessUnauthorized() {
67 return (httpCode == UNAUTHORIZED_401 || response.contains(SHELLY_APIERR_UNAUTHORIZED));
70 public boolean isHttpTimeout() {
71 return httpCode == -1 || response.toUpperCase().contains(SHELLY_APIERR_TIMEOUT.toLowerCase());
74 public boolean isHttpServerError() {
75 return httpCode == INTERNAL_SERVER_ERROR_500;
78 public boolean isNotCalibrtated() {
79 return getHttpResponse().contains(SHELLY_APIERR_NOT_CALIBRATED);
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();
89 Request request = contentResponse.getRequest();
90 if (request != null) {
91 url = request.getURI().toString();
92 method = request.getMethod();