diff --git a/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java b/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java index 1f73a6cbb..9a7a9d5eb 100644 --- a/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java +++ b/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java @@ -21,66 +21,16 @@ package com.omertron.themoviedbapi; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.omertron.themoviedbapi.model.Account; -import com.omertron.themoviedbapi.model.AlternativeTitle; -import com.omertron.themoviedbapi.model.Artwork; -import com.omertron.themoviedbapi.model.ArtworkType; -import com.omertron.themoviedbapi.model.ChangeKeyItem; -import com.omertron.themoviedbapi.model.ChangedItem; -import com.omertron.themoviedbapi.model.ChangedMovie; -import com.omertron.themoviedbapi.model.Collection; -import com.omertron.themoviedbapi.model.CollectionInfo; -import com.omertron.themoviedbapi.model.Company; -import com.omertron.themoviedbapi.model.Discover; -import com.omertron.themoviedbapi.model.Genre; -import com.omertron.themoviedbapi.model.JobDepartment; -import com.omertron.themoviedbapi.model.Keyword; -import com.omertron.themoviedbapi.model.KeywordMovie; -import com.omertron.themoviedbapi.model.ListItemStatus; -import com.omertron.themoviedbapi.model.MovieDb; -import com.omertron.themoviedbapi.model.MovieDbList; -import com.omertron.themoviedbapi.model.MovieDbListStatus; -import com.omertron.themoviedbapi.model.MovieList; -import com.omertron.themoviedbapi.model.Person; -import com.omertron.themoviedbapi.model.PersonCredit; -import com.omertron.themoviedbapi.model.ReleaseInfo; -import com.omertron.themoviedbapi.model.Reviews; -import com.omertron.themoviedbapi.model.StatusCode; -import com.omertron.themoviedbapi.model.TmdbConfiguration; -import com.omertron.themoviedbapi.model.TokenAuthorisation; -import com.omertron.themoviedbapi.model.TokenSession; -import com.omertron.themoviedbapi.model.Translation; -import com.omertron.themoviedbapi.model.Video; +import com.omertron.themoviedbapi.model.*; import com.omertron.themoviedbapi.results.TmdbResultsList; import com.omertron.themoviedbapi.results.TmdbResultsMap; import com.omertron.themoviedbapi.tools.ApiUrl; import com.omertron.themoviedbapi.tools.HttpTools; +import com.omertron.themoviedbapi.tools.MethodBase; +import com.omertron.themoviedbapi.tools.MethodSub; import com.omertron.themoviedbapi.tools.Param; import com.omertron.themoviedbapi.tools.TmdbParameters; -import com.omertron.themoviedbapi.wrapper.WrapperAlternativeTitles; -import com.omertron.themoviedbapi.wrapper.WrapperChanges; -import com.omertron.themoviedbapi.wrapper.WrapperCollection; -import com.omertron.themoviedbapi.wrapper.WrapperCompany; -import com.omertron.themoviedbapi.wrapper.WrapperCompanyMovies; -import com.omertron.themoviedbapi.wrapper.WrapperConfig; -import com.omertron.themoviedbapi.wrapper.WrapperGenres; -import com.omertron.themoviedbapi.wrapper.WrapperImages; -import com.omertron.themoviedbapi.wrapper.WrapperJobList; -import com.omertron.themoviedbapi.wrapper.WrapperKeywordMovies; -import com.omertron.themoviedbapi.wrapper.WrapperKeywords; -import com.omertron.themoviedbapi.wrapper.WrapperMovie; -import com.omertron.themoviedbapi.wrapper.WrapperMovieCasts; -import com.omertron.themoviedbapi.wrapper.WrapperMovieChanges; -import com.omertron.themoviedbapi.wrapper.WrapperMovieDbList; -import com.omertron.themoviedbapi.wrapper.WrapperMovieKeywords; -import com.omertron.themoviedbapi.wrapper.WrapperMovieList; -import com.omertron.themoviedbapi.wrapper.WrapperPerson; -import com.omertron.themoviedbapi.wrapper.WrapperPersonCredits; -import com.omertron.themoviedbapi.wrapper.WrapperPersonList; -import com.omertron.themoviedbapi.wrapper.WrapperReleaseInfo; -import com.omertron.themoviedbapi.wrapper.WrapperReviews; -import com.omertron.themoviedbapi.wrapper.WrapperTranslations; -import com.omertron.themoviedbapi.wrapper.WrapperVideos; +import com.omertron.themoviedbapi.wrapper.*; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; @@ -98,8 +48,7 @@ import org.yamj.api.common.http.SimpleHttpClientBuilder; /** * The MovieDb API *

- * This is for version 3 of the API as specified here: - * http://help.themoviedb.org/kb/api/about-3 + * This is for version 3 of the API as specified here: http://help.themoviedb.org/kb/api/about-3 * * @author stuart.boston */ @@ -109,21 +58,6 @@ public class TheMovieDbApi { private String apiKey; private TmdbConfiguration tmdbConfig; private HttpTools httpTools; - // API Methods - private static final String BASE_MOVIE = "movie"; - private static final String BASE_PERSON = "person"; - private static final String BASE_COMPANY = "company"; - private static final String BASE_GENRE = "genre"; - private static final String BASE_AUTH = "authentication"; - private static final String BASE_COLLECTION = "collection"; - private static final String BASE_ACCOUNT = "account"; - private static final String BASE_SEARCH = "search"; - private static final String BASE_LIST = "list"; - private static final String BASE_KEYWORD = "keyword"; - private static final String BASE_JOB = "job"; - private static final String BASE_DISCOVER = "discover"; - private static final String URL_MOVIES = "movies"; - private static final String URL_IMAGES = "images"; // Jackson JSON configuration private static ObjectMapper mapper = new ObjectMapper(); // Constants @@ -153,7 +87,7 @@ public class TheMovieDbApi { this.apiKey = apiKey; this.httpTools = new HttpTools(httpClient); - URL configUrl = new ApiUrl(apiKey, "configuration").buildUrl(); + URL configUrl = new ApiUrl(apiKey, MethodBase.CONFIGURATION).buildUrl(); String webpage = httpTools.getRequest(configUrl); try { @@ -182,8 +116,7 @@ public class TheMovieDbApi { * @param moviedb The moviedb object to compare too * @param title The title of the movie to compare * @param year The year of the movie to compare - * @param maxDistance The Levenshtein Distance between the two titles. 0 = - * exact match + * @param maxDistance The Levenshtein Distance between the two titles. 0 = exact match * @param caseSensitive true if the comparison is to be case sensitive * @return True if there is a match, False otherwise. */ @@ -282,7 +215,7 @@ public class TheMovieDbApi { */ public URL createImageUrl(String imagePath, String requiredSize) throws MovieDbException { if (!tmdbConfig.isValidSize(requiredSize)) { - throw new MovieDbException(ApiExceptionType.INVALID_IMAGE, requiredSize, ""); + throw new MovieDbException(ApiExceptionType.INVALID_IMAGE, requiredSize); } StringBuilder sb = new StringBuilder(tmdbConfig.getBaseUrl()); @@ -299,23 +232,20 @@ public class TheMovieDbApi { // /** - * This method is used to generate a valid request token for user based - * authentication. + * This method is used to generate a valid request token for user based authentication. * * A request token is required in order to request a session id. * - * You can generate any number of request tokens but they will expire after - * 60 minutes. + * You can generate any number of request tokens but they will expire after 60 minutes. * - * As soon as a valid session id has been created the token will be - * destroyed. + * As soon as a valid session id has been created the token will be destroyed. * * @return * @throws MovieDbException */ public TokenAuthorisation getAuthorisationToken() throws MovieDbException { TmdbParameters parameters = new TmdbParameters(); - URL url = new ApiUrl(apiKey, BASE_AUTH).setSubMethod("token/new").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.AUTH).setSubMethod(MethodSub.TOKEN_NEW).buildUrl(parameters); String webpage = httpTools.getRequest(url); @@ -328,8 +258,7 @@ public class TheMovieDbApi { } /** - * This method is used to generate a session id for user based - * authentication. + * This method is used to generate a session id for user based authentication. * * A session id is required in order to use any of the write methods. * @@ -346,7 +275,7 @@ public class TheMovieDbApi { } parameters.add(Param.TOKEN, token.getRequestToken()); - URL url = new ApiUrl(apiKey, BASE_AUTH).setSubMethod("session/new").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.AUTH).setSubMethod(MethodSub.SESSION_NEW).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -358,8 +287,7 @@ public class TheMovieDbApi { } /** - * This method is used to generate a session id for user based - * authentication. User must provide their username and password + * This method is used to generate a session id for user based authentication. User must provide their username and password * * A session id is required in order to use any of the write methods. * @@ -381,7 +309,7 @@ public class TheMovieDbApi { parameters.add(Param.USERNAME, username); parameters.add(Param.PASSWORD, password); - URL url = new ApiUrl(apiKey, BASE_AUTH).setSubMethod("token/validate_with_login").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.AUTH).setSubMethod(MethodSub.TOKEN_VALIDATE).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -395,24 +323,20 @@ public class TheMovieDbApi { /** * This method is used to generate a guest session id. * - * A guest session can be used to rate movies without having a registered - * TMDb user account. + * A guest session can be used to rate movies without having a registered TMDb user account. * - * You should only generate a single guest session per user (or device) as - * you will be able to attach the ratings to a TMDb user account in the - * future. + * You should only generate a single guest session per user (or device) as you will be able to attach the ratings to a TMDb user + * account in the future. * - * There are also IP limits in place so you should always make sure it's the - * end user doing the guest session actions. + * There are also IP limits in place so you should always make sure it's the end user doing the guest session actions. * - * If a guest session is not used for the first time within 24 hours, it - * will be automatically discarded. + * If a guest session is not used for the first time within 24 hours, it will be automatically discarded. * * @return * @throws MovieDbException */ public TokenSession getGuestSessionToken() throws MovieDbException { - URL url = new ApiUrl(apiKey, BASE_AUTH).setSubMethod("guest_session/new").buildUrl(); + URL url = new ApiUrl(apiKey, MethodBase.AUTH).setSubMethod(MethodSub.GUEST_SESSION).buildUrl(); String webpage = httpTools.getRequest(url); try { @@ -424,8 +348,7 @@ public class TheMovieDbApi { } /** - * Get the basic information for an account. You will need to have a valid - * session id. + * Get the basic information for an account. You will need to have a valid session id. * * @param sessionId * @return @@ -435,7 +358,7 @@ public class TheMovieDbApi { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.SESSION, sessionId); - URL url = new ApiUrl(apiKey, BASE_ACCOUNT).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -458,7 +381,7 @@ public class TheMovieDbApi { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.SESSION, sessionId); - URL url = new ApiUrl(apiKey, BASE_ACCOUNT).setSubMethod(accountId, "favorite_movies").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(accountId, MethodSub.FAVORITE_MOVIES).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -478,7 +401,7 @@ public class TheMovieDbApi { body.put("favorite", isFavorite); String jsonBody = convertToJson(body); - URL url = new ApiUrl(apiKey, BASE_ACCOUNT).setSubMethod(accountId, "favorite").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(accountId, MethodSub.FAVORITE).buildUrl(parameters); String webpage = httpTools.postRequest(url, jsonBody); try { @@ -524,7 +447,7 @@ public class TheMovieDbApi { body.put("movie_watchlist", add); String jsonBody = convertToJson(body); - URL url = new ApiUrl(apiKey, BASE_ACCOUNT).setSubMethod(accountId, "movie_watchlist").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(accountId, MethodSub.MOVIE_WATCHLIST).buildUrl(parameters); String webpage = httpTools.postRequest(url, jsonBody); try { @@ -542,8 +465,7 @@ public class TheMovieDbApi { * * It will return the single highest rated poster and backdrop. * - * ApiExceptionType.MOVIE_ID_NOT_FOUND will be thrown if there are no movies - * found. + * ApiExceptionType.MOVIE_ID_NOT_FOUND will be thrown if there are no movies found. * * @param movieId * @param language @@ -557,7 +479,7 @@ public class TheMovieDbApi { parameters.add(Param.LANGUAGE, language); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, BASE_MOVIE).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { MovieDb movie = mapper.readValue(webpage, MovieDb.class); @@ -577,8 +499,7 @@ public class TheMovieDbApi { * * It will return the single highest rated poster and backdrop. * - * ApiExceptionType.MOVIE_ID_NOT_FOUND will be thrown if there are no movies - * found. + * ApiExceptionType.MOVIE_ID_NOT_FOUND will be thrown if there are no movies found. * * @param imdbId * @param language @@ -592,7 +513,7 @@ public class TheMovieDbApi { parameters.add(Param.LANGUAGE, language); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, BASE_MOVIE).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -609,8 +530,7 @@ public class TheMovieDbApi { } /** - * This method is used to retrieve all of the alternative titles we have for - * a particular movie. + * This method is used to retrieve all of the alternative titles we have for a particular movie. * * @param movieId * @param country @@ -624,7 +544,7 @@ public class TheMovieDbApi { parameters.add(Param.COUNTRY, country); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, BASE_MOVIE).setSubMethod("alternative_titles").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.ALT_TITLES).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { WrapperAlternativeTitles wrapper = mapper.readValue(webpage, WrapperAlternativeTitles.class); @@ -652,7 +572,7 @@ public class TheMovieDbApi { parameters.add(Param.ID, movieId); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, BASE_MOVIE).setSubMethod("casts").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.CASTS).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -667,8 +587,7 @@ public class TheMovieDbApi { } /** - * This method should be used when you’re wanting to retrieve all of the - * images for a particular movie. + * This method should be used when you’re wanting to retrieve all of the images for a particular movie. * * @param movieId * @param language @@ -682,7 +601,7 @@ public class TheMovieDbApi { parameters.add(Param.LANGUAGE, language); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, BASE_MOVIE).setSubMethod(URL_IMAGES).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.IMAGES).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -697,8 +616,7 @@ public class TheMovieDbApi { } /** - * This method is used to retrieve all of the keywords that have been added - * to a particular movie. + * This method is used to retrieve all of the keywords that have been added to a particular movie. * * Currently, only English keywords exist. * @@ -712,7 +630,7 @@ public class TheMovieDbApi { parameters.add(Param.ID, movieId); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, BASE_MOVIE).setSubMethod("keywords").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.KEYWORDS).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -727,8 +645,7 @@ public class TheMovieDbApi { } /** - * This method is used to retrieve all of the release and certification data - * we have for a specific movie. + * This method is used to retrieve all of the release and certification data we have for a specific movie. * * @param movieId * @param language @@ -742,7 +659,7 @@ public class TheMovieDbApi { parameters.add(Param.LANGUAGE, language); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, BASE_MOVIE).setSubMethod("releases").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.RELEASES).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -757,8 +674,7 @@ public class TheMovieDbApi { } /** - * This method is used to retrieve all of the trailers for a particular - * movie. + * This method is used to retrieve all of the trailers for a particular movie. * * Supported sites are YouTube and QuickTime. * @@ -774,7 +690,7 @@ public class TheMovieDbApi { parameters.add(Param.LANGUAGE, language); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, BASE_MOVIE).setSubMethod("videos").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.VIDEOS).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -789,8 +705,7 @@ public class TheMovieDbApi { } /** - * This method is used to retrieve a list of the available translations for - * a specific movie. + * This method is used to retrieve a list of the available translations for a specific movie. * * @param movieId * @param appendToResponse @@ -802,7 +717,7 @@ public class TheMovieDbApi { parameters.add(Param.ID, movieId); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, BASE_MOVIE).setSubMethod("translations").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.TRANSLATIONS).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -817,11 +732,9 @@ public class TheMovieDbApi { } /** - * The similar movies method will let you retrieve the similar movies for a - * particular movie. + * The similar movies method will let you retrieve the similar movies for a particular movie. * - * This data is created dynamically but with the help of users votes on - * TMDb. + * This data is created dynamically but with the help of users votes on TMDb. * * The data is much better with movies that have more keywords * @@ -839,7 +752,7 @@ public class TheMovieDbApi { parameters.add(Param.PAGE, page); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, BASE_MOVIE).setSubMethod("similar_movies").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.SIMILAR_MOVIES).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -860,7 +773,7 @@ public class TheMovieDbApi { parameters.add(Param.PAGE, page); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, BASE_MOVIE).setSubMethod("reviews").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.REVIEWS).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -891,7 +804,7 @@ public class TheMovieDbApi { parameters.add(Param.PAGE, page); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, BASE_MOVIE).setSubMethod("lists").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.LISTS).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -912,13 +825,11 @@ public class TheMovieDbApi { * * By default, only the last 24 hours of changes are returned. * - * The maximum number of days that can be returned in a single request is - * 14. + * The maximum number of days that can be returned in a single request is 14. * * The language is present on fields that are translatable. * - * TODO: DOES NOT WORK AT THE MOMENT. This is due to the "value" item - * changing type in the ChangeItem + * TODO: DOES NOT WORK AT THE MOMENT. This is due to the "value" item changing type in the ChangeItem * * @param movieId * @param startDate the start date of the changes, optional @@ -932,7 +843,7 @@ public class TheMovieDbApi { parameters.add(Param.START_DATE, startDate); parameters.add(Param.END_DATE, endDate); - URL url = new ApiUrl(apiKey, BASE_MOVIE).setSubMethod("changes").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.CHANGES).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { WrapperChanges wrapper = mapper.readValue(webpage, WrapperChanges.class); @@ -957,7 +868,7 @@ public class TheMovieDbApi { * @throws MovieDbException */ public MovieDb getLatestMovie() throws MovieDbException { - URL url = new ApiUrl(apiKey, BASE_MOVIE).setSubMethod("latest").buildUrl(); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.LATEST).buildUrl(); String webpage = httpTools.getRequest(url); try { @@ -985,7 +896,7 @@ public class TheMovieDbApi { parameters.add(Param.LANGUAGE, language); parameters.add(Param.PAGE, page); - URL url = new ApiUrl(apiKey, BASE_MOVIE).setSubMethod("upcoming").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.UPCOMING).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1003,8 +914,7 @@ public class TheMovieDbApi { /** * This method is used to retrieve the movies currently in theatres. * - * This is a curated list that will normally contain 100 movies. The default - * response will return 20 movies. + * This is a curated list that will normally contain 100 movies. The default response will return 20 movies. * * TODO: Implement more than 20 movies * @@ -1018,7 +928,7 @@ public class TheMovieDbApi { parameters.add(Param.LANGUAGE, language); parameters.add(Param.PAGE, page); - URL url = new ApiUrl(apiKey, BASE_MOVIE).setSubMethod("now-playing").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.NOW_PLAYING).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1049,7 +959,7 @@ public class TheMovieDbApi { parameters.add(Param.LANGUAGE, language); parameters.add(Param.PAGE, page); - URL url = new ApiUrl(apiKey, BASE_MOVIE).setSubMethod("popular").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.POPULAR).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1064,8 +974,7 @@ public class TheMovieDbApi { } /** - * This method is used to retrieve the top rated movies that have over 10 - * votes on TMDb. + * This method is used to retrieve the top rated movies that have over 10 votes on TMDb. * * The default response will return 20 movies. * @@ -1081,7 +990,7 @@ public class TheMovieDbApi { parameters.add(Param.LANGUAGE, language); parameters.add(Param.PAGE, page); - URL url = new ApiUrl(apiKey, BASE_MOVIE).setSubMethod("top-rated").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.TOP_RATED).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1107,7 +1016,7 @@ public class TheMovieDbApi { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.SESSION, sessionId); - URL url = new ApiUrl(apiKey, BASE_ACCOUNT).setSubMethod(accountId, "rated/movies").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(accountId, MethodSub.RATED_MOVIES).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1137,7 +1046,7 @@ public class TheMovieDbApi { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.SESSION, sessionId); - URL url = new ApiUrl(apiKey, BASE_MOVIE).setSubMethod(movieId, "rating").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(movieId, MethodSub.RATING).buildUrl(parameters); String jsonBody = convertToJson(Collections.singletonMap("value", rating)); String webpage = httpTools.postRequest(url, jsonBody); @@ -1156,11 +1065,9 @@ public class TheMovieDbApi { // /** - * This method is used to retrieve all of the basic information about a - * movie collection. + * This method is used to retrieve all of the basic information about a movie collection. * - * You can get the ID needed for this method by making a getMovieInfo - * request for the belongs_to_collection. + * You can get the ID needed for this method by making a getMovieInfo request for the belongs_to_collection. * * @param collectionId * @param language @@ -1172,7 +1079,7 @@ public class TheMovieDbApi { parameters.add(Param.ID, collectionId); parameters.add(Param.LANGUAGE, language); - URL url = new ApiUrl(apiKey, BASE_COLLECTION).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.COLLECTION).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1196,7 +1103,7 @@ public class TheMovieDbApi { parameters.add(Param.ID, collectionId); parameters.add(Param.LANGUAGE, language); - URL url = new ApiUrl(apiKey, BASE_COLLECTION).setSubMethod(URL_IMAGES).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.COLLECTION).setSubMethod(MethodSub.IMAGES).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1227,7 +1134,7 @@ public class TheMovieDbApi { parameters.add(Param.ID, personId); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, BASE_PERSON).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.PERSON).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1239,8 +1146,7 @@ public class TheMovieDbApi { } /** - * This method is used to retrieve all of the cast & crew information for - * the person. + * This method is used to retrieve all of the cast & crew information for the person. * * It will return the single highest rated poster for each movie record. * @@ -1254,7 +1160,7 @@ public class TheMovieDbApi { parameters.add(Param.ID, personId); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, BASE_PERSON).setSubMethod("credits").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.PERSON).setSubMethod(MethodSub.CREDITS).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1279,7 +1185,7 @@ public class TheMovieDbApi { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.ID, personId); - URL url = new ApiUrl(apiKey, BASE_PERSON).setSubMethod(URL_IMAGES).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.PERSON).setSubMethod(MethodSub.IMAGES).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1300,8 +1206,7 @@ public class TheMovieDbApi { * * By default, only the last 24 hours of changes are returned. * - * The maximum number of days that can be returned in a single request is - * 14. + * The maximum number of days that can be returned in a single request is 14. * * The language is present on fields that are translatable. * @@ -1340,7 +1245,7 @@ public class TheMovieDbApi { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.PAGE, page); - URL url = new ApiUrl(apiKey, BASE_PERSON).setSubMethod("popular").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.PERSON).setSubMethod(MethodSub.POPULAR).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1361,7 +1266,7 @@ public class TheMovieDbApi { * @throws MovieDbException */ public Person getPersonLatest() throws MovieDbException { - URL url = new ApiUrl(apiKey, BASE_PERSON).setSubMethod("latest").buildUrl(); + URL url = new ApiUrl(apiKey, MethodBase.PERSON).setSubMethod(MethodSub.LATEST).buildUrl(); String webpage = httpTools.getRequest(url); try { @@ -1375,8 +1280,7 @@ public class TheMovieDbApi { // /** - * This method is used to retrieve the basic information about a production - * company on TMDb. + * This method is used to retrieve the basic information about a production company on TMDb. * * @param companyId * @return @@ -1386,7 +1290,7 @@ public class TheMovieDbApi { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.ID, companyId); - URL url = new ApiUrl(apiKey, BASE_COMPANY).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.COMPANY).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1400,8 +1304,7 @@ public class TheMovieDbApi { /** * This method is used to retrieve the movies associated with a company. * - * These movies are returned in order of most recently released to oldest. - * The default response will return 20 movies per page. + * These movies are returned in order of most recently released to oldest. The default response will return 20 movies per page. * * TODO: Implement more than 20 movies * @@ -1417,7 +1320,7 @@ public class TheMovieDbApi { parameters.add(Param.LANGUAGE, language); parameters.add(Param.PAGE, page); - URL url = new ApiUrl(apiKey, BASE_COMPANY).setSubMethod(URL_MOVIES).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.COMPANY).setSubMethod(MethodSub.MOVIES).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1446,7 +1349,7 @@ public class TheMovieDbApi { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.LANGUAGE, language); - URL url = new ApiUrl(apiKey, BASE_GENRE).setSubMethod("list").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.GENRE).setSubMethod(MethodSub.LIST).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1463,11 +1366,9 @@ public class TheMovieDbApi { /** * Get a list of movies per genre. * - * It is important to understand that only movies with more than 10 votes - * get listed. + * It is important to understand that only movies with more than 10 votes get listed. * - * This prevents movies from 1 10/10 rating from being listed first and for - * the first 5 pages. + * This prevents movies from 1 10/10 rating from being listed first and for the first 5 pages. * * @param genreId * @param language @@ -1483,7 +1384,7 @@ public class TheMovieDbApi { parameters.add(Param.PAGE, page); parameters.add(Param.INCLUDE_ALL_MOVIES, includeAllMovies); - URL url = new ApiUrl(apiKey, BASE_GENRE).setSubMethod(URL_MOVIES).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.GENRE).setSubMethod(MethodSub.MOVIES).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1500,16 +1401,13 @@ public class TheMovieDbApi { // /** - * Search Movies This is a good starting point to start finding movies on - * TMDb. + * Search Movies This is a good starting point to start finding movies on TMDb. * * @param movieName - * @param searchYear Limit the search to the provided year. Zero (0) will - * get all years + * @param searchYear Limit the search to the provided year. Zero (0) will get all years * @param language The language to include. Can be blank/null. * @param includeAdult true or false to include adult titles in the search - * @param page The page of results to return. 0 to get the default (first - * page) + * @param page The page of results to return. 0 to get the default (first page) * @return * @throws MovieDbException */ @@ -1521,7 +1419,7 @@ public class TheMovieDbApi { parameters.add(Param.ADULT, includeAdult); parameters.add(Param.PAGE, page); - URL url = new ApiUrl(apiKey, BASE_SEARCH).setSubMethod("movie").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.SEARCH).setSubMethod(MethodSub.MOVIE).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1551,7 +1449,7 @@ public class TheMovieDbApi { parameters.add(Param.LANGUAGE, language); parameters.add(Param.PAGE, page); - URL url = new ApiUrl(apiKey, BASE_SEARCH).setSubMethod("collection").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.SEARCH).setSubMethod(MethodSub.COLLECTION).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1568,8 +1466,7 @@ public class TheMovieDbApi { /** * This is a good starting point to start finding people on TMDb. * - * The idea is to be a quick and light method so you can iterate through - * people quickly. + * The idea is to be a quick and light method so you can iterate through people quickly. * * @param personName * @param includeAdult @@ -1583,7 +1480,7 @@ public class TheMovieDbApi { parameters.add(Param.ADULT, includeAdult); parameters.add(Param.PAGE, page); - URL url = new ApiUrl(apiKey, BASE_SEARCH).setSubMethod("person").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.SEARCH).setSubMethod(MethodSub.PERSON).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1612,7 +1509,7 @@ public class TheMovieDbApi { parameters.add(Param.LANGUAGE, language); parameters.add(Param.PAGE, page); - URL url = new ApiUrl(apiKey, BASE_SEARCH).setSubMethod("list").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.SEARCH).setSubMethod(MethodSub.LIST).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1629,8 +1526,8 @@ public class TheMovieDbApi { /** * Search Companies. * - * You can use this method to search for production companies that are part - * of TMDb. The company IDs will map to those returned on movie calls. + * You can use this method to search for production companies that are part of TMDb. The company IDs will map to those returned + * on movie calls. * * http://help.themoviedb.org/kb/api/search-companies * @@ -1644,7 +1541,7 @@ public class TheMovieDbApi { parameters.add(Param.QUERY, companyName); parameters.add(Param.PAGE, page); - URL url = new ApiUrl(apiKey, BASE_SEARCH).setSubMethod("company").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.SEARCH).setSubMethod(MethodSub.COMPANY).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1671,7 +1568,7 @@ public class TheMovieDbApi { parameters.add(Param.QUERY, query); parameters.add(Param.PAGE, page); - URL url = new ApiUrl(apiKey, BASE_SEARCH).setSubMethod("keyword").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.SEARCH).setSubMethod(MethodSub.KEYWORD).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1698,7 +1595,7 @@ public class TheMovieDbApi { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.ID, listId); - URL url = new ApiUrl(apiKey, BASE_LIST).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.LIST).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1721,7 +1618,7 @@ public class TheMovieDbApi { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.SESSION, sessionId); - URL url = new ApiUrl(apiKey, BASE_ACCOUNT).setSubMethod(accountID, "lists").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(accountID, MethodSub.LISTS).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1750,7 +1647,7 @@ public class TheMovieDbApi { body.put("description", StringUtils.trimToEmpty(description)); String jsonBody = convertToJson(body); - URL url = new ApiUrl(apiKey, "list").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.LIST).buildUrl(parameters); String webpage = httpTools.postRequest(url, jsonBody); try { @@ -1773,7 +1670,7 @@ public class TheMovieDbApi { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.ID, movieId); - URL url = new ApiUrl(apiKey, BASE_LIST).setSubMethod(listId, "item_status").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.LIST).setSubMethod(listId, MethodSub.ITEM_STATUS).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1785,8 +1682,7 @@ public class TheMovieDbApi { } /** - * This method lets users add new movies to a list that they created. A - * valid session id is required. + * This method lets users add new movies to a list that they created. A valid session id is required. * * @param sessionId * @param listId @@ -1795,12 +1691,11 @@ public class TheMovieDbApi { * @throws MovieDbException */ public StatusCode addMovieToList(String sessionId, String listId, Integer movieId) throws MovieDbException { - return modifyMovieList(sessionId, listId, movieId, "add_item"); + return modifyMovieList(sessionId, listId, movieId, MethodSub.ADD_ITEM); } /** - * This method lets users remove movies from a list that they created. A - * valid session id is required. + * This method lets users remove movies from a list that they created. A valid session id is required. * * @param sessionId * @param listId @@ -1809,16 +1704,16 @@ public class TheMovieDbApi { * @throws MovieDbException */ public StatusCode removeMovieFromList(String sessionId, String listId, Integer movieId) throws MovieDbException { - return modifyMovieList(sessionId, listId, movieId, "remove_item"); + return modifyMovieList(sessionId, listId, movieId, MethodSub.REMOVE_ITEM); } - private StatusCode modifyMovieList(String sessionId, String listId, Integer movieId, String operation) throws MovieDbException { + private StatusCode modifyMovieList(String sessionId, String listId, Integer movieId, MethodSub operation) throws MovieDbException { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.SESSION, sessionId); - String jsonBody = convertToJson(Collections.singletonMap("media_id", movieId + "")); + String jsonBody = convertToJson(Collections.singletonMap("media_id", String.valueOf(movieId))); - URL url = new ApiUrl(apiKey, BASE_LIST).setSubMethod(listId, operation).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.LIST).setSubMethod(listId, operation).buildUrl(parameters); String webpage = httpTools.postRequest(url, jsonBody); try { @@ -1841,7 +1736,7 @@ public class TheMovieDbApi { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.SESSION, sessionId); - URL url = new ApiUrl(apiKey, BASE_ACCOUNT).setSubMethod(accountId, "movie_watchlist").buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(accountId, MethodSub.MOVIE_WATCHLIST).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1853,8 +1748,7 @@ public class TheMovieDbApi { } /** - * This method lets users delete a list that they created. A valid session - * id is required. + * This method lets users delete a list that they created. A valid session id is required. * * @param sessionId * @param listId @@ -1863,9 +1757,10 @@ public class TheMovieDbApi { */ public StatusCode deleteMovieList(String sessionId, String listId) throws MovieDbException { TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.ID, listId); parameters.add(Param.SESSION, sessionId); - URL url = new ApiUrl(apiKey, BASE_LIST).setSubMethod(listId).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.LIST).buildUrl(parameters); String webpage = httpTools.deleteRequest(url); try { @@ -1889,7 +1784,7 @@ public class TheMovieDbApi { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.ID, keywordId); - URL url = new ApiUrl(apiKey, BASE_KEYWORD).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.KEYWORD).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1916,7 +1811,7 @@ public class TheMovieDbApi { parameters.add(Param.LANGUAGE, language); parameters.add(Param.PAGE, page); - URL url = new ApiUrl(apiKey, BASE_KEYWORD).setSubMethod(URL_MOVIES).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.KEYWORD).setSubMethod(MethodSub.MOVIES).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -1934,12 +1829,10 @@ public class TheMovieDbApi { // /** - * Get a list of movie ids that have been edited. By default we show the - * last 24 hours and only 100 items per page. The maximum number of days - * that can be returned in a single request is 14. You can then use the - * movie changes API to get the actual data that has been changed. Please - * note that the change log system to support this was changed on October 5, - * 2012 and will only show movies that have been edited since. + * Get a list of movie ids that have been edited. By default we show the last 24 hours and only 100 items per page. The maximum + * number of days that can be returned in a single request is 14. You can then use the movie changes API to get the actual data + * that has been changed. Please note that the change log system to support this was changed on October 5, 2012 and will only + * show movies that have been edited since. * * @param page * @param startDate the start date of the changes, optional @@ -1953,7 +1846,7 @@ public class TheMovieDbApi { params.add(Param.START_DATE, startDate); params.add(Param.END_DATE, endDate); - URL url = new ApiUrl(apiKey, BASE_MOVIE).setSubMethod("changes").buildUrl(params); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.CHANGES).buildUrl(params); String webpage = httpTools.getRequest(url); try { @@ -1976,7 +1869,7 @@ public class TheMovieDbApi { // public TmdbResultsList getJobs() throws MovieDbException { - URL url = new ApiUrl(apiKey, BASE_JOB).setSubMethod("list").buildUrl(); + URL url = new ApiUrl(apiKey, MethodBase.JOB).setSubMethod(MethodSub.LIST).buildUrl(); String webpage = httpTools.getRequest(url); try { @@ -1993,42 +1886,30 @@ public class TheMovieDbApi { // /** - * Discover movies by different types of data like average rating, number of - * votes, genres and certifications. + * Discover movies by different types of data like average rating, number of votes, genres and certifications. * - * You can alternatively create a "discover" object and pass it to this - * method to cut out the requirement for all of these parameters + * You can alternatively create a "discover" object and pass it to this method to cut out the requirement for all of these + * parameters * * @param page Minimum value is 1 * @param language ISO 639-1 code. - * @param sortBy Available options are vote_average.desc, vote_average.asc, - * release_date.desc, release_date.asc, popularity.desc, popularity.asc + * @param sortBy Available options are vote_average.desc, vote_average.asc, release_date.desc, release_date.asc, + * popularity.desc, popularity.asc * @param includeAdult Toggle the inclusion of adult titles - * @param year Filter the results release dates to matches that include this - * value - * @param primaryReleaseYear Filter the results so that only the primary - * release date year has this value - * @param voteCountGte Only include movies that are equal to, or have a vote - * count higher than this value - * @param voteAverageGte Only include movies that are equal to, or have a - * higher average rating than this value - * @param withGenres Only include movies with the specified genres. Expected - * value is an integer (the id of a genre). Multiple values can be - * specified. Comma separated indicates an 'AND' query, while a pipe (|) - * separated value indicates an 'OR'. - * @param releaseDateGte The minimum release to include. Expected format is - * YYYY-MM-DD - * @param releaseDateLte The maximum release to include. Expected format is - * YYYY-MM-DD - * @param certificationCountry Only include movies with certifications for a - * specific country. When this value is specified, 'certificationLte' is - * required. A ISO 3166-1 is expected. - * @param certificationLte Only include movies with this certification and - * lower. Expected value is a valid certification for the specified - * 'certificationCountry'. - * @param withCompanies Filter movies to include a specific company. - * Expected value is an integer (the id of a company). They can be comma - * separated to indicate an 'AND' query. + * @param year Filter the results release dates to matches that include this value + * @param primaryReleaseYear Filter the results so that only the primary release date year has this value + * @param voteCountGte Only include movies that are equal to, or have a vote count higher than this value + * @param voteAverageGte Only include movies that are equal to, or have a higher average rating than this value + * @param withGenres Only include movies with the specified genres. Expected value is an integer (the id of a genre). Multiple + * values can be specified. Comma separated indicates an 'AND' query, while a pipe (|) separated value indicates an 'OR'. + * @param releaseDateGte The minimum release to include. Expected format is YYYY-MM-DD + * @param releaseDateLte The maximum release to include. Expected format is YYYY-MM-DD + * @param certificationCountry Only include movies with certifications for a specific country. When this value is specified, + * 'certificationLte' is required. A ISO 3166-1 is expected. + * @param certificationLte Only include movies with this certification and lower. Expected value is a valid certification for + * the specified 'certificationCountry'. + * @param withCompanies Filter movies to include a specific company. Expected value is an integer (the id of a company). They + * can be comma separated to indicate an 'AND' query. * @return * @throws MovieDbException */ @@ -2056,15 +1937,14 @@ public class TheMovieDbApi { } /** - * Discover movies by different types of data like average rating, number of - * votes, genres and certifications. + * Discover movies by different types of data like average rating, number of votes, genres and certifications. * * @param discover A discover object containing the search criteria required * @return * @throws MovieDbException */ public TmdbResultsList getDiscover(Discover discover) throws MovieDbException { - URL url = new ApiUrl(apiKey, BASE_DISCOVER).setSubMethod("movie").buildUrl(discover.getParams()); + URL url = new ApiUrl(apiKey, MethodBase.DISCOVER).setSubMethod(MethodSub.MOVIE).buildUrl(discover.getParams()); String webpage = httpTools.getRequest(url); try { diff --git a/src/main/java/com/omertron/themoviedbapi/tools/ApiUrl.java b/src/main/java/com/omertron/themoviedbapi/tools/ApiUrl.java index 66d301ffa..2fa2506ab 100644 --- a/src/main/java/com/omertron/themoviedbapi/tools/ApiUrl.java +++ b/src/main/java/com/omertron/themoviedbapi/tools/ApiUrl.java @@ -54,9 +54,9 @@ public class ApiUrl { * @param apiKey * @param method */ - public ApiUrl(String apiKey, String method) { + public ApiUrl(String apiKey, MethodBase method) { this.apiKey = apiKey; - this.method = method; + this.method = method.getValue(); } /** @@ -65,8 +65,8 @@ public class ApiUrl { * @param submethod * @return */ - public ApiUrl setSubMethod(String submethod) { - this.submethod = submethod; + public ApiUrl setSubMethod(MethodSub submethod) { + this.submethod = submethod.getValue(); return this; } @@ -77,8 +77,8 @@ public class ApiUrl { * @param submethod * @return */ - public ApiUrl setSubMethod(int someId, String submethod) { - this.submethod = someId + submethod; + public ApiUrl setSubMethod(int someId, MethodSub submethod) { + this.submethod = someId + submethod.getValue(); return this; } @@ -89,8 +89,8 @@ public class ApiUrl { * @param submethod * @return */ - public ApiUrl setSubMethod(String someId, String submethod) { - this.submethod = someId + submethod; + public ApiUrl setSubMethod(String someId, MethodSub submethod) { + this.submethod = someId + submethod.getValue(); return this; } diff --git a/src/main/java/com/omertron/themoviedbapi/tools/MethodBase.java b/src/main/java/com/omertron/themoviedbapi/tools/MethodBase.java new file mode 100644 index 000000000..2f8b1e6ed --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/tools/MethodBase.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2004-2015 Stuart Boston + * + * This file is part of TheMovieDB API. + * + * TheMovieDB API is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * TheMovieDB API is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with TheMovieDB API. If not, see . + * + */ +package com.omertron.themoviedbapi.tools; + +import java.util.EnumSet; +import org.apache.commons.lang3.StringUtils; + +public enum MethodBase { + + ACCOUNT("account"), + AUTH("authentication"), + COLLECTION("collection"), + COMPANY("company"), + CONFIGURATION("configuration"), + DISCOVER("discover"), + GENRE("genre"), + JOB("job"), + KEYWORD("keyword"), + LIST("list"), + MOVIE("movie"), + PERSON("person"), + SEARCH("search"); + + private final String value; + + private MethodBase(String value) { + this.value = value; + } + + /** + * Get the URL parameter to use + * + * @return + */ + public String getValue() { + return this.value; + } + + /** + * Convert a string into an Enum type + * + * @param value + * @return + */ + public static MethodBase fromString(String value) { + if (StringUtils.isNotBlank(value)) { + for (final MethodBase method : EnumSet.allOf(MethodBase.class)) { + if (value.equalsIgnoreCase(method.value)) { + return method; + } + } + } + + // We've not found the type! + throw new IllegalArgumentException("Method '" + value + "' not recognised"); + } +} diff --git a/src/main/java/com/omertron/themoviedbapi/tools/MethodSub.java b/src/main/java/com/omertron/themoviedbapi/tools/MethodSub.java new file mode 100644 index 000000000..8e54a1746 --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/tools/MethodSub.java @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2004-2015 Stuart Boston + * + * This file is part of TheMovieDB API. + * + * TheMovieDB API is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * TheMovieDB API is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with TheMovieDB API. If not, see . + * + */ +package com.omertron.themoviedbapi.tools; + +import java.util.EnumSet; +import org.apache.commons.lang3.StringUtils; + +public enum MethodSub { + + ALT_TITLES("alternative_titles"), + CASTS("casts"), + CHANGES("changes"), + COLLECTION("collection"), + COMPANY("company"), + CREDITS("credits"), + FAVORITE("favorite"), + FAVORITE_MOVIES("favorite_movies"), + GUEST_SESSION("guest_session/new"), + IMAGES("images"), + ITEM_STATUS("item_status"), + KEYWORD("keyword"), + KEYWORDS("keywords"), + LATEST("latest"), + LIST("list"), + LISTS("lists"), + MOVIE("movie"), + MOVIES("movies"), + MOVIE_WATCHLIST("movie_watchlist"), + NOW_PLAYING("now-playing"), + PERSON("person"), + POPULAR("popular"), + RATED_MOVIES("rated/movies"), + RATING("rating"), + RELEASES("releases"), + REVIEWS("reviews"), + SESSION_NEW("session/new"), + SIMILAR_MOVIES("similar_movies"), + TOKEN_NEW("token/new"), + TOKEN_VALIDATE("token/validate_with_login"), + TOP_RATED("top-rated"), + TRANSLATIONS("translations"), + UPCOMING("upcoming"), + VIDEOS("videos"), + ADD_ITEM("add_item"), + REMOVE_ITEM("remove_item"); + + private final String value; + + private MethodSub(String value) { + this.value = value; + } + + /** + * Get the URL parameter to use + * + * @return + */ + public String getValue() { + return this.value; + } + + /** + * Convert a string into an Enum type + * + * @param value + * @return + */ + public static MethodSub fromString(String value) { + if (StringUtils.isNotBlank(value)) { + for (final MethodSub method : EnumSet.allOf(MethodSub.class)) { + if (value.equalsIgnoreCase(method.value)) { + return method; + } + } + } + + // We've not found the type! + throw new IllegalArgumentException("Method '" + value + "' not recognised"); + } +} diff --git a/src/test/java/com/omertron/themoviedbapi/tools/ApiUrlTest.java b/src/test/java/com/omertron/themoviedbapi/tools/ApiUrlTest.java index e86e618aa..fb9979244 100644 --- a/src/test/java/com/omertron/themoviedbapi/tools/ApiUrlTest.java +++ b/src/test/java/com/omertron/themoviedbapi/tools/ApiUrlTest.java @@ -64,7 +64,7 @@ public class ApiUrlTest { public void testConfiguration() { LOG.info("Configuration Test"); - URL result = new ApiUrl(APIKEY, "configuration").buildUrl(); + URL result = new ApiUrl(APIKEY, MethodBase.CONFIGURATION).buildUrl(); String expResult = "http://api.themoviedb.org/3/configuration?api_key=APIKEY"; assertEquals("Wrong config URL", expResult, result.toString()); } @@ -75,8 +75,8 @@ public class ApiUrlTest { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.QUERY, "query"); - URL result = new ApiUrl(APIKEY, "method").buildUrl(parameters); - String expResult = "http://api.themoviedb.org/3/method?api_key=APIKEY&query=query"; + URL result = new ApiUrl(APIKEY, MethodBase.MOVIE).buildUrl(parameters); + String expResult = "http://api.themoviedb.org/3/movie?api_key=APIKEY&query=query"; assertEquals("Wrong Query URL", expResult, result.toString()); } @@ -86,8 +86,8 @@ public class ApiUrlTest { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.QUERY, "query"); - URL result = new ApiUrl(APIKEY, "method").setSubMethod("sub").buildUrl(parameters); - String expResult = "http://api.themoviedb.org/3/method/sub?api_key=APIKEY&query=query"; + URL result = new ApiUrl(APIKEY, MethodBase.MOVIE).setSubMethod(MethodSub.LATEST).buildUrl(parameters); + String expResult = "http://api.themoviedb.org/3/movie/latest?api_key=APIKEY&query=query"; assertEquals("Wrong Query-Sub URL", expResult, result.toString()); } @@ -99,8 +99,8 @@ public class ApiUrlTest { parameters.add(Param.PAGE, 1); parameters.add(Param.LANGUAGE, "lang"); - URL result = new ApiUrl(APIKEY, "method").setSubMethod("sub").buildUrl(parameters); - String expResult = "http://api.themoviedb.org/3/method/sub?api_key=APIKEY&query=query&language=lang&page=1"; + URL result = new ApiUrl(APIKEY, MethodBase.MOVIE).setSubMethod(MethodSub.LATEST).buildUrl(parameters); + String expResult = "http://api.themoviedb.org/3/movie/latest?api_key=APIKEY&query=query&language=lang&page=1"; assertEquals("Wrong Query Extra URL", expResult, result.toString()); } @@ -110,8 +110,8 @@ public class ApiUrlTest { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.ID, "ID"); - URL result = new ApiUrl(APIKEY, "method").buildUrl(parameters); - String expResult = "http://api.themoviedb.org/3/method/ID?api_key=APIKEY"; + URL result = new ApiUrl(APIKEY, MethodBase.MOVIE).buildUrl(parameters); + String expResult = "http://api.themoviedb.org/3/movie/ID?api_key=APIKEY"; assertEquals("Wrong ID URL", expResult, result.toString()); } @@ -121,8 +121,8 @@ public class ApiUrlTest { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.ID, "ID"); - URL result = new ApiUrl(APIKEY, "method").setSubMethod("sub").buildUrl(parameters); - String expResult = "http://api.themoviedb.org/3/method/ID/sub?api_key=APIKEY"; + URL result = new ApiUrl(APIKEY, MethodBase.MOVIE).setSubMethod(MethodSub.LATEST).buildUrl(parameters); + String expResult = "http://api.themoviedb.org/3/movie/ID/latest?api_key=APIKEY"; assertEquals("Wrong ID-Sub URL", expResult, result.toString()); } @@ -134,8 +134,8 @@ public class ApiUrlTest { parameters.add(Param.PAGE, 1); parameters.add(Param.LANGUAGE, "lang"); - URL result = new ApiUrl(APIKEY, "method").setSubMethod("sub").buildUrl(parameters); - String expResult = "http://api.themoviedb.org/3/method/ID/sub?api_key=APIKEY&language=lang&page=1"; + URL result = new ApiUrl(APIKEY, MethodBase.MOVIE).setSubMethod(MethodSub.LATEST).buildUrl(parameters); + String expResult = "http://api.themoviedb.org/3/movie/ID/latest?api_key=APIKEY&language=lang&page=1"; assertEquals("Wrong Query Extra URL", expResult, result.toString()); }