Skip to content

Commit

Permalink
TraktV2: add helper methods to get page and item count
Browse files Browse the repository at this point in the history
  • Loading branch information
UweTrottmann committed Sep 6, 2024
1 parent 4e54896 commit 0d8e93f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/uwetrottmann/trakt5/TraktV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -114,8 +115,13 @@ public void test_collectionShows() throws IOException {

@Test
public void test_notes() throws IOException {
List<NoteResponse> allNotes = executeCall(
Response<List<NoteResponse>> response = executeCallWithoutReadingBody(
getTrakt().users().notes(UserSlug.ME, "all", null, null, Extended.FULL));

assertThat(TraktV2.getPageCount(response)).isNotNull();
assertThat(TraktV2.getItemCount(response)).isNotNull();

List<NoteResponse> allNotes = response.body();
assertThat(allNotes).isNotEmpty();
for (NoteResponse noteResponse : allNotes) {
assertThat(noteResponse.attached_to).isNotNull();
Expand Down

0 comments on commit 0d8e93f

Please sign in to comment.