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.airgradient.internal.communication;
15 import static org.openhab.binding.airgradient.internal.AirGradientBindingConstants.CALIBRATE_CO2_PATH;
16 import static org.openhab.binding.airgradient.internal.AirGradientBindingConstants.CURRENT_MEASURES_PATH;
17 import static org.openhab.binding.airgradient.internal.AirGradientBindingConstants.LEDS_MODE_PATH;
18 import static org.openhab.binding.airgradient.internal.AirGradientBindingConstants.REQUEST_TIMEOUT;
20 import java.util.concurrent.TimeUnit;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.eclipse.jetty.client.HttpClient;
25 import org.eclipse.jetty.client.api.Request;
26 import org.eclipse.jetty.http.HttpMethod;
27 import org.openhab.binding.airgradient.internal.config.AirGradientAPIConfiguration;
30 * Helper for doing rest calls to the API.
32 * @author Jørgen Austvik - Initial contribution
35 public class RESTHelper {
37 public static @Nullable String generateMeasuresUrl(AirGradientAPIConfiguration apiConfig) {
38 if (apiConfig.hasCloudUrl()) {
39 return apiConfig.hostname + String.format(CURRENT_MEASURES_PATH, apiConfig.token);
41 return apiConfig.hostname;
45 public static @Nullable String generateCalibrationCo2Url(AirGradientAPIConfiguration apiConfig, String serialNo) {
46 if (apiConfig.hasCloudUrl()) {
47 return apiConfig.hostname + String.format(CALIBRATE_CO2_PATH, serialNo, apiConfig.token);
49 return apiConfig.hostname;
53 public static @Nullable String generateGetLedsModeUrl(AirGradientAPIConfiguration apiConfig, String serialNo) {
54 if (apiConfig.hasCloudUrl()) {
55 return apiConfig.hostname + String.format(LEDS_MODE_PATH, serialNo, apiConfig.token);
57 return apiConfig.hostname;
61 public static @Nullable Request generateRequest(HttpClient httpClient, @Nullable String url) {
62 return generateRequest(httpClient, url, HttpMethod.GET);
65 public static @Nullable Request generateRequest(HttpClient httpClient, @Nullable String url, HttpMethod method) {
70 Request request = httpClient.newRequest(url);
71 request.timeout(REQUEST_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
72 request.method(method);