com.uwetrottmann.tmdb2.Tmdb Maven / Gradle / Ivy
Show all versions of tmdb-java Show documentation
package com.uwetrottmann.tmdb2;
import com.uwetrottmann.tmdb2.services.CollectionService;
import com.uwetrottmann.tmdb2.services.ConfigurationService;
import com.uwetrottmann.tmdb2.services.DiscoverService;
import com.uwetrottmann.tmdb2.services.FindService;
import com.uwetrottmann.tmdb2.services.GenreService;
import com.uwetrottmann.tmdb2.services.MoviesService;
import com.uwetrottmann.tmdb2.services.PeopleService;
import com.uwetrottmann.tmdb2.services.SearchService;
import com.uwetrottmann.tmdb2.services.TvEpisodesService;
import com.uwetrottmann.tmdb2.services.TvSeasonsService;
import com.uwetrottmann.tmdb2.services.TvService;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Helper class for easy usage of the TMDB v3 API using retrofit.
*
* Create an instance of this class and then call any of the service methods.
*
*
The service methods take care of constructing the required {@link Retrofit} instance and creating the service. It
* is recommended you use your existing OkHttp client instance by overriding {@link #okHttpClient()}.
*
*
Only one {@link Retrofit} instance is created upon the first and re-used for any consequent service method call.
*/
public class Tmdb {
public static final String API_HOST = "api.themoviedb.org";
public static final String API_VERSION = "3";
public static final String API_URL = "https://" + API_HOST + "/" + API_VERSION + "/";
/**
* API key query parameter name.
*/
public static final String PARAM_API_KEY = "api_key";
private OkHttpClient okHttpClient;
private Retrofit retrofit;
private String apiKey;
/**
* Create a new manager instance.
*
* @param apiKey Your TMDB API key.
*/
public Tmdb(String apiKey) {
this.apiKey = apiKey;
}
public void apiKey(String apiKey) {
this.apiKey = apiKey;
}
public String apiKey() {
return apiKey;
}
/**
* 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(API_URL)
.addConverterFactory(GsonConverterFactory.create(TmdbHelper.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 an interceptor to add the api key query parameter and to log requests.
*/
protected void setOkHttpClientDefaults(OkHttpClient.Builder builder) {
builder.addInterceptor(new TmdbInterceptor(this));
}
/**
* Return the current {@link Retrofit} instance. If none exists (first call, auth changed), builds a new one.
*
When building, sets the base url and a custom client with an {@link Interceptor} which supplies authentication
* data.
*/
protected Retrofit getRetrofit() {
if (retrofit == null) {
retrofit = retrofitBuilder().build();
}
return retrofit;
}
public ConfigurationService configurationService() {
return getRetrofit().create(ConfigurationService.class);
}
public FindService findService() {
return getRetrofit().create(FindService.class);
}
public MoviesService moviesService() {
return getRetrofit().create(MoviesService.class);
}
public PeopleService personService() {
return getRetrofit().create(PeopleService.class);
}
public SearchService searchService() {
return getRetrofit().create(SearchService.class);
}
public TvService tvService() {
return getRetrofit().create(TvService.class);
}
public TvSeasonsService tvSeasonsService() {
return getRetrofit().create(TvSeasonsService.class);
}
public TvEpisodesService tvEpisodesService() {
return getRetrofit().create(TvEpisodesService.class);
}
public DiscoverService discoverService() {
return getRetrofit().create(DiscoverService.class);
}
public CollectionService collectionService() {
return getRetrofit().create(CollectionService.class);
}
public GenreService genreService() {
return getRetrofit().create(GenreService.class);
}
}