From 23f2d4e609fb5b14df84cc9aa927d2de2356ab81 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 17 Apr 2016 22:21:14 +0000 Subject: [PATCH] Expire auth token after 1 hour --- source/net/filebot/web/TheTVDBClient2.java | 24 ++++++++++++-------- test/net/filebot/web/TheTVDBClient2Test.java | 8 ++----- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/source/net/filebot/web/TheTVDBClient2.java b/source/net/filebot/web/TheTVDBClient2.java index b29c3a36..8202569d 100644 --- a/source/net/filebot/web/TheTVDBClient2.java +++ b/source/net/filebot/web/TheTVDBClient2.java @@ -15,6 +15,7 @@ import java.net.URI; import java.net.URL; import java.nio.ByteBuffer; import java.time.Duration; +import java.time.Instant; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; @@ -79,18 +80,23 @@ public class TheTVDBClient2 extends AbstractEpisodeListProvider implements Artwo } private String token = null; + private Instant tokenExpireInstant = null; + private Duration tokenExpireDuration = Duration.ofHours(1); - private synchronized String getAuthorizationToken() { - // curl -v -X GET --header 'Accept: application/json' --header 'Authorization: Bearer TOKEN' 'https://api.thetvdb.com/languages' - if (token == null) { - try { - Object json = requestJson("login", singletonMap("apikey", apikey)); - token = getString(json, "token"); - } catch (Exception e) { - throw new IllegalStateException("Failed to retrieve authorization token: " + e.getMessage(), e); + private String getAuthorizationToken() { + synchronized (tokenExpireDuration) { + System.out.println("EXPIRE: " + tokenExpireInstant); + if (token == null || (tokenExpireInstant != null && Instant.now().isAfter(tokenExpireInstant))) { + try { + Object json = requestJson("login", singletonMap("apikey", apikey)); + token = getString(json, "token"); + tokenExpireInstant = Instant.now().plus(tokenExpireDuration); + } catch (Exception e) { + throw new IllegalStateException("Failed to retrieve authorization token: " + e.getMessage(), e); + } } + return token; } - return token; } protected String[] languages() throws Exception { diff --git a/test/net/filebot/web/TheTVDBClient2Test.java b/test/net/filebot/web/TheTVDBClient2Test.java index 42fe70de..17167c06 100644 --- a/test/net/filebot/web/TheTVDBClient2Test.java +++ b/test/net/filebot/web/TheTVDBClient2Test.java @@ -10,7 +10,7 @@ import org.junit.Test; public class TheTVDBClient2Test { - TheTVDBClient2 thetvdb = new TheTVDBClient2("BA864DEE427E384A"); + static TheTVDBClient2 thetvdb = new TheTVDBClient2("BA864DEE427E384A"); SearchResult buffy = new SearchResult(70327, "Buffy the Vampire Slayer"); SearchResult wonderfalls = new SearchResult(78845, "Wonderfalls"); @@ -123,15 +123,11 @@ public class TheTVDBClient2Test { @Test public void getSeriesInfo() throws Exception { - TheTVDBSeriesInfo it = (TheTVDBSeriesInfo) thetvdb.getSeriesInfo(80348, Locale.ENGLISH); + SeriesInfo it = thetvdb.getSeriesInfo(80348, Locale.ENGLISH); assertEquals(80348, it.getId(), 0); - assertEquals("TV-PG", it.getContentRating()); - assertEquals("2007-09-24", it.getFirstAired().toString()); assertEquals("Action", it.getGenres().get(0)); - assertEquals("tt0934814", it.getImdbId()); assertEquals("en", it.getLanguage()); - assertEquals(987, it.getOverview().length()); assertEquals("45", it.getRuntime().toString()); assertEquals("Chuck", it.getName()); }