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