Adapt to AccessTokenResponse API change (#13278)

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2022-08-22 12:08:14 +02:00 committed by GitHub
parent 29fb142e6a
commit 19659b6e05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 10 deletions

View File

@ -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;

View File

@ -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();
}

View File

@ -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();