From 28b09e878be2c16919982a98de0409f450e6474d Mon Sep 17 00:00:00 2001 From: Uwe Trottmann Date: Fri, 6 Sep 2024 09:09:03 +0200 Subject: [PATCH] TraktV2: add helper methods to get page and item count --- CHANGELOG.md | 1 + .../java/com/uwetrottmann/trakt5/TraktV2.java | 28 +++++++++++++++++++ .../trakt5/services/UsersTest.java | 8 +++++- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 352b06eb..55f2f244 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ 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/src/main/java/com/uwetrottmann/trakt5/TraktV2.java b/src/main/java/com/uwetrottmann/trakt5/TraktV2.java index 96d051a5..1faf3786 100644 --- a/src/main/java/com/uwetrottmann/trakt5/TraktV2.java +++ b/src/main/java/com/uwetrottmann/trakt5/TraktV2.java @@ -376,6 +376,34 @@ 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/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();