2 * Copyright (c) 2010-2022 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 @Nullable String contentType) throws NetatmoException {
57 return executeUri(uriBuilder, HttpMethod.POST, clazz, payload, contentType);
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),
62 "application/x-www-form-urlencoded;charset=UTF-8", 3);
65 private <T extends ApiResponse<?>> T executeUri(UriBuilder uriBuilder, HttpMethod method, Class<T> clazz,
66 @Nullable String payload, @Nullable String contentType) throws NetatmoException {
67 URI uri = uriBuilder.build();
68 T response = apiBridge.executeUri(uri, method, clazz, payload, contentType, 3);
69 if (response instanceof ApiResponse.Ok && ((ApiResponse.Ok) response).failed()) {
70 throw new NetatmoException("Command failed : %s for uri : %s", response.getStatus(), uri.toString());
75 private static UriBuilder appendParams(UriBuilder builder, @Nullable Object... params) {
76 if (params.length % 2 != 0) {
77 throw new IllegalArgumentException("appendParams : params count must be even");
79 for (int i = 0; i < params.length; i += 2) {
80 Object query = params[i];
81 if (query instanceof String) {
82 Object param = params[i + 1];
84 builder.queryParam((String) query, param);
87 throw new IllegalArgumentException("appendParams : even parameters must be Strings");
93 protected static UriBuilder getApiBaseBuilder() {
94 return API_BASE_BUILDER.clone();
97 public static UriBuilder getApiUriBuilder(String path, @Nullable Object... params) {
98 return appendParams(API_URI_BUILDER.clone().path(path), params);
101 protected static UriBuilder getAppUriBuilder(String path, @Nullable Object... params) {
102 return appendParams(APP_URI_BUILDER.clone().path(path), params);
105 private String toRequest(Map<String, String> entries) {
106 return entries.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining("&"));
109 public Set<Scope> getRequiredScopes() {
110 return requiredScopes;