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.netatmo.internal.api;
15 import static org.eclipse.jetty.http.HttpMethod.POST;
16 import static org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.*;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
24 import javax.ws.rs.core.UriBuilder;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.eclipse.jetty.http.HttpMethod;
29 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.FeatureArea;
30 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.Scope;
31 import org.openhab.binding.netatmo.internal.handler.ApiBridgeHandler;
34 * Base class for all various rest managers
36 * @author Gaƫl L'hopital - Initial contribution
39 public abstract class RestManager {
40 private static final UriBuilder API_URI_BUILDER = getApiBaseBuilder(PATH_API);
42 private final Set<Scope> requiredScopes;
43 private final ApiBridgeHandler apiBridge;
45 public RestManager(ApiBridgeHandler apiBridge, FeatureArea features) {
46 this.requiredScopes = features.scopes;
47 this.apiBridge = apiBridge;
50 protected <T extends ApiResponse<?>> T get(UriBuilder uriBuilder, Class<T> clazz) throws NetatmoException {
51 return executeUri(uriBuilder, HttpMethod.GET, clazz, null, null);
54 protected <T extends ApiResponse<?>> T post(UriBuilder uriBuilder, Class<T> clazz, @Nullable String payload)
55 throws NetatmoException {
56 return executeUri(uriBuilder, HttpMethod.POST, clazz, payload, payload == null ? null : CONTENT_APP_JSON);
59 protected <T> T post(URI uri, Class<T> clazz, Map<String, String> entries) throws NetatmoException {
60 return apiBridge.executeUri(uri, POST, clazz, toRequest(entries), CONTENT_APP_FORM, 3);
63 private <T extends ApiResponse<?>> T executeUri(UriBuilder uriBuilder, HttpMethod method, Class<T> clazz,
64 @Nullable String payload, @Nullable String contentType) throws NetatmoException {
65 URI uri = uriBuilder.build();
66 T response = apiBridge.executeUri(uri, method, clazz, payload, contentType, 3);
67 if (response instanceof ApiResponse.Ok && ((ApiResponse.Ok) response).failed()) {
68 throw new NetatmoException("Command failed : %s for uri : %s", response.getStatus(), uri.toString());
73 private static UriBuilder appendParams(UriBuilder builder, @Nullable Object... params) {
74 if (params.length % 2 != 0) {
75 throw new IllegalArgumentException("appendParams : params count must be even");
77 for (int i = 0; i < params.length; i += 2) {
78 Object param1 = params[i];
79 Object param2 = params[i + 1];
80 if (param1 instanceof String query) {
81 if (param2 != null) { // or else just ignore this query element
82 builder.queryParam(query, param2);
85 throw new IllegalArgumentException("appendParams : even parameters must be Strings");
91 protected static UriBuilder getApiBaseBuilder(String... paths) {
92 UriBuilder builder = UriBuilder.fromUri(URL_API);
93 Stream.of(paths).forEach(path -> builder.path(path));
97 public static UriBuilder getApiUriBuilder(String path, @Nullable Object... params) {
98 return appendParams(API_URI_BUILDER.clone().path(path), params);
101 private String toRequest(Map<String, String> entries) {
102 return entries.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining("&"));
105 public Set<Scope> getRequiredScopes() {
106 return requiredScopes;