From 19659b6e0573c31d2f95ac6a2ede187999cf4ee2 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Mon, 22 Aug 2022 12:08:14 +0200 Subject: [PATCH] Adapt to AccessTokenResponse API change (#13278) Signed-off-by: Jacob Laursen --- .../internal/bridge/AutomowerBridge.java | 4 ++-- .../binding/ecobee/internal/api/EcobeeApi.java | 4 ++-- .../homeconnect/internal/client/HttpHelper.java | 16 ++++++++++------ 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/bridge/AutomowerBridge.java b/bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/bridge/AutomowerBridge.java index 1755a5ce0..ff9c3bb9a 100644 --- a/bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/bridge/AutomowerBridge.java +++ b/bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/bridge/AutomowerBridge.java @@ -13,7 +13,7 @@ package org.openhab.binding.automower.internal.bridge; import java.io.IOException; -import java.time.LocalDateTime; +import java.time.Instant; import java.util.concurrent.ScheduledExecutorService; import org.eclipse.jdt.annotation.NonNullByDefault; @@ -55,7 +55,7 @@ public class AutomowerBridge { private AccessTokenResponse authenticate() throws AutomowerCommunicationException { try { AccessTokenResponse result = authService.getAccessTokenResponse(); - if (result == null || result.isExpired(LocalDateTime.now(), 120)) { + if (result == null || result.isExpired(Instant.now(), 120)) { result = authService.getAccessTokenByClientCredentials(null); } return result; diff --git a/bundles/org.openhab.binding.ecobee/src/main/java/org/openhab/binding/ecobee/internal/api/EcobeeApi.java b/bundles/org.openhab.binding.ecobee/src/main/java/org/openhab/binding/ecobee/internal/api/EcobeeApi.java index 0abed2f5b..0f8802e43 100644 --- a/bundles/org.openhab.binding.ecobee/src/main/java/org/openhab/binding/ecobee/internal/api/EcobeeApi.java +++ b/bundles/org.openhab.binding.ecobee/src/main/java/org/openhab/binding/ecobee/internal/api/EcobeeApi.java @@ -19,7 +19,7 @@ import java.io.EOFException; import java.io.IOException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; -import java.time.LocalDateTime; +import java.time.Instant; import java.util.List; import java.util.Properties; import java.util.Set; @@ -151,7 +151,7 @@ public class EcobeeApi implements AccessTokenRefreshListener { AccessTokenResponse localAccessTokenResponse = oAuthClientService.getAccessTokenResponse(); if (localAccessTokenResponse != null) { logger.trace("API: Got AccessTokenResponse from OAuth service: {}", localAccessTokenResponse); - if (localAccessTokenResponse.isExpired(LocalDateTime.now(), TOKEN_EXPIRES_IN_BUFFER_SECONDS)) { + if (localAccessTokenResponse.isExpired(Instant.now(), TOKEN_EXPIRES_IN_BUFFER_SECONDS)) { logger.debug("API: Token is expiring soon. Refresh it now"); localAccessTokenResponse = oAuthClientService.refreshToken(); } diff --git a/bundles/org.openhab.binding.homeconnect/src/main/java/org/openhab/binding/homeconnect/internal/client/HttpHelper.java b/bundles/org.openhab.binding.homeconnect/src/main/java/org/openhab/binding/homeconnect/internal/client/HttpHelper.java index aeb910445..addb242c3 100644 --- a/bundles/org.openhab.binding.homeconnect/src/main/java/org/openhab/binding/homeconnect/internal/client/HttpHelper.java +++ b/bundles/org.openhab.binding.homeconnect/src/main/java/org/openhab/binding/homeconnect/internal/client/HttpHelper.java @@ -17,7 +17,9 @@ import static io.github.bucket4j.Refill.intervally; import java.io.IOException; import java.time.Duration; +import java.time.Instant; import java.time.LocalDateTime; +import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.HashMap; import java.util.Map; @@ -96,8 +98,7 @@ public class HttpHelper { try { AccessTokenResponse accessTokenResponse = oAuthClientService.getAccessTokenResponse(); // refresh the token if it's about to expire - if (accessTokenResponse != null - && accessTokenResponse.isExpired(LocalDateTime.now(), OAUTH_EXPIRE_BUFFER)) { + if (accessTokenResponse != null && accessTokenResponse.isExpired(Instant.now(), OAUTH_EXPIRE_BUFFER)) { LoggerFactory.getLogger(HttpHelper.class).debug("Requesting a refresh of the access token."); accessTokenResponse = oAuthClientService.refreshToken(); } @@ -106,11 +107,14 @@ public class HttpHelper { String lastToken = lastAccessToken; if (lastToken == null) { LoggerFactory.getLogger(HttpHelper.class).debug("The used access token was created at {}", - accessTokenResponse.getCreatedOn().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); + LocalDateTime.ofInstant(accessTokenResponse.getCreatedOn(), ZoneId.systemDefault()) + .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); } else if (!lastToken.equals(accessTokenResponse.getAccessToken())) { - LoggerFactory.getLogger(HttpHelper.class).debug( - "The access token changed. New one created at {}", - accessTokenResponse.getCreatedOn().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); + LoggerFactory.getLogger(HttpHelper.class) + .debug("The access token changed. New one created at {}", + LocalDateTime + .ofInstant(accessTokenResponse.getCreatedOn(), ZoneId.systemDefault()) + .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); } lastAccessToken = accessTokenResponse.getAccessToken();