Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper methods for common reponse codes, pagination #153

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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_
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/com/uwetrottmann/trakt5/TraktV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,58 @@ public Response<AccessToken> 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
* <a href="https://trakt.docs.apiary.io/#introduction/locked-user-account">Trakt account is locked</a>.
*/
public static boolean isAccountLocked(Response<?> response) {
return response.code() == 423;
}

/**
* Checks if the response code is 426, which indicates the
* <a href="https://trakt.docs.apiary.io/#introduction/vip-methods">Trakt account is not a VIP</a>.
*/
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
2 changes: 1 addition & 1 deletion src/test/java/com/uwetrottmann/trakt5/BaseTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public <T> void assertSuccessfulResponse(Response<T> response) {
}

private <T> void handleFailedResponse(Response<T> response) {
if (response.code() == 401) {
if (TraktV2.isUnauthorized(response)) {
fail("Authorization required, supply a valid OAuth access token: "
+ response.code() + " " + response.message());
} else {
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.CheckinError;
import com.uwetrottmann.trakt5.entities.EpisodeCheckin;
import com.uwetrottmann.trakt5.entities.EpisodeCheckinResponse;
Expand Down Expand Up @@ -52,7 +53,7 @@ public <T> T executeCheckInCall(@Nonnull Call<T> 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 {
Expand Down Expand Up @@ -156,7 +157,7 @@ public void test_checkin_blocked() throws IOException, InterruptedException {

MovieCheckin movieCheckin = buildMovieCheckin();
Response<MovieCheckinResponse> 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());
}
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