]> git.basschouten.com Git - openhab-addons.git/blob
a522e1aa06d5f067400b84b5758df203261c5e6b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.airgradient.internal.communication;
14
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;
19
20 import java.util.concurrent.TimeUnit;
21
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;
28
29 /**
30  * Helper for doing rest calls to the API.
31  *
32  * @author Jørgen Austvik - Initial contribution
33  */
34 @NonNullByDefault
35 public class RESTHelper {
36
37     public static @Nullable String generateMeasuresUrl(AirGradientAPIConfiguration apiConfig) {
38         if (apiConfig.hasCloudUrl()) {
39             return apiConfig.hostname + String.format(CURRENT_MEASURES_PATH, apiConfig.token);
40         } else {
41             return apiConfig.hostname;
42         }
43     }
44
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);
48         } else {
49             return apiConfig.hostname;
50         }
51     }
52
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);
56         } else {
57             return apiConfig.hostname;
58         }
59     }
60
61     public static @Nullable Request generateRequest(HttpClient httpClient, @Nullable String url) {
62         return generateRequest(httpClient, url, HttpMethod.GET);
63     }
64
65     public static @Nullable Request generateRequest(HttpClient httpClient, @Nullable String url, HttpMethod method) {
66         if (url == null) {
67             return null;
68         }
69
70         Request request = httpClient.newRequest(url);
71         request.timeout(REQUEST_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
72         request.method(method);
73         return request;
74     }
75 }