use of com.uwetrottmann.trakt5.TraktV2 in project SeriesGuide by UweTrottmann.
the class TraktCredentials method refreshAccessToken.
/**
* Tries to refresh the current access token. Returns {@code false} on failure.
*/
public synchronized boolean refreshAccessToken(TraktV2 trakt) {
// do we even have a refresh token?
String oldRefreshToken = TraktOAuthSettings.getRefreshToken(mContext);
if (TextUtils.isEmpty(oldRefreshToken)) {
Timber.d("refreshAccessToken: no refresh token, give up.");
return false;
}
// try to get a new access token from trakt
String accessToken = null;
String refreshToken = null;
long expiresIn = -1;
try {
Response<AccessToken> response = trakt.refreshAccessToken();
if (response.isSuccessful()) {
AccessToken token = response.body();
accessToken = token.access_token;
refreshToken = token.refresh_token;
expiresIn = token.expires_in;
} else {
if (!SgTrakt.isUnauthorized(response)) {
SgTrakt.trackFailedRequest(mContext, "refresh access token", response);
}
}
} catch (IOException e) {
SgTrakt.trackFailedRequest(mContext, "refresh access token", e);
}
// did we obtain all required data?
if (TextUtils.isEmpty(accessToken) || TextUtils.isEmpty(refreshToken) || expiresIn < 1) {
Timber.e("refreshAccessToken: failed.");
return false;
}
// store the new access token, refresh token and expiry date
if (!setAccessToken(accessToken) || !TraktOAuthSettings.storeRefreshData(mContext, refreshToken, expiresIn)) {
Timber.e("refreshAccessToken: saving failed");
return false;
}
Timber.d("refreshAccessToken: success.");
return true;
}
use of com.uwetrottmann.trakt5.TraktV2 in project SeriesGuide by UweTrottmann.
the class TraktTask method doCommentAction.
private TraktResponse doCommentAction() {
Comment commentOrNull = buildComment();
if (commentOrNull == null)
return buildErrorResponse();
TraktV2 trakt = SgApp.getServicesComponent(context).trakt();
try {
// post comment
retrofit2.Response<Comment> response = trakt.comments().post(commentOrNull).execute();
if (response.isSuccessful()) {
Comment postedComment = response.body();
if (postedComment.id != null) {
return new TraktResponse(true, null);
}
} else {
// check if comment failed validation or item does not exist on trakt
if (response.code() == 422) {
return new TraktResponse(false, context.getString(R.string.shout_invalid));
} else if (response.code() == 404) {
return new TraktResponse(false, context.getString(R.string.shout_invalid));
} else if (SgTrakt.isUnauthorized(response)) {
// for users banned from posting comments requests also return 401
// so do not sign out if an error header does not indicate the token is invalid
String authHeader = response.headers().get("Www-Authenticate");
if (authHeader != null && !authHeader.contains("invalid_token")) {
Pattern pattern = Pattern.compile("error_description=\"(.*)\"");
Matcher matcher = pattern.matcher(authHeader);
String message;
if (matcher.find()) {
message = matcher.group(1);
} else {
message = context.getString(R.string.trakt_error_credentials);
}
return new TraktResponse(false, message);
} else {
TraktCredentials.get(context).setCredentialsInvalid();
return new TraktResponse(false, context.getString(R.string.trakt_error_credentials));
}
} else {
Errors.logAndReport("post comment", response);
}
}
} catch (Exception e) {
Errors.logAndReport("post comment", e);
}
// return generic failure message
return buildErrorResponse();
}
use of com.uwetrottmann.trakt5.TraktV2 in project SeriesGuide by UweTrottmann.
the class TraktTask method doCheckInAction.
private TraktResponse doCheckInAction() {
TraktV2 trakt = SgApp.getServicesComponent(context).trakt();
try {
retrofit2.Response response;
String message = args.getString(InitBundle.MESSAGE);
switch(action) {
case CHECKIN_EPISODE:
{
// Check in using show Trakt ID
// and season and episode number (likely most reliable).
long episodeId = args.getLong(InitBundle.EPISODE_ID);
SgRoomDatabase database = SgRoomDatabase.getInstance(context);
SgEpisode2Numbers episode = database.sgEpisode2Helper().getEpisodeNumbers(episodeId);
if (episode == null) {
Timber.e("Failed to get episode %d", episodeId);
return buildErrorResponse();
}
Integer showTraktId = ShowTools.getShowTraktId(context, episode.getShowId());
if (showTraktId == null) {
Timber.e("Failed to get show %d", episode.getShowId());
return buildErrorResponse();
}
SyncEpisode traktEpisode = new SyncEpisode().season(episode.getSeason()).number(episode.getEpisodenumber());
Show traktShow = new Show();
traktShow.ids = ShowIds.trakt(showTraktId);
EpisodeCheckin checkin = new EpisodeCheckin.Builder(traktEpisode, APP_VERSION, null).show(traktShow).message(message).build();
response = trakt.checkin().checkin(checkin).execute();
break;
}
case CHECKIN_MOVIE:
{
int movieTmdbId = args.getInt(InitBundle.MOVIE_TMDB_ID);
MovieCheckin checkin = new MovieCheckin.Builder(new SyncMovie().id(MovieIds.tmdb(movieTmdbId)), APP_VERSION, null).message(message).build();
response = trakt.checkin().checkin(checkin).execute();
break;
}
default:
throw new IllegalArgumentException("check-in action unknown.");
}
if (response.isSuccessful()) {
return new TraktResponse(true, context.getString(R.string.checkin_success_trakt, args.getString(InitBundle.TITLE)));
} else {
// check if the user wants to check-in, but there is already a check-in in progress
CheckinError checkinError = trakt.checkForCheckinError(response);
if (checkinError != null) {
OffsetDateTime expiresAt = checkinError.expires_at;
int waitTimeMin = expiresAt == null ? -1 : (int) ((expiresAt.toInstant().toEpochMilli() - System.currentTimeMillis()) / 1000);
return new CheckinBlockedResponse(waitTimeMin);
} else // check if item does not exist on trakt (yet)
if (response.code() == 404) {
return new TraktResponse(false, context.getString(R.string.trakt_error_not_exists));
} else if (SgTrakt.isUnauthorized(context, response)) {
return new TraktResponse(false, context.getString(R.string.trakt_error_credentials));
} else {
Errors.logAndReport("check-in", response);
}
}
} catch (Exception e) {
Errors.logAndReport("check-in", e);
}
// return generic failure message
return buildErrorResponse();
}
Aggregations