]> git.basschouten.com Git - openhab-addons.git/blob
e960bfc58d688af1ba7fd37a848eb4ee01c38ed5
[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.volvooncall.internal.api;
14
15 import java.io.IOException;
16 import java.nio.charset.StandardCharsets;
17 import java.util.Objects;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.TimeoutException;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.eclipse.jetty.client.HttpClient;
25 import org.eclipse.jetty.client.api.ContentProvider;
26 import org.eclipse.jetty.client.api.ContentResponse;
27 import org.eclipse.jetty.client.api.Request;
28 import org.eclipse.jetty.client.util.StringContentProvider;
29 import org.eclipse.jetty.http.HttpField;
30 import org.eclipse.jetty.http.HttpHeader;
31 import org.eclipse.jetty.http.HttpMethod;
32 import org.openhab.binding.volvooncall.internal.VolvoOnCallException;
33 import org.openhab.binding.volvooncall.internal.VolvoOnCallException.ErrorType;
34 import org.openhab.binding.volvooncall.internal.config.ApiBridgeConfiguration;
35 import org.openhab.binding.volvooncall.internal.dto.PostResponse;
36 import org.openhab.binding.volvooncall.internal.dto.VocAnswer;
37 import org.openhab.core.cache.ExpiringCacheMap;
38 import org.openhab.core.id.InstanceUUID;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import com.google.gson.Gson;
43 import com.google.gson.JsonSyntaxException;
44
45 /**
46  * {@link VocHttpApi} wraps the VolvoOnCall REST API.
47  *
48  * @author GaĆ«l L'hopital - Initial contribution
49  */
50 @NonNullByDefault
51 public class VocHttpApi {
52     // The URL to use to connect to VocAPI.
53     // For North America and China syntax changes to vocapi-cn.xxx
54     private static final String SERVICE_URL = "https://vocapi.wirelesscar.net/customerapi/rest/v3.0/";
55     private static final int TIMEOUT_MS = 10000;
56     private static final String JSON_CONTENT_TYPE = "application/json";
57
58     private final Logger logger = LoggerFactory.getLogger(VocHttpApi.class);
59     private final Gson gson;
60     private final ExpiringCacheMap<String, @Nullable String> cache;
61     private final HttpClient httpClient;
62     private final ApiBridgeConfiguration configuration;
63
64     public VocHttpApi(ApiBridgeConfiguration configuration, Gson gson, HttpClient httpClient)
65             throws VolvoOnCallException {
66         this.gson = gson;
67         this.cache = new ExpiringCacheMap<>(120 * 1000);
68         this.configuration = configuration;
69         this.httpClient = httpClient;
70
71         httpClient.setUserAgentField(new HttpField(HttpHeader.USER_AGENT, "openhab/voc_binding/" + InstanceUUID.get()));
72         try {
73             httpClient.start();
74         } catch (Exception e) {
75             throw new VolvoOnCallException(new IOException("Unable to start Jetty HttpClient", e));
76         }
77     }
78
79     public void dispose() throws Exception {
80         httpClient.stop();
81     }
82
83     private @Nullable String getResponse(HttpMethod method, String url, @Nullable String body) {
84         try {
85             Request request = httpClient.newRequest(url).header(HttpHeader.CACHE_CONTROL, "no-cache")
86                     .header(HttpHeader.CONTENT_TYPE, JSON_CONTENT_TYPE).header(HttpHeader.ACCEPT, "*/*")
87                     .header(HttpHeader.AUTHORIZATION, configuration.getAuthorization()).header("x-device-id", "Device")
88                     .header("x-originator-type", "App").header("x-os-type", "Android").header("x-os-version", "22")
89                     .timeout(TIMEOUT_MS, TimeUnit.MILLISECONDS);
90             if (body != null) {
91                 ContentProvider content = new StringContentProvider(JSON_CONTENT_TYPE, body, StandardCharsets.UTF_8);
92                 request = request.content(content);
93             }
94             ContentResponse contentResponse = request.method(method).send();
95             return contentResponse.getContentAsString();
96         } catch (InterruptedException | TimeoutException | ExecutionException e) {
97             return null;
98         }
99     }
100
101     private <T extends VocAnswer> T callUrl(HttpMethod method, String endpoint, Class<T> objectClass,
102             @Nullable String body) throws VolvoOnCallException {
103         try {
104             String url = endpoint.startsWith("http") ? endpoint : SERVICE_URL + endpoint;
105             String jsonResponse = method == HttpMethod.GET
106                     ? cache.putIfAbsentAndGet(endpoint, () -> getResponse(method, url, body))
107                     : getResponse(method, url, body);
108             if (jsonResponse == null) {
109                 throw new IOException();
110             } else {
111                 logger.debug("Request to `{}` answered : {}", url, jsonResponse);
112                 T responseDTO = Objects.requireNonNull(gson.fromJson(jsonResponse, objectClass));
113                 String error = responseDTO.getErrorLabel();
114                 if (error != null) {
115                     throw new VolvoOnCallException(error, responseDTO.getErrorDescription());
116                 }
117                 return responseDTO;
118             }
119         } catch (JsonSyntaxException | IOException e) {
120             throw new VolvoOnCallException(e);
121         }
122     }
123
124     public <T extends VocAnswer> T getURL(String endpoint, Class<T> objectClass) throws VolvoOnCallException {
125         return callUrl(HttpMethod.GET, endpoint, objectClass, null);
126     }
127
128     public @Nullable PostResponse postURL(String endpoint, @Nullable String body) throws VolvoOnCallException {
129         try {
130             return callUrl(HttpMethod.POST, endpoint, PostResponse.class, body);
131         } catch (VolvoOnCallException e) {
132             if (e.getType() == ErrorType.SERVICE_UNABLE_TO_START) {
133                 logger.info("Unable to start service request sent to VoC");
134                 return null;
135             } else {
136                 throw e;
137             }
138         }
139     }
140
141     public <T extends VocAnswer> T getURL(Class<T> objectClass, String vin) throws VolvoOnCallException {
142         String url = String.format("vehicles/%s/%s", vin, objectClass.getSimpleName().toLowerCase());
143         return getURL(url, objectClass);
144     }
145 }