]> git.basschouten.com Git - openhab-addons.git/blob
aacbdedfa43dd2ad30950d672617d05bec5fd650
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.netatmo.internal.api;
14
15 import static org.eclipse.jetty.http.HttpMethod.POST;
16 import static org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.*;
17
18 import java.net.URI;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.stream.Collectors;
22
23 import javax.ws.rs.core.UriBuilder;
24
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;
31
32 /**
33  * Base class for all various rest managers
34  *
35  * @author GaĆ«l L'hopital - Initial contribution
36  */
37 @NonNullByDefault
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);
42
43     private final Set<Scope> requiredScopes;
44     private final ApiBridgeHandler apiBridge;
45
46     public RestManager(ApiBridgeHandler apiBridge, FeatureArea features) {
47         this.requiredScopes = features.scopes;
48         this.apiBridge = apiBridge;
49     }
50
51     protected <T extends ApiResponse<?>> T get(UriBuilder uriBuilder, Class<T> clazz) throws NetatmoException {
52         return executeUri(uriBuilder, HttpMethod.GET, clazz, null, null);
53     }
54
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);
58     }
59
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);
62     }
63
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());
70         }
71         return response;
72     }
73
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");
77         }
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];
82                 if (param != null) {
83                     builder.queryParam((String) query, param);
84                 }
85             } else {
86                 throw new IllegalArgumentException("appendParams : even parameters must be Strings");
87             }
88         }
89         return builder;
90     }
91
92     protected static UriBuilder getApiBaseBuilder() {
93         return API_BASE_BUILDER.clone();
94     }
95
96     public static UriBuilder getApiUriBuilder(String path, @Nullable Object... params) {
97         return appendParams(API_URI_BUILDER.clone().path(path), params);
98     }
99
100     protected static UriBuilder getAppUriBuilder(String path, @Nullable Object... params) {
101         return appendParams(APP_URI_BUILDER.clone().path(path), params);
102     }
103
104     private String toRequest(Map<String, String> entries) {
105         return entries.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining("&"));
106     }
107
108     public Set<Scope> getRequiredScopes() {
109         return requiredScopes;
110     }
111 }