]> git.basschouten.com Git - openhab-addons.git/blob
2cd431e4772afc8b4f7df6e41153473b9462db08
[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 import java.util.stream.Stream;
23
24 import javax.ws.rs.core.UriBuilder;
25
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;
32
33 /**
34  * Base class for all various rest managers
35  *
36  * @author GaĆ«l L'hopital - Initial contribution
37  */
38 @NonNullByDefault
39 public abstract class RestManager {
40     private static final UriBuilder API_URI_BUILDER = getApiBaseBuilder(PATH_API);
41
42     private final Set<Scope> requiredScopes;
43     protected final ApiBridgeHandler apiBridge;
44
45     public RestManager(ApiBridgeHandler apiBridge, FeatureArea features) {
46         this.requiredScopes = features.scopes;
47         this.apiBridge = apiBridge;
48     }
49
50     protected <T extends ApiResponse<?>> T get(UriBuilder uriBuilder, Class<T> clazz) throws NetatmoException {
51         return executeUri(uriBuilder, HttpMethod.GET, clazz, null, null);
52     }
53
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);
57     }
58
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);
61     }
62
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());
69         }
70         return response;
71     }
72
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");
76         }
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);
83                 }
84             } else {
85                 throw new IllegalArgumentException("appendParams : even parameters must be Strings");
86             }
87         }
88         return builder;
89     }
90
91     protected static UriBuilder getApiBaseBuilder(String... paths) {
92         UriBuilder builder = UriBuilder.fromUri(URL_API);
93         Stream.of(paths).forEach(path -> builder.path(path));
94         return builder;
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     private String toRequest(Map<String, String> entries) {
102         return entries.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining("&"));
103     }
104
105     public Set<Scope> getRequiredScopes() {
106         return requiredScopes;
107     }
108 }