diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ae1f859..55f2f244 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ Change Log ========== + +## next + +* `TraktV2`: add `isUnauthorized(response)`, `isAccountLocked(response)` and `isNotVip(response)` helper methods. +* `TraktV2`: add `getPageCount(response)` and `getItemCount(response)` helper methods. ## 6.15.0 _2024-08-30_ diff --git a/README.md b/README.md index 2e93eb4c..62d2a284 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ try { System.out.println("Title: " + trending.show.title); } } else { - if (response.code() == 401) { + if (TraktV2.isUnauthorized(response)) { // authorization required, supply a valid OAuth access token } else { // the request failed for some other reason diff --git a/src/main/java/com/uwetrottmann/trakt5/TraktV2.java b/src/main/java/com/uwetrottmann/trakt5/TraktV2.java index 7f4bfa75..1faf3786 100644 --- a/src/main/java/com/uwetrottmann/trakt5/TraktV2.java +++ b/src/main/java/com/uwetrottmann/trakt5/TraktV2.java @@ -352,6 +352,58 @@ public Response refreshAccessToken(String refreshToken) throws IOEx ).execute(); } + /** + * Checks if the response code is 401, which indicates an {@link #accessToken(String)} needs to be supplied, + * or it is no longer valid. + */ + public static boolean isUnauthorized(Response response) { + return response.code() == 401; + } + + /** + * Checks if the response code is 423, which indicates the + * Trakt account is locked. + */ + public static boolean isAccountLocked(Response response) { + return response.code() == 423; + } + + /** + * Checks if the response code is 426, which indicates the + * Trakt account is not a VIP. + */ + public static boolean isNotVip(Response response) { + return response.code() == 426; + } + + /** + * If it exists, returns the value of the {@code X-Pagination-Page-Count} header from the response. + */ + @Nullable + public static Integer getPageCount(Response response) { + return getIntHeaderOrNull(response, "x-pagination-page-count"); + } + + /** + * If it exists, returns the value of the {@code X-Pagination-Item-Count} header from the response. + */ + @Nullable + public static Integer getItemCount(Response response) { + return getIntHeaderOrNull(response, "x-pagination-item-count"); + } + + @Nullable + private static Integer getIntHeaderOrNull(Response response, String header) { + String headerOrNull = response.headers().get(header); + if (headerOrNull != null) { + try { + return Integer.valueOf(headerOrNull); + } catch (NumberFormatException ignored) { + } + } + return null; + } + /** * If the response code is 409 tries to convert the body into a {@link CheckinError}. */ diff --git a/src/test/java/com/uwetrottmann/trakt5/BaseTestCase.java b/src/test/java/com/uwetrottmann/trakt5/BaseTestCase.java index 8d090d8e..20fc5133 100644 --- a/src/test/java/com/uwetrottmann/trakt5/BaseTestCase.java +++ b/src/test/java/com/uwetrottmann/trakt5/BaseTestCase.java @@ -141,7 +141,7 @@ public void assertSuccessfulResponse(Response response) { } private void handleFailedResponse(Response response) { - if (response.code() == 401) { + if (TraktV2.isUnauthorized(response)) { fail("Authorization required, supply a valid OAuth access token: " + response.code() + " " + response.message()); } else { diff --git a/src/test/java/com/uwetrottmann/trakt5/services/CheckinTest.java b/src/test/java/com/uwetrottmann/trakt5/services/CheckinTest.java index dfe90da2..4a260ab2 100644 --- a/src/test/java/com/uwetrottmann/trakt5/services/CheckinTest.java +++ b/src/test/java/com/uwetrottmann/trakt5/services/CheckinTest.java @@ -18,6 +18,7 @@ import com.uwetrottmann.trakt5.BaseTestCase; import com.uwetrottmann.trakt5.TestData; +import com.uwetrottmann.trakt5.TraktV2; import com.uwetrottmann.trakt5.entities.CheckinError; import com.uwetrottmann.trakt5.entities.EpisodeCheckin; import com.uwetrottmann.trakt5.entities.EpisodeCheckinResponse; @@ -52,7 +53,7 @@ public T executeCheckInCall(@Nonnull Call call) throws IOException, Inter if (!response.isSuccessful()) { if (getTrakt().checkForCheckinError(response) != null) { fail("Check-in still in progress, may be left over from failed test"); - } else if (response.code() == 401) { + } else if (TraktV2.isUnauthorized(response)) { fail("Authorization required, supply a valid OAuth access token: " + response.code() + " " + response.message()); } else { @@ -156,7 +157,7 @@ public void test_checkin_blocked() throws IOException, InterruptedException { MovieCheckin movieCheckin = buildMovieCheckin(); Response responseBlocked = checkin.checkin(movieCheckin).execute(); - if (responseBlocked.code() == 401) { + if (TraktV2.isUnauthorized(responseBlocked)) { fail("Authorization required, supply a valid OAuth access token: " + responseBlocked.code() + " " + responseBlocked.message()); } diff --git a/src/test/java/com/uwetrottmann/trakt5/services/UsersTest.java b/src/test/java/com/uwetrottmann/trakt5/services/UsersTest.java index 1e590c03..8e5c128a 100644 --- a/src/test/java/com/uwetrottmann/trakt5/services/UsersTest.java +++ b/src/test/java/com/uwetrottmann/trakt5/services/UsersTest.java @@ -18,6 +18,7 @@ import com.uwetrottmann.trakt5.BaseTestCase; import com.uwetrottmann.trakt5.TestData; +import com.uwetrottmann.trakt5.TraktV2; import com.uwetrottmann.trakt5.entities.BaseMovie; import com.uwetrottmann.trakt5.entities.BaseShow; import com.uwetrottmann.trakt5.entities.Followed; @@ -114,8 +115,13 @@ public void test_collectionShows() throws IOException { @Test public void test_notes() throws IOException { - List allNotes = executeCall( + Response> response = executeCallWithoutReadingBody( getTrakt().users().notes(UserSlug.ME, "all", null, null, Extended.FULL)); + + assertThat(TraktV2.getPageCount(response)).isNotNull(); + assertThat(TraktV2.getItemCount(response)).isNotNull(); + + List allNotes = response.body(); assertThat(allNotes).isNotEmpty(); for (NoteResponse noteResponse : allNotes) { assertThat(noteResponse.attached_to).isNotNull();