2 * Copyright (c) 2010-2023 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.intesis.internal.api;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.client.HttpClient;
22 import org.eclipse.jetty.client.api.ContentResponse;
23 import org.eclipse.jetty.client.api.Request;
24 import org.eclipse.jetty.client.util.StringContentProvider;
25 import org.eclipse.jetty.http.HttpHeader;
26 import org.openhab.binding.intesis.internal.config.IntesisHomeConfiguration;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * {@link IntesisHomeHttpApi} wraps the IntesisHome REST API and provides various low level function to access the
32 * device api (not cloud api).
34 * @author Hans-Jörg Merk - Initial contribution
37 public class IntesisHomeHttpApi {
38 public static final String CONTENT_TYPE_JSON = "application/json; charset=UTF-8";
40 private final Logger logger = LoggerFactory.getLogger(IntesisHomeHttpApi.class);
41 private final HttpClient httpClient;
43 public IntesisHomeHttpApi(IntesisHomeConfiguration config, HttpClient httpClient) {
44 this.httpClient = httpClient;
48 * Used to post a request to the device
50 * @param ipAddress of the device
51 * @param content string
52 * @return JSON string as response
55 public String postRequest(String ipAddress, String contentString) {
56 String url = "http://" + ipAddress + "/api.cgi";
58 Request request = httpClient.POST(url);
59 request.header(HttpHeader.CONTENT_TYPE, "application/json");
60 request.content(new StringContentProvider(contentString), "application/json");
62 ContentResponse contentResponse = request.timeout(5, TimeUnit.SECONDS).send();
64 String response = contentResponse.getContentAsString().replace("\t", "").replace("\r\n", "").trim();
66 if (response != null && !response.isEmpty()) {
71 } catch (TimeoutException | InterruptedException | ExecutionException e) {
72 logger.debug("Could not make HTTP Post request");