2 * Copyright (c) 2010-2023 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.Objects;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.TimeoutException;
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;
42 import com.google.gson.Gson;
43 import com.google.gson.JsonSyntaxException;
46 * {@link VocHttpApi} wraps the VolvoOnCall REST API.
48 * @author Gaƫl L'hopital - Initial contribution
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";
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;
64 public VocHttpApi(ApiBridgeConfiguration configuration, Gson gson, HttpClient httpClient)
65 throws VolvoOnCallException {
67 this.cache = new ExpiringCacheMap<>(120 * 1000);
68 this.configuration = configuration;
69 this.httpClient = httpClient;
71 httpClient.setUserAgentField(new HttpField(HttpHeader.USER_AGENT, "openhab/voc_binding/" + InstanceUUID.get()));
74 } catch (Exception e) {
75 throw new VolvoOnCallException(new IOException("Unable to start Jetty HttpClient", e));
79 public void dispose() throws Exception {
83 private @Nullable String getResponse(HttpMethod method, String url, @Nullable String body) {
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);
91 ContentProvider content = new StringContentProvider(JSON_CONTENT_TYPE, body, StandardCharsets.UTF_8);
92 request = request.content(content);
94 ContentResponse contentResponse = request.method(method).send();
95 return contentResponse.getContentAsString();
96 } catch (InterruptedException | TimeoutException | ExecutionException e) {
101 private <T extends VocAnswer> T callUrl(HttpMethod method, String endpoint, Class<T> objectClass,
102 @Nullable String body) throws VolvoOnCallException {
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();
111 logger.debug("Request to `{}` answered : {}", url, jsonResponse);
112 T responseDTO = Objects.requireNonNull(gson.fromJson(jsonResponse, objectClass));
113 String error = responseDTO.getErrorLabel();
115 throw new VolvoOnCallException(error, responseDTO.getErrorDescription());
119 } catch (JsonSyntaxException | IOException e) {
120 throw new VolvoOnCallException(e);
124 public <T extends VocAnswer> T getURL(String endpoint, Class<T> objectClass) throws VolvoOnCallException {
125 return callUrl(HttpMethod.GET, endpoint, objectClass, null);
128 public @Nullable PostResponse postURL(String endpoint, @Nullable String body) throws VolvoOnCallException {
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");
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);