Skip to content

Commit

Permalink
TraktV2: add helper methods for some common status codes
Browse files Browse the repository at this point in the history
  • Loading branch information
UweTrottmann committed Sep 6, 2024
1 parent a91490d commit 73f3317
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Change Log
==========
## next

* `TraktV2`: add `isUnauthorized(response)`, `isAccountLocked(response)` and `isNotVip(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
24 changes: 24 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,30 @@ 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 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

0 comments on commit 73f3317

Please sign in to comment.