]> git.basschouten.com Git - openhab-addons.git/blob
c556a8a0a900fdf102bf62d6b0ee6520a51d3993
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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             @Nullable String contentType) throws NetatmoException {
57         return executeUri(uriBuilder, HttpMethod.POST, clazz, payload, contentType);
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),
62                 "application/x-www-form-urlencoded;charset=UTF-8", 3);
63     }
64
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());
71         }
72         return response;
73     }
74
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");
78         }
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];
83                 if (param != null) {
84                     builder.queryParam((String) query, param);
85                 }
86             } else {
87                 throw new IllegalArgumentException("appendParams : even parameters must be Strings");
88             }
89         }
90         return builder;
91     }
92
93     protected static UriBuilder getApiBaseBuilder() {
94         return API_BASE_BUILDER.clone();
95     }
96
97     public static UriBuilder getApiUriBuilder(String path, @Nullable Object... params) {
98         return appendParams(API_URI_BUILDER.clone().path(path), params);
99     }
100
101     protected static UriBuilder getAppUriBuilder(String path, @Nullable Object... params) {
102         return appendParams(APP_URI_BUILDER.clone().path(path), params);
103     }
104
105     private String toRequest(Map<String, String> entries) {
106         return entries.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining("&"));
107     }
108
109     public Set<Scope> getRequiredScopes() {
110         return requiredScopes;
111     }
112 }