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;
23 import javax.ws.rs.core.UriBuilder;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.eclipse.jetty.http.HttpMethod;
28 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.FeatureArea;
29 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.Scope;
30 import org.openhab.binding.netatmo.internal.handler.ApiBridgeHandler;
33 * Base class for all various rest managers
35 * @author Gaƫl L'hopital - Initial contribution
38 public abstract class RestManager {
39 private static final UriBuilder API_BASE_BUILDER = UriBuilder.fromUri(URL_API);
40 private static final UriBuilder APP_URI_BUILDER = UriBuilder.fromUri(URL_APP).path(PATH_API);
41 private static final UriBuilder API_URI_BUILDER = getApiBaseBuilder().path(PATH_API);
43 private final Set<Scope> requiredScopes;
44 private final ApiBridgeHandler apiBridge;
46 public RestManager(ApiBridgeHandler apiBridge, FeatureArea features) {
47 this.requiredScopes = features.scopes;
48 this.apiBridge = apiBridge;
51 protected <T extends ApiResponse<?>> T get(UriBuilder uriBuilder, Class<T> clazz) throws NetatmoException {
52 return executeUri(uriBuilder, HttpMethod.GET, clazz, null, null);
55 protected <T extends ApiResponse<?>> T post(UriBuilder uriBuilder, Class<T> clazz, @Nullable String payload)
56 throws NetatmoException {
57 return executeUri(uriBuilder, HttpMethod.POST, clazz, payload, payload == null ? null : CONTENT_APP_JSON);
60 protected <T> T post(URI uri, Class<T> clazz, Map<String, String> entries) throws NetatmoException {
61 return apiBridge.executeUri(uri, POST, clazz, toRequest(entries), CONTENT_APP_FORM, 3);
64 private <T extends ApiResponse<?>> T executeUri(UriBuilder uriBuilder, HttpMethod method, Class<T> clazz,
65 @Nullable String payload, @Nullable String contentType) throws NetatmoException {
66 URI uri = uriBuilder.build();
67 T response = apiBridge.executeUri(uri, method, clazz, payload, contentType, 3);
68 if (response instanceof ApiResponse.Ok && ((ApiResponse.Ok) response).failed()) {
69 throw new NetatmoException("Command failed : %s for uri : %s", response.getStatus(), uri.toString());
74 private static UriBuilder appendParams(UriBuilder builder, @Nullable Object... params) {
75 if (params.length % 2 != 0) {
76 throw new IllegalArgumentException("appendParams : params count must be even");
78 for (int i = 0; i < params.length; i += 2) {
79 Object query = params[i];
80 if (query instanceof String) {
81 Object param = params[i + 1];
83 builder.queryParam((String) query, param);
86 throw new IllegalArgumentException("appendParams : even parameters must be Strings");
92 protected static UriBuilder getApiBaseBuilder() {
93 return API_BASE_BUILDER.clone();
96 public static UriBuilder getApiUriBuilder(String path, @Nullable Object... params) {
97 return appendParams(API_URI_BUILDER.clone().path(path), params);
100 protected static UriBuilder getAppUriBuilder(String path, @Nullable Object... params) {
101 return appendParams(APP_URI_BUILDER.clone().path(path), params);
104 private String toRequest(Map<String, String> entries) {
105 return entries.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining("&"));
108 public Set<Scope> getRequiredScopes() {
109 return requiredScopes;