-
Notifications
You must be signed in to change notification settings - Fork 44
/
TraktV2.java
554 lines (493 loc) · 20.1 KB
/
TraktV2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*
* Copyright 2014-2024 Uwe Trottmann
* Copyright 2019 Marc de Courville
* Copyright 2020 Sam Malone
* Copyright 2020 srggsf
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.uwetrottmann.trakt5;
import com.uwetrottmann.trakt5.entities.AccessToken;
import com.uwetrottmann.trakt5.entities.AccessTokenRefreshRequest;
import com.uwetrottmann.trakt5.entities.AccessTokenRequest;
import com.uwetrottmann.trakt5.entities.CheckinError;
import com.uwetrottmann.trakt5.entities.ClientId;
import com.uwetrottmann.trakt5.entities.DeviceCode;
import com.uwetrottmann.trakt5.entities.DeviceCodeAccessTokenRequest;
import com.uwetrottmann.trakt5.entities.TraktError;
import com.uwetrottmann.trakt5.entities.TraktOAuthError;
import com.uwetrottmann.trakt5.services.Authentication;
import com.uwetrottmann.trakt5.services.Calendars;
import com.uwetrottmann.trakt5.services.Checkin;
import com.uwetrottmann.trakt5.services.Comments;
import com.uwetrottmann.trakt5.services.Episodes;
import com.uwetrottmann.trakt5.services.Genres;
import com.uwetrottmann.trakt5.services.Movies;
import com.uwetrottmann.trakt5.services.Notes;
import com.uwetrottmann.trakt5.services.People;
import com.uwetrottmann.trakt5.services.Recommendations;
import com.uwetrottmann.trakt5.services.Scrobble;
import com.uwetrottmann.trakt5.services.Search;
import com.uwetrottmann.trakt5.services.Seasons;
import com.uwetrottmann.trakt5.services.Shows;
import com.uwetrottmann.trakt5.services.Sync;
import com.uwetrottmann.trakt5.services.Users;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import javax.annotation.Nullable;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.annotation.Annotation;
import java.net.URLEncoder;
/**
* Helper class for easy usage of the Trakt v2 API using retrofit.
*/
public class TraktV2 {
/**
* Trakt API v2 URL.
*/
public static final String API_HOST = "api.trakt.tv";
public static final String API_STAGING_HOST = "api-staging.trakt.tv";
public static final String API_URL = "https://" + API_HOST + "/";
public static final String API_STAGING_URL = "https://" + API_STAGING_HOST + "/";
public static final String API_VERSION = "2";
public static final String SITE_URL = "https://trakt.tv";
public static final String OAUTH2_AUTHORIZATION_URL = SITE_URL + "/oauth/authorize";
public static final String HEADER_AUTHORIZATION = "Authorization";
public static final String HEADER_CONTENT_TYPE = "Content-Type";
public static final String CONTENT_TYPE_JSON = "application/json";
public static final String HEADER_TRAKT_API_VERSION = "trakt-api-version";
public static final String HEADER_TRAKT_API_KEY = "trakt-api-key";
@Nullable private OkHttpClient okHttpClient;
@Nullable private Retrofit retrofit;
private String apiKey;
@Nullable private String clientSecret;
@Nullable private String redirectUri;
@Nullable private String accessToken;
@Nullable private String refreshToken;
private final HttpUrl apiBaseUrl;
/**
* Get a new API manager instance.
*
* @param apiKey The API key obtained from Trakt, currently equal to the OAuth client id.
* @see #TraktV2(java.lang.String, boolean)
*/
public TraktV2(String apiKey) {
this(apiKey, false);
}
/**
* Get a new API manager instance capable of calling OAuth2 protected endpoints.
*
* @param apiKey The API key obtained from Trakt, currently equal to the OAuth client id.
* @param clientSecret The client secret obtained from Trakt.
* @param redirectUri The redirect URI to use for OAuth2 token requests.
* @see #TraktV2(java.lang.String, java.lang.String, java.lang.String, boolean)
*/
public TraktV2(String apiKey, String clientSecret, String redirectUri) {
this(apiKey, clientSecret, redirectUri, false);
}
/**
* Get a new API manager instance.
*
* @param apiKey The API key obtained from Trakt, currently equal to the OAuth client id.
* @param staging Use {@link TraktV2#API_STAGING_URL} if {true} or {@link TraktV2#API_URL} otherwise.
* @see #TraktV2(java.lang.String)
*/
public TraktV2(String apiKey, boolean staging){
this.apiKey = apiKey;
apiBaseUrl = HttpUrl.get(staging ? API_STAGING_URL : API_URL);
}
/**
* Get a new API manager instance capable of calling OAuth2 protected endpoints.
*
* @param apiKey The API key obtained from Trakt, currently equal to the OAuth client id.
* @param clientSecret The client secret obtained from Trakt.
* @param redirectUri The redirect URI to use for OAuth2 token requests.
* @param staging Use {@link TraktV2#API_STAGING_URL} if {true} or {@link TraktV2#API_URL} otherwise.
* @see #TraktV2(java.lang.String, java.lang.String, java.lang.String)
*/
public TraktV2(String apiKey, String clientSecret, String redirectUri, boolean staging) {
this(apiKey, staging);
this.clientSecret = clientSecret;
this.redirectUri = redirectUri;
}
public String apiHost(){
return apiBaseUrl.host();
}
public String apiKey() {
return apiKey;
}
public TraktV2 apiKey(String apiKey) {
this.apiKey = apiKey;
return this;
}
@Nullable
public String accessToken() {
return accessToken;
}
/**
* Sets the OAuth 2.0 access token to be appended to requests.
*
* <p> If set, some methods will return user-specific data.
*
* @param accessToken A valid access token, obtained via e.g. {@link #exchangeCodeForAccessToken(String)}.
*/
public TraktV2 accessToken(@Nullable String accessToken) {
this.accessToken = accessToken;
return this;
}
@Nullable
public String refreshToken() {
return refreshToken;
}
/**
* Sets the OAuth 2.0 refresh token to be used, in case the current access token has expired, to get a new access
* token.
*/
public TraktV2 refreshToken(@Nullable String refreshToken) {
this.refreshToken = refreshToken;
return this;
}
/**
* Creates a {@link Retrofit.Builder} that sets the base URL, adds a Gson converter and sets {@link #okHttpClient()}
* as its client.
*
* @see #okHttpClient()
*/
protected Retrofit.Builder retrofitBuilder() {
return new Retrofit.Builder()
.baseUrl(apiBaseUrl)
.addConverterFactory(GsonConverterFactory.create(TraktV2Helper.getGsonBuilder().create()))
.client(okHttpClient());
}
/**
* Returns the default OkHttp client instance. It is strongly recommended to override this and use your app
* instance.
*
* @see #setOkHttpClientDefaults(OkHttpClient.Builder)
*/
protected synchronized OkHttpClient okHttpClient() {
if (okHttpClient == null) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
setOkHttpClientDefaults(builder);
okHttpClient = builder.build();
}
return okHttpClient;
}
/**
* Adds {@link TraktV2Interceptor} as an application interceptor and {@link TraktV2Authenticator} as an authenticator.
*/
protected void setOkHttpClientDefaults(OkHttpClient.Builder builder) {
builder.addInterceptor(new TraktV2Interceptor(this));
builder.authenticator(new TraktV2Authenticator(this));
}
/**
* Return the {@link Retrofit} instance. If called for the first time builds the instance.
*/
protected Retrofit retrofit() {
if (retrofit == null) {
retrofit = retrofitBuilder().build();
}
return retrofit;
}
/**
* Build an OAuth 2.0 authorization URL to obtain an authorization code.
*
* <p>Send the user to the URL. Once the user authorized your app, the server will redirect to {@code redirectUri}
* with the authorization code and the sent state in the query parameter {@code code}.
*
* <p>Ensure the state matches, then supply the authorization code to {@link #exchangeCodeForAccessToken} to get an
* access token.
*
* @param state State variable to prevent request forgery attacks.
*/
public String buildAuthorizationUrl(String state) {
if (redirectUri == null) {
throw new IllegalStateException("redirectUri not provided");
}
@SuppressWarnings("StringBufferReplaceableByString")
StringBuilder authUrl = new StringBuilder(OAUTH2_AUTHORIZATION_URL);
authUrl.append("?").append("response_type=code");
authUrl.append("&").append("redirect_uri=").append(urlEncode(redirectUri));
authUrl.append("&").append("state=").append(urlEncode(state));
authUrl.append("&").append("client_id=").append(urlEncode(apiKey()));
return authUrl.toString();
}
private String urlEncode(String content) {
try {
// can not use java.nio.charset.StandardCharsets as on Android only available since API 19
return URLEncoder.encode(content, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new UnsupportedOperationException(e);
}
}
/**
* Request a code to start the device authentication process from Trakt.
*
* The {@code device_code} and {@code interval} will be used later to poll for the {@code access_token}.
* The {@code user_code} and {@code verification_url} should be presented to the user.
*/
public Response<DeviceCode> generateDeviceCode() throws IOException {
ClientId clientId = new ClientId();
clientId.client_id = apiKey;
return authentication().generateDeviceCode(clientId).execute();
}
/**
* Request an access token from Trakt using device authentication.
*
* <p>Supply the received access token to {@link #accessToken(String)} and store the refresh token to later refresh
* the access token once it has expired.
*
* <p>On failure re-authorization of your app is required (see {@link #generateDeviceCode()}).
*
* @param deviceCode A valid device code (see {@link #generateDeviceCode()}).
*/
public Response<AccessToken> exchangeDeviceCodeForAccessToken(String deviceCode) throws IOException {
if (clientSecret == null) {
throw new IllegalStateException("clientSecret not provided");
}
DeviceCodeAccessTokenRequest request = new DeviceCodeAccessTokenRequest();
request.client_id = apiKey;
request.client_secret = clientSecret;
request.code = deviceCode;
return authentication().exchangeDeviceCodeForAccessToken(request).execute();
}
/**
* Request an access token from Trakt.
*
* <p>Supply the received access token to {@link #accessToken(String)} and store the refresh token to later refresh
* the access token once it has expired.
*
* <p>On failure re-authorization of your app is required (see {@link #buildAuthorizationUrl}).
*
* @param authCode A valid authorization code (see {@link #buildAuthorizationUrl(String)}).
*/
public Response<AccessToken> exchangeCodeForAccessToken(String authCode) throws IOException {
if (clientSecret == null) {
throw new IllegalStateException("clientSecret not provided");
}
if (redirectUri == null) {
throw new IllegalStateException("redirectUri not provided");
}
return authentication().exchangeCodeForAccessToken(
new AccessTokenRequest(
authCode,
apiKey(),
clientSecret,
redirectUri)
).execute();
}
/**
* Request to refresh an expired access token for Trakt. If your app is still authorized, returns a response which
* includes a new access token.
*
* <p>Supply the received access token to {@link #accessToken(String)} and store the refresh token to later refresh
* the access token once it has expired.
*
* <p>On failure re-authorization of your app is required (see {@link #buildAuthorizationUrl}).
*/
public Response<AccessToken> refreshAccessToken(String refreshToken) throws IOException {
if (clientSecret == null) {
throw new IllegalStateException("clientSecret not provided");
}
if (redirectUri == null) {
throw new IllegalStateException("redirectUri not provided");
}
return authentication().refreshAccessToken(
new AccessTokenRefreshRequest(
refreshToken,
apiKey(),
clientSecret,
redirectUri
)
).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}.
*/
@Nullable
public CheckinError checkForCheckinError(Response<?> response) {
if (response.code() != 409) {
return null; // only code 409 can be a check-in error
}
Converter<ResponseBody, CheckinError> errorConverter =
retrofit().responseBodyConverter(CheckinError.class, new Annotation[0]);
try {
//noinspection ConstantConditions never null if unsuccessful
return errorConverter.convert(response.errorBody());
} catch (IOException e) {
return new CheckinError(); // null values
}
}
/**
* If the response is not successful, tries to parse the error body into a {@link TraktError}.
*/
@Nullable
public TraktError checkForTraktError(Response<?> response) {
if (response.isSuccessful()) {
return null;
}
Converter<ResponseBody, TraktError> errorConverter =
retrofit().responseBodyConverter(TraktError.class, new Annotation[0]);
try {
//noinspection ConstantConditions never null if unsuccessful
return errorConverter.convert(response.errorBody());
} catch (IOException ignored) {
return new TraktError(); // null values
}
}
/**
* If the {@link Authentication} response is not successful,
* tries to parse the error body into a {@link TraktOAuthError}.
*/
@Nullable
public TraktOAuthError checkForTraktOAuthError(Response<?> response) {
if (response.isSuccessful()) {
return null;
}
Converter<ResponseBody, TraktOAuthError> errorConverter = retrofit()
.responseBodyConverter(TraktOAuthError.class, new Annotation[0]);
if (response.errorBody() != null) {
try {
return errorConverter.convert(response.errorBody());
} catch (IOException ignored) {
}
}
return new TraktOAuthError(); // null values
}
public Authentication authentication() {
return retrofit().create(Authentication.class);
}
/**
* By default, the calendar will return all shows or movies for the specified time period. If OAuth is sent, the
* items returned will be limited to what the user has watched, collected, or added to their watchlist. You'll most
* likely want to send OAuth to make the calendar more relevant to the user.
*/
public Calendars calendars() {
return retrofit().create(Calendars.class);
}
/**
* Checking in is a manual process used by mobile apps. While not as effortless as scrobbling, checkins help fill in
* the gaps. You might be watching live tv, at a friend's house, or watching a movie in theaters. You can simply
* checkin from your phone or tablet in those situations.
*/
public Checkin checkin() {
return retrofit().create(Checkin.class);
}
/**
* Comments are attached to any movie, show, season, episode, or list and can be shorter shouts or more in depth
* reviews. Each comment can have replies and can be voted up or down. These votes are used to determine popular
* comments.
*/
public Comments comments() {
return retrofit().create(Comments.class);
}
/**
* One or more genres are attached to all movies and shows. Some API methods allow filtering by genre, so it's good
* to cache this list in your app.
*/
public Genres genres() {
return retrofit().create(Genres.class);
}
public Movies movies() {
return retrofit().create(Movies.class);
}
public Notes notes() {
return retrofit().create(Notes.class);
}
public People people() {
return retrofit().create(People.class);
}
/**
* Recommendations are based on the watched history for a user and their friends. There are other factors that go
* into the algorithm as well to further personalize what gets recommended.
*/
public Recommendations recommendations() {
return retrofit().create(Recommendations.class);
}
/**
* Searches can use queries or ID lookups. Queries will search fields like the title and description. ID lookups are
* helpful if you have an external ID and want to get the Trakt ID and info. This method will search for movies,
* shows, episodes, people, users, and lists.
*/
public Search search() {
return retrofit().create(Search.class);
}
public Shows shows() {
return retrofit().create(Shows.class);
}
public Seasons seasons() {
return retrofit().create(Seasons.class);
}
public Episodes episodes() {
return retrofit().create(Episodes.class);
}
public Sync sync() {
return retrofit().create(Sync.class);
}
public Scrobble scrobble() { return retrofit().create(Scrobble.class); }
public Users users() {
return retrofit().create(Users.class);
}
}