From dd3e04e6d1fafacda014bbda3987f6b5fa834b3a Mon Sep 17 00:00:00 2001 From: Stuart Boston Date: Wed, 4 Mar 2015 09:37:59 +0000 Subject: [PATCH] Simplify ApiUrl --- .../themoviedbapi/methods/TmdbAccount.java | 20 +-- .../methods/TmdbAuthentication.java | 8 +- .../methods/TmdbCertifications.java | 4 +- .../themoviedbapi/methods/TmdbChanges.java | 2 +- .../methods/TmdbCollections.java | 2 +- .../themoviedbapi/methods/TmdbCompanies.java | 2 +- .../methods/TmdbConfiguration.java | 4 +- .../themoviedbapi/methods/TmdbDiscover.java | 4 +- .../themoviedbapi/methods/TmdbGenres.java | 4 +- .../themoviedbapi/methods/TmdbKeywords.java | 2 +- .../themoviedbapi/methods/TmdbLists.java | 6 +- .../themoviedbapi/methods/TmdbMovies.java | 36 ++--- .../themoviedbapi/methods/TmdbPeople.java | 18 +-- .../themoviedbapi/methods/TmdbSearch.java | 16 +-- .../omertron/themoviedbapi/tools/ApiUrl.java | 135 +++++++++++------- .../themoviedbapi/tools/ApiUrlTest.java | 8 +- 16 files changed, 155 insertions(+), 116 deletions(-) diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbAccount.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbAccount.java index 19545d7dd..9349972d8 100644 --- a/src/main/java/com/omertron/themoviedbapi/methods/TmdbAccount.java +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbAccount.java @@ -91,7 +91,7 @@ public class TmdbAccount extends AbstractMethod { parameters.add(Param.SESSION, sessionId); parameters.add(Param.ID, accountId); - URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(MethodSub.LISTS).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).subMethod(MethodSub.LISTS).buildUrl(parameters); return processWrapperList(getTypeReference(UserList.class), url, "user list"); } @@ -108,7 +108,7 @@ public class TmdbAccount extends AbstractMethod { parameters.add(Param.SESSION, sessionId); parameters.add(Param.ID, accountId); - URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(MethodSub.FAVORITE_MOVIES).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).subMethod(MethodSub.FAVORITE_MOVIES).buildUrl(parameters); return processWrapperList(getTypeReference(MovieBasic.class), url, "favorite movies"); } @@ -125,7 +125,7 @@ public class TmdbAccount extends AbstractMethod { parameters.add(Param.SESSION, sessionId); parameters.add(Param.ID, accountId); - URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(MethodSub.FAVORITE_TV).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).subMethod(MethodSub.FAVORITE_TV).buildUrl(parameters); return processWrapperList(getTypeReference(TVBasic.class), url, "favorite TV shows"); } @@ -151,7 +151,7 @@ public class TmdbAccount extends AbstractMethod { .add(PostBody.FAVORITE, setFavorite) .build(); - URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(MethodSub.FAVORITE).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).subMethod(MethodSub.FAVORITE).buildUrl(parameters); String webpage = httpTools.postRequest(url, jsonBody); try { @@ -180,7 +180,7 @@ public class TmdbAccount extends AbstractMethod { parameters.add(Param.SORT_BY, sortBy); parameters.add(Param.LANGUAGE, language); - URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(MethodSub.RATED_MOVIES).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).subMethod(MethodSub.RATED_MOVIES).buildUrl(parameters); return processWrapperList(getTypeReference(MovieBasic.class), url, "rated movies"); } @@ -203,7 +203,7 @@ public class TmdbAccount extends AbstractMethod { parameters.add(Param.SORT_BY, sortBy); parameters.add(Param.LANGUAGE, language); - URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(MethodSub.RATED_TV).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).subMethod(MethodSub.RATED_TV).buildUrl(parameters); return processWrapperList(getTypeReference(TVBasic.class), url, "rated TV shows"); } @@ -226,7 +226,7 @@ public class TmdbAccount extends AbstractMethod { parameters.add(Param.SORT_BY, sortBy); parameters.add(Param.LANGUAGE, language); - URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(MethodSub.WATCHLIST_MOVIES).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).subMethod(MethodSub.WATCHLIST_MOVIES).buildUrl(parameters); return processWrapperList(getTypeReference(MovieBasic.class), url, "movie watch list"); } @@ -249,7 +249,7 @@ public class TmdbAccount extends AbstractMethod { parameters.add(Param.SORT_BY, sortBy); parameters.add(Param.LANGUAGE, language); - URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(MethodSub.WATCHLIST_TV).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).subMethod(MethodSub.WATCHLIST_TV).buildUrl(parameters); return processWrapperList(getTypeReference(TVBasic.class), url, "TV watch list"); } @@ -275,7 +275,7 @@ public class TmdbAccount extends AbstractMethod { .add(PostBody.WATCHLIST, addToWatchlist) .build(); - URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(MethodSub.WATCHLIST).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).subMethod(MethodSub.WATCHLIST).buildUrl(parameters); String webpage = httpTools.postRequest(url, jsonBody); try { @@ -305,7 +305,7 @@ public class TmdbAccount extends AbstractMethod { parameters.add(Param.SORT_BY, sortBy.getPropertyString()); } - URL url = new ApiUrl(apiKey, MethodBase.GUEST_SESSION).setSubMethod(MethodSub.RATED_MOVIES_GUEST).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.GUEST_SESSION).subMethod(MethodSub.RATED_MOVIES_GUEST).buildUrl(parameters); return processWrapperList(getTypeReference(MovieBasic.class), url, "Guest Session Movies"); } } diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbAuthentication.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbAuthentication.java index 30413cf93..b1dcf3120 100644 --- a/src/main/java/com/omertron/themoviedbapi/methods/TmdbAuthentication.java +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbAuthentication.java @@ -63,7 +63,7 @@ public class TmdbAuthentication extends AbstractMethod { */ public TokenAuthorisation getAuthorisationToken() throws MovieDbException { TmdbParameters parameters = new TmdbParameters(); - URL url = new ApiUrl(apiKey, MethodBase.AUTH).setSubMethod(MethodSub.TOKEN_NEW).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.AUTH).subMethod(MethodSub.TOKEN_NEW).buildUrl(parameters); String webpage = httpTools.getRequest(url); @@ -91,7 +91,7 @@ public class TmdbAuthentication extends AbstractMethod { } parameters.add(Param.TOKEN, token.getRequestToken()); - URL url = new ApiUrl(apiKey, MethodBase.AUTH).setSubMethod(MethodSub.SESSION_NEW).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.AUTH).subMethod(MethodSub.SESSION_NEW).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -123,7 +123,7 @@ public class TmdbAuthentication extends AbstractMethod { parameters.add(Param.USERNAME, username); parameters.add(Param.PASSWORD, password); - URL url = new ApiUrl(apiKey, MethodBase.AUTH).setSubMethod(MethodSub.TOKEN_VALIDATE).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.AUTH).subMethod(MethodSub.TOKEN_VALIDATE).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -149,7 +149,7 @@ public class TmdbAuthentication extends AbstractMethod { * @throws MovieDbException */ public TokenSession getGuestSessionToken() throws MovieDbException { - URL url = new ApiUrl(apiKey, MethodBase.AUTH).setSubMethod(MethodSub.GUEST_SESSION).buildUrl(); + URL url = new ApiUrl(apiKey, MethodBase.AUTH).subMethod(MethodSub.GUEST_SESSION).buildUrl(); String webpage = httpTools.getRequest(url); try { diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbCertifications.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbCertifications.java index 31596d0ea..e5bb9cc21 100644 --- a/src/main/java/com/omertron/themoviedbapi/methods/TmdbCertifications.java +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbCertifications.java @@ -58,7 +58,7 @@ public class TmdbCertifications extends AbstractMethod { * @throws MovieDbException */ public TmdbResultsMap> getMoviesCertification() throws MovieDbException { - URL url = new ApiUrl(apiKey, MethodBase.CERTIFICATION).setSubMethod(MethodSub.MOVIE_LIST).buildUrl(); + URL url = new ApiUrl(apiKey, MethodBase.CERTIFICATION).subMethod(MethodSub.MOVIE_LIST).buildUrl(); String webpage = httpTools.getRequest(url); try { @@ -78,7 +78,7 @@ public class TmdbCertifications extends AbstractMethod { * @throws MovieDbException */ public TmdbResultsMap> getTvCertification() throws MovieDbException { - URL url = new ApiUrl(apiKey, MethodBase.CERTIFICATION).setSubMethod(MethodSub.TV_LIST).buildUrl(); + URL url = new ApiUrl(apiKey, MethodBase.CERTIFICATION).subMethod(MethodSub.TV_LIST).buildUrl(); String webpage = httpTools.getRequest(url); try { diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbChanges.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbChanges.java index 81b39fc78..dcc7f7bf1 100644 --- a/src/main/java/com/omertron/themoviedbapi/methods/TmdbChanges.java +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbChanges.java @@ -68,7 +68,7 @@ public class TmdbChanges extends AbstractMethod { params.add(Param.START_DATE, startDate); params.add(Param.END_DATE, endDate); - URL url = new ApiUrl(apiKey, method).setSubMethod(MethodSub.CHANGES).buildUrl(params); + URL url = new ApiUrl(apiKey, method).subMethod(MethodSub.CHANGES).buildUrl(params); TypeReference tr = new TypeReference>() { }; return processWrapperList(tr, url, "changes"); diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbCollections.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbCollections.java index baac02a1f..064ae5699 100644 --- a/src/main/java/com/omertron/themoviedbapi/methods/TmdbCollections.java +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbCollections.java @@ -92,7 +92,7 @@ public class TmdbCollections extends AbstractMethod { parameters.add(Param.ID, collectionId); parameters.add(Param.LANGUAGE, language); - URL url = new ApiUrl(apiKey, MethodBase.COLLECTION).setSubMethod(MethodSub.IMAGES).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.COLLECTION).subMethod(MethodSub.IMAGES).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbCompanies.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbCompanies.java index 427762404..ac82e8e7d 100644 --- a/src/main/java/com/omertron/themoviedbapi/methods/TmdbCompanies.java +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbCompanies.java @@ -88,7 +88,7 @@ public class TmdbCompanies extends AbstractMethod { parameters.add(Param.LANGUAGE, language); parameters.add(Param.PAGE, page); - URL url = new ApiUrl(apiKey, MethodBase.COMPANY).setSubMethod(MethodSub.MOVIES).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.COMPANY).subMethod(MethodSub.MOVIES).buildUrl(parameters); String webpage = httpTools.getRequest(url); return processWrapperList(getTypeReference(MovieBasic.class), url, webpage); } diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbConfiguration.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbConfiguration.java index 469af49bd..7ea039da5 100644 --- a/src/main/java/com/omertron/themoviedbapi/methods/TmdbConfiguration.java +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbConfiguration.java @@ -90,7 +90,7 @@ public class TmdbConfiguration extends AbstractMethod { * @throws MovieDbException */ public TmdbResultsList getJobs() throws MovieDbException { - URL url = new ApiUrl(apiKey, MethodBase.JOB).setSubMethod(MethodSub.LIST).buildUrl(); + URL url = new ApiUrl(apiKey, MethodBase.JOB).subMethod(MethodSub.LIST).buildUrl(); String webpage = httpTools.getRequest(url); try { @@ -104,7 +104,7 @@ public class TmdbConfiguration extends AbstractMethod { } public Map> getTimezones() throws MovieDbException { - URL url = new ApiUrl(apiKey, MethodBase.TIMEZONES).setSubMethod(MethodSub.LIST).buildUrl(); + URL url = new ApiUrl(apiKey, MethodBase.TIMEZONES).subMethod(MethodSub.LIST).buildUrl(); String webpage = httpTools.getRequest(url); List>> tzList; diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbDiscover.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbDiscover.java index 48d1e8209..d7bea8965 100644 --- a/src/main/java/com/omertron/themoviedbapi/methods/TmdbDiscover.java +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbDiscover.java @@ -55,7 +55,7 @@ public class TmdbDiscover extends AbstractMethod { * @throws MovieDbException */ public List getDiscoverMovies(Discover discover) throws MovieDbException { - URL url = new ApiUrl(apiKey, MethodBase.DISCOVER).setSubMethod(MethodSub.MOVIE).buildUrl(discover.getParams()); + URL url = new ApiUrl(apiKey, MethodBase.DISCOVER).subMethod(MethodSub.MOVIE).buildUrl(discover.getParams()); String webpage = httpTools.getRequest(url); return processWrapperList(getTypeReference(MovieBasic.class), url, webpage); } @@ -68,7 +68,7 @@ public class TmdbDiscover extends AbstractMethod { * @throws MovieDbException */ public List getDiscoverTV(Discover discover) throws MovieDbException { - URL url = new ApiUrl(apiKey, MethodBase.DISCOVER).setSubMethod(MethodSub.TV).buildUrl(discover.getParams()); + URL url = new ApiUrl(apiKey, MethodBase.DISCOVER).subMethod(MethodSub.TV).buildUrl(discover.getParams()); String webpage = httpTools.getRequest(url); return processWrapperList(getTypeReference(TVBasic.class), url, webpage); } diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbGenres.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbGenres.java index 4c860b5df..d3bdacd90 100644 --- a/src/main/java/com/omertron/themoviedbapi/methods/TmdbGenres.java +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbGenres.java @@ -85,7 +85,7 @@ public class TmdbGenres extends AbstractMethod { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.LANGUAGE, language); - URL url = new ApiUrl(apiKey, MethodBase.GENRE).setSubMethod(sub).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.GENRE).subMethod(sub).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -119,7 +119,7 @@ public class TmdbGenres extends AbstractMethod { parameters.add(Param.INCLUDE_ALL_MOVIES, includeAllMovies); parameters.add(Param.INCLUDE_ADULT, includeAdult); - URL url = new ApiUrl(apiKey, MethodBase.GENRE).setSubMethod(MethodSub.MOVIES).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.GENRE).subMethod(MethodSub.MOVIES).buildUrl(parameters); String webpage = httpTools.getRequest(url); return processWrapperList(getTypeReference(MovieBasic.class), url, webpage); diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbKeywords.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbKeywords.java index 5b04f11dc..fb6c1f8ac 100644 --- a/src/main/java/com/omertron/themoviedbapi/methods/TmdbKeywords.java +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbKeywords.java @@ -88,7 +88,7 @@ public class TmdbKeywords extends AbstractMethod { parameters.add(Param.LANGUAGE, language); parameters.add(Param.PAGE, page); - URL url = new ApiUrl(apiKey, MethodBase.KEYWORD).setSubMethod(MethodSub.MOVIES).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.KEYWORD).subMethod(MethodSub.MOVIES).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbLists.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbLists.java index 79ba8424b..a087317a0 100644 --- a/src/main/java/com/omertron/themoviedbapi/methods/TmdbLists.java +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbLists.java @@ -91,7 +91,7 @@ public class TmdbLists extends AbstractMethod { parameters.add(Param.ID, listId); parameters.add(Param.MOVIE_ID, mediaId); - URL url = new ApiUrl(apiKey, MethodBase.LIST).setSubMethod(MethodSub.ITEM_STATUS).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.LIST).subMethod(MethodSub.ITEM_STATUS).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -173,7 +173,7 @@ public class TmdbLists extends AbstractMethod { .add(PostBody.MEDIA_ID, movieId) .build(); - URL url = new ApiUrl(apiKey, MethodBase.LIST).setSubMethod(operation).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.LIST).subMethod(operation).buildUrl(parameters); String webpage = httpTools.postRequest(url, jsonBody); try { @@ -232,7 +232,7 @@ public class TmdbLists extends AbstractMethod { parameters.add(Param.ID, listId); parameters.add(Param.CONFIRM, confirm); - URL url = new ApiUrl(apiKey, MethodBase.LIST).setSubMethod(MethodSub.CLEAR).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.LIST).subMethod(MethodSub.CLEAR).buildUrl(parameters); String webpage = httpTools.postRequest(url, ""); try { diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbMovies.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbMovies.java index 05278700a..10912ab0e 100644 --- a/src/main/java/com/omertron/themoviedbapi/methods/TmdbMovies.java +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbMovies.java @@ -156,7 +156,7 @@ public class TmdbMovies extends AbstractMethod { parameters.add(Param.ID, movieId); parameters.add(Param.SESSION, sessionId); - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.ACCOUNT_STATES).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).subMethod(MethodSub.ACCOUNT_STATES).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -181,7 +181,7 @@ public class TmdbMovies extends AbstractMethod { parameters.add(Param.COUNTRY, country); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.ALT_TITLES).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).subMethod(MethodSub.ALT_TITLES).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { WrapperAlternativeTitles wrapper = MAPPER.readValue(webpage, WrapperAlternativeTitles.class); @@ -206,7 +206,7 @@ public class TmdbMovies extends AbstractMethod { parameters.add(Param.ID, movieId); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.CREDITS).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).subMethod(MethodSub.CREDITS).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { return MAPPER.readValue(webpage, MediaCreditList.class); @@ -230,7 +230,7 @@ public class TmdbMovies extends AbstractMethod { parameters.add(Param.LANGUAGE, language); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.IMAGES).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).subMethod(MethodSub.IMAGES).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -258,7 +258,7 @@ public class TmdbMovies extends AbstractMethod { parameters.add(Param.ID, movieId); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.KEYWORDS).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).subMethod(MethodSub.KEYWORDS).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -286,7 +286,7 @@ public class TmdbMovies extends AbstractMethod { parameters.add(Param.LANGUAGE, language); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.RELEASES).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).subMethod(MethodSub.RELEASES).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -316,7 +316,7 @@ public class TmdbMovies extends AbstractMethod { parameters.add(Param.LANGUAGE, language); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.VIDEOS).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).subMethod(MethodSub.VIDEOS).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -342,7 +342,7 @@ public class TmdbMovies extends AbstractMethod { parameters.add(Param.ID, movieId); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.TRANSLATIONS).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).subMethod(MethodSub.TRANSLATIONS).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -376,7 +376,7 @@ public class TmdbMovies extends AbstractMethod { parameters.add(Param.PAGE, page); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.SIMILAR_MOVIES).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).subMethod(MethodSub.SIMILAR_MOVIES).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -406,7 +406,7 @@ public class TmdbMovies extends AbstractMethod { parameters.add(Param.LANGUAGE, language); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.REVIEWS).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).subMethod(MethodSub.REVIEWS).buildUrl(parameters); WrapperGenericList wrapper = processWrapper(getTypeReference(Review.class), url, "review"); TmdbResultsList results = new TmdbResultsList(null); results.getResults().addAll(wrapper.getResults()); @@ -431,7 +431,7 @@ public class TmdbMovies extends AbstractMethod { parameters.add(Param.PAGE, page); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.LISTS).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).subMethod(MethodSub.LISTS).buildUrl(parameters); List wrapper = processWrapperList(getTypeReference(UserList.class), url, "movie lists"); TmdbResultsList results = new TmdbResultsList(null); results.getResults().addAll(wrapper); @@ -461,7 +461,7 @@ public class TmdbMovies extends AbstractMethod { parameters.add(Param.START_DATE, startDate); parameters.add(Param.END_DATE, endDate); - URL url = new ApiUrl(apiKey, MethodBase.PERSON).setSubMethod(MethodSub.CHANGES).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.PERSON).subMethod(MethodSub.CHANGES).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -493,7 +493,7 @@ public class TmdbMovies extends AbstractMethod { parameters.add(Param.SESSION, sessionId); parameters.add(Param.GUEST_SESSION_ID, guestSessionId); - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.RATING).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).subMethod(MethodSub.RATING).buildUrl(parameters); String jsonBody = new PostTools() .add(PostBody.VALUE, rating) @@ -514,7 +514,7 @@ public class TmdbMovies extends AbstractMethod { * @throws MovieDbException */ public MovieDb getLatestMovie() throws MovieDbException { - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.LATEST).buildUrl(); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).subMethod(MethodSub.LATEST).buildUrl(); String webpage = httpTools.getRequest(url); try { @@ -541,7 +541,7 @@ public class TmdbMovies extends AbstractMethod { parameters.add(Param.LANGUAGE, language); parameters.add(Param.PAGE, page); - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.UPCOMING).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).subMethod(MethodSub.UPCOMING).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -570,7 +570,7 @@ public class TmdbMovies extends AbstractMethod { parameters.add(Param.LANGUAGE, language); parameters.add(Param.PAGE, page); - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.NOW_PLAYING).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).subMethod(MethodSub.NOW_PLAYING).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -598,7 +598,7 @@ public class TmdbMovies extends AbstractMethod { parameters.add(Param.LANGUAGE, language); parameters.add(Param.PAGE, page); - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.POPULAR).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).subMethod(MethodSub.POPULAR).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -626,7 +626,7 @@ public class TmdbMovies extends AbstractMethod { parameters.add(Param.LANGUAGE, language); parameters.add(Param.PAGE, page); - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.TOP_RATED).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).subMethod(MethodSub.TOP_RATED).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbPeople.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbPeople.java index 4f0817681..55543f464 100644 --- a/src/main/java/com/omertron/themoviedbapi/methods/TmdbPeople.java +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbPeople.java @@ -97,7 +97,7 @@ public class TmdbPeople extends AbstractMethod { parameters.add(Param.LANGUAGE, language); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, MethodBase.PERSON).setSubMethod(MethodSub.MOVIE_CREDITS).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.PERSON).subMethod(MethodSub.MOVIE_CREDITS).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -128,7 +128,7 @@ public class TmdbPeople extends AbstractMethod { parameters.add(Param.LANGUAGE, language); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, MethodBase.PERSON).setSubMethod(MethodSub.TV_CREDITS).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.PERSON).subMethod(MethodSub.TV_CREDITS).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -159,7 +159,7 @@ public class TmdbPeople extends AbstractMethod { parameters.add(Param.LANGUAGE, language); parameters.add(Param.APPEND, appendToResponse); - URL url = new ApiUrl(apiKey, MethodBase.PERSON).setSubMethod(MethodSub.COMBINED_CREDITS).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.PERSON).subMethod(MethodSub.COMBINED_CREDITS).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -180,7 +180,7 @@ public class TmdbPeople extends AbstractMethod { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.ID, personId); - URL url = new ApiUrl(apiKey, MethodBase.PERSON).setSubMethod(MethodSub.EXTERNAL_IDS).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.PERSON).subMethod(MethodSub.EXTERNAL_IDS).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -201,7 +201,7 @@ public class TmdbPeople extends AbstractMethod { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.ID, personId); - URL url = new ApiUrl(apiKey, MethodBase.PERSON).setSubMethod(MethodSub.IMAGES).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.PERSON).subMethod(MethodSub.IMAGES).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -232,7 +232,7 @@ public class TmdbPeople extends AbstractMethod { parameters.add(Param.PAGE, page); parameters.add(Param.LANGUAGE, language); - URL url = new ApiUrl(apiKey, MethodBase.PERSON).setSubMethod(MethodSub.TAGGED_IMAGES).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.PERSON).subMethod(MethodSub.TAGGED_IMAGES).buildUrl(parameters); WrapperGenericList wrapper; @@ -274,7 +274,7 @@ public class TmdbPeople extends AbstractMethod { parameters.add(Param.START_DATE, startDate); parameters.add(Param.END_DATE, endDate); - URL url = new ApiUrl(apiKey, MethodBase.PERSON).setSubMethod(MethodSub.CHANGES).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.PERSON).subMethod(MethodSub.CHANGES).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -297,7 +297,7 @@ public class TmdbPeople extends AbstractMethod { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.PAGE, page); - URL url = new ApiUrl(apiKey, MethodBase.PERSON).setSubMethod(MethodSub.POPULAR).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.PERSON).subMethod(MethodSub.POPULAR).buildUrl(parameters); String webpage = httpTools.getRequest(url); WrapperGenericList wrapper; @@ -320,7 +320,7 @@ public class TmdbPeople extends AbstractMethod { * @throws MovieDbException */ public Person getPersonLatest() throws MovieDbException { - URL url = new ApiUrl(apiKey, MethodBase.PERSON).setSubMethod(MethodSub.LATEST).buildUrl(); + URL url = new ApiUrl(apiKey, MethodBase.PERSON).subMethod(MethodSub.LATEST).buildUrl(); String webpage = httpTools.getRequest(url); try { diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbSearch.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbSearch.java index 19f9d8c0d..42a00798f 100644 --- a/src/main/java/com/omertron/themoviedbapi/methods/TmdbSearch.java +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbSearch.java @@ -78,7 +78,7 @@ public class TmdbSearch extends AbstractMethod { parameters.add(Param.QUERY, query); parameters.add(Param.PAGE, page); - URL url = new ApiUrl(apiKey, MethodBase.SEARCH).setSubMethod(MethodSub.COMPANY).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.SEARCH).subMethod(MethodSub.COMPANY).buildUrl(parameters); WrapperGenericList wrapper = processWrapper(getTypeReference(Company.class), url, "company"); TmdbResultsList results = new TmdbResultsList(wrapper.getResults()); results.copyWrapper(wrapper); @@ -100,7 +100,7 @@ public class TmdbSearch extends AbstractMethod { parameters.add(Param.PAGE, page); parameters.add(Param.LANGUAGE, language); - URL url = new ApiUrl(apiKey, MethodBase.SEARCH).setSubMethod(MethodSub.COLLECTION).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.SEARCH).subMethod(MethodSub.COLLECTION).buildUrl(parameters); WrapperGenericList wrapper = processWrapper(getTypeReference(Collection.class), url, "collection"); TmdbResultsList results = new TmdbResultsList(wrapper.getResults()); results.copyWrapper(wrapper); @@ -120,7 +120,7 @@ public class TmdbSearch extends AbstractMethod { parameters.add(Param.QUERY, query); parameters.add(Param.PAGE, page); - URL url = new ApiUrl(apiKey, MethodBase.SEARCH).setSubMethod(MethodSub.KEYWORD).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.SEARCH).subMethod(MethodSub.KEYWORD).buildUrl(parameters); WrapperGenericList wrapper = processWrapper(getTypeReference(Keyword.class), url, "keyword"); TmdbResultsList results = new TmdbResultsList(wrapper.getResults()); results.copyWrapper(wrapper); @@ -142,7 +142,7 @@ public class TmdbSearch extends AbstractMethod { parameters.add(Param.PAGE, page); parameters.add(Param.INCLUDE_ADULT, includeAdult); - URL url = new ApiUrl(apiKey, MethodBase.SEARCH).setSubMethod(MethodSub.LIST).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.SEARCH).subMethod(MethodSub.LIST).buildUrl(parameters); WrapperGenericList wrapper = processWrapper(getTypeReference(UserList.class), url, "list"); TmdbResultsList results = new TmdbResultsList(wrapper.getResults()); results.copyWrapper(wrapper); @@ -179,7 +179,7 @@ public class TmdbSearch extends AbstractMethod { if (searchType != null) { parameters.add(Param.SEARCH_TYPE, searchType.getPropertyString()); } - URL url = new ApiUrl(apiKey, MethodBase.SEARCH).setSubMethod(MethodSub.MOVIE).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.SEARCH).subMethod(MethodSub.MOVIE).buildUrl(parameters); WrapperGenericList wrapper = processWrapper(getTypeReference(MovieDb.class), url, "movie"); TmdbResultsList results = new TmdbResultsList(wrapper.getResults()); @@ -208,7 +208,7 @@ public class TmdbSearch extends AbstractMethod { parameters.add(Param.LANGUAGE, language); parameters.add(Param.ADULT, includeAdult); - URL url = new ApiUrl(apiKey, MethodBase.SEARCH).setSubMethod(MethodSub.MULTI).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.SEARCH).subMethod(MethodSub.MULTI).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -241,7 +241,7 @@ public class TmdbSearch extends AbstractMethod { if (searchType != null) { parameters.add(Param.SEARCH_TYPE, searchType.getPropertyString()); } - URL url = new ApiUrl(apiKey, MethodBase.SEARCH).setSubMethod(MethodSub.PERSON).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.SEARCH).subMethod(MethodSub.PERSON).buildUrl(parameters); WrapperGenericList wrapper = processWrapper(getTypeReference(PersonFind.class), url, "person"); TmdbResultsList results = new TmdbResultsList(wrapper.getResults()); @@ -269,7 +269,7 @@ public class TmdbSearch extends AbstractMethod { if (searchType != null) { parameters.add(Param.SEARCH_TYPE, searchType.getPropertyString()); } - URL url = new ApiUrl(apiKey, MethodBase.SEARCH).setSubMethod(MethodSub.MOVIE).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.SEARCH).subMethod(MethodSub.MOVIE).buildUrl(parameters); WrapperGenericList wrapper = processWrapper(getTypeReference(TVBasic.class), url, "TV Show"); TmdbResultsList results = new TmdbResultsList(wrapper.getResults()); diff --git a/src/main/java/com/omertron/themoviedbapi/tools/ApiUrl.java b/src/main/java/com/omertron/themoviedbapi/tools/ApiUrl.java index 6f483b7ac..f7f69ba59 100644 --- a/src/main/java/com/omertron/themoviedbapi/tools/ApiUrl.java +++ b/src/main/java/com/omertron/themoviedbapi/tools/ApiUrl.java @@ -40,7 +40,6 @@ public class ApiUrl { private static final Logger LOG = LoggerFactory.getLogger(ApiUrl.class); // TheMovieDbApi API Base URL private static final String TMDB_API_BASE = "http://api.themoviedb.org/3/"; -// private static final String TMDB_API_BASE = "http://private-639f-themoviedb.apiary-proxy.com/3/"; // Parameter configuration private static final String DELIMITER_FIRST = "?"; private static final String DELIMITER_SUBSEQUENT = "&"; @@ -66,7 +65,7 @@ public class ApiUrl { * @param submethod * @return */ - public ApiUrl setSubMethod(MethodSub submethod) { + public ApiUrl subMethod(MethodSub submethod) { this.submethod = submethod.getValue(); return this; } @@ -95,48 +94,95 @@ public class ApiUrl { // Get the start of the URL urlString.append(method); - // We have either a queury, or a direct request + // We have either a queury, or a ID request if (params.has(Param.QUERY)) { - // Append the suffix of the API URL - if (StringUtils.isNotBlank(submethod)) { - urlString.append("/").append(submethod); - } - - // Append the key information - urlString.append(DELIMITER_FIRST) - .append(Param.API_KEY.getValue()) - .append(apiKey); - - // Append the search term - urlString.append(DELIMITER_SUBSEQUENT); - urlString.append(Param.QUERY.getValue()); - - String query = (String) params.get(Param.QUERY); - - try { - urlString.append(URLEncoder.encode(query, "UTF-8")); - } catch (UnsupportedEncodingException ex) { - LOG.trace("Unable to encode query: '{}' trying raw.", query, ex); - // If we can't encode it, try it raw - urlString.append(query); - } + urlString.append(queryProcessing(params)); } else { - // Append the ID if provided - if (params.has(Param.ID)) { - urlString.append("/").append(params.get(Param.ID)); - } - - if (StringUtils.isNotBlank(submethod)) { - urlString.append("/").append(submethod); - } - - // Append the key information - urlString.append(DELIMITER_FIRST) - .append(Param.API_KEY.getValue()) - .append(apiKey); + urlString.append(idProcessing(params)); } - // Append remaining parameters + urlString.append(otherProcessing(params)); + + try { + LOG.trace("URL: {}", urlString.toString()); + return new URL(urlString.toString()); + } catch (MalformedURLException ex) { + LOG.warn("Failed to create URL {} - {}", urlString.toString(), ex.getMessage()); + return null; + } + } + + /** + * Create the query based URL portion + * + * @param params + * @return + */ + private StringBuilder queryProcessing(TmdbParameters params) { + StringBuilder urlString = new StringBuilder(); + + // Append the suffix of the API URL + if (StringUtils.isNotBlank(submethod)) { + urlString.append("/").append(submethod); + } + + // Append the key information + urlString.append(DELIMITER_FIRST) + .append(Param.API_KEY.getValue()) + .append(apiKey); + + // Append the search term + urlString.append(DELIMITER_SUBSEQUENT); + urlString.append(Param.QUERY.getValue()); + + String query = (String) params.get(Param.QUERY); + + try { + urlString.append(URLEncoder.encode(query, "UTF-8")); + } catch (UnsupportedEncodingException ex) { + LOG.trace("Unable to encode query: '{}' trying raw.", query, ex); + // If we can't encode it, try it raw + urlString.append(query); + } + + return urlString; + } + + /** + * Create the ID based URL portion + * + * @param params + * @return + */ + private StringBuilder idProcessing(final TmdbParameters params) { + StringBuilder urlString = new StringBuilder(); + + // Append the ID + if (params.has(Param.ID)) { + urlString.append("/").append(params.get(Param.ID)); + } + + if (StringUtils.isNotBlank(submethod)) { + urlString.append("/").append(submethod); + } + + // Append the key information + urlString.append(DELIMITER_FIRST) + .append(Param.API_KEY.getValue()) + .append(apiKey); + + return urlString; + } + + /** + * Create a string of the remaining parameters + * + * @param params + * @return + */ + private StringBuilder otherProcessing(final TmdbParameters params) { + StringBuilder urlString = new StringBuilder(); + for (Map.Entry argEntry : params.getEntries()) { // Skip the ID an QUERY params if (argEntry.getKey() == Param.ID || argEntry.getKey() == Param.QUERY) { @@ -147,13 +193,6 @@ public class ApiUrl { .append(argEntry.getKey().getValue()) .append(argEntry.getValue()); } - - try { - LOG.trace("URL: {}", urlString.toString()); - return new URL(urlString.toString()); - } catch (MalformedURLException ex) { - LOG.warn("Failed to create URL {} - {}", urlString.toString(), ex.getMessage()); - return null; - } + return urlString; } } diff --git a/src/test/java/com/omertron/themoviedbapi/tools/ApiUrlTest.java b/src/test/java/com/omertron/themoviedbapi/tools/ApiUrlTest.java index fb9979244..ff6015e18 100644 --- a/src/test/java/com/omertron/themoviedbapi/tools/ApiUrlTest.java +++ b/src/test/java/com/omertron/themoviedbapi/tools/ApiUrlTest.java @@ -86,7 +86,7 @@ public class ApiUrlTest { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.QUERY, "query"); - URL result = new ApiUrl(APIKEY, MethodBase.MOVIE).setSubMethod(MethodSub.LATEST).buildUrl(parameters); + URL result = new ApiUrl(APIKEY, MethodBase.MOVIE).subMethod(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,7 +99,7 @@ public class ApiUrlTest { parameters.add(Param.PAGE, 1); parameters.add(Param.LANGUAGE, "lang"); - URL result = new ApiUrl(APIKEY, MethodBase.MOVIE).setSubMethod(MethodSub.LATEST).buildUrl(parameters); + URL result = new ApiUrl(APIKEY, MethodBase.MOVIE).subMethod(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()); } @@ -121,7 +121,7 @@ public class ApiUrlTest { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.ID, "ID"); - URL result = new ApiUrl(APIKEY, MethodBase.MOVIE).setSubMethod(MethodSub.LATEST).buildUrl(parameters); + URL result = new ApiUrl(APIKEY, MethodBase.MOVIE).subMethod(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,7 +134,7 @@ public class ApiUrlTest { parameters.add(Param.PAGE, 1); parameters.add(Param.LANGUAGE, "lang"); - URL result = new ApiUrl(APIKEY, MethodBase.MOVIE).setSubMethod(MethodSub.LATEST).buildUrl(parameters); + URL result = new ApiUrl(APIKEY, MethodBase.MOVIE).subMethod(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()); }