2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.volvooncall.internal.api;
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;
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;
41 import com.google.gson.Gson;
42 import com.google.gson.JsonSyntaxException;
45 * {@link VocHttpApi} wraps the VolvoOnCall REST API.
47 * @author Gaƫl L'hopital - Initial contribution
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";
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;
63 public VocHttpApi(ApiBridgeConfiguration configuration, Gson gson, HttpClient httpClient)
64 throws VolvoOnCallException {
66 this.cache = new ExpiringCacheMap<>(120 * 1000);
67 this.configuration = configuration;
68 this.httpClient = httpClient;
70 httpClient.setUserAgentField(new HttpField(HttpHeader.USER_AGENT, "openhab/voc_binding/" + InstanceUUID.get()));
73 } catch (Exception e) {
74 throw new VolvoOnCallException(new IOException("Unable to start Jetty HttpClient", e));
78 public void dispose() throws Exception {
82 private @Nullable String getResponse(HttpMethod method, String url, @Nullable String body) {
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);
90 ContentProvider content = new StringContentProvider(JSON_CONTENT_TYPE, body, StandardCharsets.UTF_8);
91 request = request.content(content);
93 ContentResponse contentResponse = request.method(method).send();
94 return contentResponse.getContentAsString();
95 } catch (InterruptedException | TimeoutException | ExecutionException e) {
100 private <T extends VocAnswer> T callUrl(HttpMethod method, String endpoint, Class<T> objectClass,
101 @Nullable String body) throws VolvoOnCallException {
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();
110 logger.debug("Request to `{}` answered : {}", url, jsonResponse);
111 T responseDTO = gson.fromJson(jsonResponse, objectClass);
112 String error = responseDTO.getErrorLabel();
114 throw new VolvoOnCallException(error, responseDTO.getErrorDescription());
118 } catch (JsonSyntaxException | IOException e) {
119 throw new VolvoOnCallException(e);
123 public <T extends VocAnswer> T getURL(String endpoint, Class<T> objectClass) throws VolvoOnCallException {
124 return callUrl(HttpMethod.GET, endpoint, objectClass, null);
127 public @Nullable PostResponse postURL(String endpoint, @Nullable String body) throws VolvoOnCallException {
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");
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);