Skip to content

Commit

Permalink
add support to handle required and optional parameters better
Browse files Browse the repository at this point in the history
  • Loading branch information
c-eg committed Jan 3, 2024
1 parent 1820ca4 commit 30a0242
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 77 deletions.
24 changes: 12 additions & 12 deletions src/main/java/info/movito/themoviedbapi/TmdbAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class TmdbAccount extends AbstractTmdbApi {
public Account getAccount(SessionToken sessionToken) {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_ACCOUNT);

apiUrl.addParam(PARAM_SESSION, sessionToken);
apiUrl.addPathParam(PARAM_SESSION, sessionToken);

return mapJsonResult(apiUrl, Account.class);
}
Expand All @@ -51,7 +51,7 @@ public MovieListResultsPage getLists(SessionToken sessionToken, AccountID accoun
Integer page) {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_ACCOUNT, accountId, "lists");

apiUrl.addParam(PARAM_SESSION, sessionToken);
apiUrl.addPathParam(PARAM_SESSION, sessionToken);
apiUrl.addLanguage(language);
apiUrl.addPage(page);

Expand All @@ -64,7 +64,7 @@ public MovieListResultsPage getLists(SessionToken sessionToken, AccountID accoun
public MovieResultsPage getRatedMovies(SessionToken sessionToken, AccountID accountId, Integer page) {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_ACCOUNT, accountId, "rated/movies");

apiUrl.addParam(PARAM_SESSION, sessionToken);
apiUrl.addPathParam(PARAM_SESSION, sessionToken);
apiUrl.addPage(page);

return mapJsonResult(apiUrl, MovieResultsPage.class);
Expand All @@ -76,7 +76,7 @@ public MovieResultsPage getRatedMovies(SessionToken sessionToken, AccountID acco
public TvResultsPage getRatedTvSeries(SessionToken sessionToken, AccountID accountId, Integer page) {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_ACCOUNT, accountId, "rated/tv");

apiUrl.addParam(PARAM_SESSION, sessionToken);
apiUrl.addPathParam(PARAM_SESSION, sessionToken);
apiUrl.addPage(page);

return mapJsonResult(apiUrl, TvResultsPage.class);
Expand All @@ -88,7 +88,7 @@ public TvResultsPage getRatedTvSeries(SessionToken sessionToken, AccountID accou
public TvEpisodesResultsPage getRatedEpisodes(SessionToken sessionToken, AccountID accountId, Integer page) {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_ACCOUNT, accountId, "rated/tv/episodes");

apiUrl.addParam(PARAM_SESSION, sessionToken);
apiUrl.addPathParam(PARAM_SESSION, sessionToken);
apiUrl.addPage(page);

return mapJsonResult(apiUrl, TvEpisodesResultsPage.class);
Expand Down Expand Up @@ -128,7 +128,7 @@ public boolean postTvExpisodeRating(SessionToken sessionToken, Integer seriesId,
}

private boolean postRatingInternal(SessionToken sessionToken, Integer rating, ApiUrl apiUrl) {
apiUrl.addParam(PARAM_SESSION, sessionToken);
apiUrl.addPathParam(PARAM_SESSION, sessionToken);

if (rating < 0 || rating > 10) {
throw new MovieDbException("rating out of range");
Expand All @@ -144,7 +144,7 @@ private boolean postRatingInternal(SessionToken sessionToken, Integer rating, Ap
*/
public MovieResultsPage getFavoriteMovies(SessionToken sessionToken, AccountID accountId) {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_ACCOUNT, accountId, "favorite/movies");
apiUrl.addParam(PARAM_SESSION, sessionToken);
apiUrl.addPathParam(PARAM_SESSION, sessionToken);

return mapJsonResult(apiUrl, MovieResultsPage.class);
}
Expand All @@ -155,7 +155,7 @@ public MovieResultsPage getFavoriteMovies(SessionToken sessionToken, AccountID a
public TvResultsPage getFavoriteSeries(SessionToken sessionToken, AccountID accountId, Integer page) {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_ACCOUNT, accountId, "favorite/tv");

apiUrl.addParam(PARAM_SESSION, sessionToken);
apiUrl.addPathParam(PARAM_SESSION, sessionToken);
apiUrl.addPage(page);

return mapJsonResult(apiUrl, TvResultsPage.class);
Expand All @@ -181,7 +181,7 @@ private ResponseStatus changeFavoriteStatus(SessionToken sessionToken, AccountID
MediaType mediaType, boolean isFavorite) {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_ACCOUNT, accountId, "favorite");

apiUrl.addParam(PARAM_SESSION, sessionToken);
apiUrl.addPathParam(PARAM_SESSION, sessionToken);

HashMap<String, Object> body = new HashMap<String, Object>();

Expand All @@ -202,7 +202,7 @@ private ResponseStatus changeFavoriteStatus(SessionToken sessionToken, AccountID
public MovieResultsPage getWatchListMovies(SessionToken sessionToken, AccountID accountId, Integer page) {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_ACCOUNT, accountId, "watchlist/movies");

apiUrl.addParam(PARAM_SESSION, sessionToken);
apiUrl.addPathParam(PARAM_SESSION, sessionToken);
apiUrl.addPage(page);

return mapJsonResult(apiUrl, MovieResultsPage.class);
Expand All @@ -215,7 +215,7 @@ public MovieResultsPage getWatchListMovies(SessionToken sessionToken, AccountID
*/
public TvResultsPage getWatchListSeries(SessionToken sessionToken, AccountID accountId, Integer page) {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_ACCOUNT, accountId, "watchlist/tv");
apiUrl.addParam(PARAM_SESSION, sessionToken);
apiUrl.addPathParam(PARAM_SESSION, sessionToken);

apiUrl.addPage(page);

Expand All @@ -242,7 +242,7 @@ private ResponseStatus modifyWatchList(SessionToken sessionToken, AccountID acco
MediaType mediaType, boolean isWatched) {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_ACCOUNT, accountId, "watchlist");

apiUrl.addParam(PARAM_SESSION, sessionToken);
apiUrl.addPathParam(PARAM_SESSION, sessionToken);

HashMap<String, Object> body = new HashMap<String, Object>();

Expand Down
20 changes: 9 additions & 11 deletions src/main/java/info/movito/themoviedbapi/TmdbApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import info.movito.themoviedbapi.tools.RequestType;
import info.movito.themoviedbapi.tools.UrlReader;
import info.movito.themoviedbapi.tools.WebBrowser;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.net.URL;
import java.util.List;

/**
Expand All @@ -19,6 +21,7 @@
* @author Holger Brandl.
*/
public class TmdbApi {
@Getter
private final String apiKey;

private final TmdbConfiguration tmdbConfig;
Expand All @@ -32,7 +35,7 @@ public class TmdbApi {
* Automatically retry after indicated amount of seconds if we hit the request limit. See the
* <a href="https://developer.themoviedb.org/docs/rate-limiting">documentation</a> for details.
*/
private boolean autoRetry = true;
private final boolean autoRetry;

/**
* Constructor.
Expand Down Expand Up @@ -76,21 +79,20 @@ public TmdbApi(String apiKey, UrlReader urlReader, boolean autoRetry) {
* @param jsonBody can be null
*/
public String requestWebPage(ApiUrl apiUrl, String jsonBody, RequestType requestType) {

assert StringUtils.isNotBlank(apiKey);
apiUrl.addParam(AbstractTmdbApi.PARAM_API_KEY, getApiKey());
apiUrl.addPathParam(AbstractTmdbApi.PARAM_API_KEY, getApiKey());

return requestWebPageInternal(apiUrl, jsonBody, requestType);
return requestWebPageInternal(apiUrl.buildUrl(), jsonBody, requestType);
}

private String requestWebPageInternal(ApiUrl apiUrl, String jsonBody, RequestType requestType) {
private String requestWebPageInternal(URL url, String jsonBody, RequestType requestType) {
try {
return urlReader.request(apiUrl.buildUrl(), jsonBody, requestType);
return urlReader.request(url, jsonBody, requestType);
}
catch (RequestCountLimitException rcle) {
if (autoRetry) {
Utils.sleep(rcle.getRetryAfter() * 1000);
return requestWebPageInternal(apiUrl, jsonBody, requestType);
return requestWebPageInternal(url, jsonBody, requestType);
}
else {
// just return the orignal json response if autoRetry is disabled. This will cause a ResponseStatusException.
Expand All @@ -99,10 +101,6 @@ private String requestWebPageInternal(ApiUrl apiUrl, String jsonBody, RequestTyp
}
}

public String getApiKey() {
return apiKey;
}

public TmdbConfiguration getConfiguration() {
return tmdbConfig;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public TokenSession getSessionToken(TokenAuthorisation token) {
throw new MovieDbException("Authorisation token was not successful!");
}

apiUrl.addParam(PARAM_REQUEST_TOKEN, token.getRequestToken());
apiUrl.addPathParam(PARAM_REQUEST_TOKEN, token.getRequestToken());

return mapJsonResult(apiUrl, TokenSession.class);
}
Expand All @@ -66,9 +66,9 @@ public TokenSession getSessionToken(TokenAuthorisation token) {
public TokenAuthorisation getLoginToken(TokenAuthorisation token, String user, String pwd) {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_AUTH, "token/validate_with_login");

apiUrl.addParam(PARAM_REQUEST_TOKEN, token.getRequestToken());
apiUrl.addParam("username", user);
apiUrl.addParam("password", pwd);
apiUrl.addPathParam(PARAM_REQUEST_TOKEN, token.getRequestToken());
apiUrl.addPathParam("username", user);
apiUrl.addPathParam("password", pwd);

return mapJsonResult(apiUrl, TokenAuthorisation.class);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/info/movito/themoviedbapi/TmdbDiscover.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public MovieResultsPage getDiscover(Discover discover) {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_DISCOVER, "movie");

for (String key : discover.getParams().keySet()) {
apiUrl.addParam(key, discover.getParams().get(key));
apiUrl.addPathParam(key, discover.getParams().get(key));
}

return mapJsonResult(apiUrl, MovieResultsPage.class);
Expand Down Expand Up @@ -149,7 +149,7 @@ public TvResultsPage getDiscoverTV(Discover discover) {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_DISCOVER, "tv");

for (String key : discover.getParams().keySet()) {
apiUrl.addParam(key, discover.getParams().get(key));
apiUrl.addPathParam(key, discover.getParams().get(key));
}

return mapJsonResult(apiUrl, TvResultsPage.class);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/info/movito/themoviedbapi/TmdbFind.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class TmdbFind extends AbstractTmdbApi {
public FindResults find(String id, ExternalSource externalSource, String language) {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_FIND, id);

apiUrl.addParam("external_source", externalSource.toString());
apiUrl.addPathParam("external_source", externalSource.toString());
apiUrl.addLanguage(language);

return mapJsonResult(apiUrl, FindResults.class);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/info/movito/themoviedbapi/TmdbGenre.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public MovieResultsPage getGenreMovies(int genreId, String language, Integer pag

apiUrl.addPage(page);

apiUrl.addParam(PARAM_INCLUDE_ALL_MOVIES, includeAllMovies);
apiUrl.addPathParam(PARAM_INCLUDE_ALL_MOVIES, includeAllMovies);

return mapJsonResult(apiUrl, MovieResultsPage.class);
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/info/movito/themoviedbapi/TmdbLists.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public MovieList getList(String listId) {
public String createList(SessionToken sessionToken, String name, String description) {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_LIST);

apiUrl.addParam(TmdbAccount.PARAM_SESSION, sessionToken);
apiUrl.addPathParam(TmdbAccount.PARAM_SESSION, sessionToken);

HashMap<String, String> body = new HashMap<>();
body.put("name", StringUtils.trimToEmpty(name));
Expand All @@ -64,7 +64,7 @@ public String createList(SessionToken sessionToken, String name, String descript
public boolean isMovieOnList(String listId, Integer movieId) {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_LIST, listId, "item_status");

apiUrl.addParam("movie_id", movieId);
apiUrl.addPathParam("movie_id", movieId);

return mapJsonResult(apiUrl, ListItemStatus.class).isItemPresent();
}
Expand All @@ -91,7 +91,7 @@ private ResponseStatus modifyMovieList(SessionToken sessionToken, String listId,
String operation) {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_LIST, listId, operation);

apiUrl.addParam(TmdbAccount.PARAM_SESSION, sessionToken);
apiUrl.addPathParam(TmdbAccount.PARAM_SESSION, sessionToken);

String jsonBody = Utils.convertToJson(jsonMapper, Collections.singletonMap("media_id", movieId + ""));

Expand All @@ -104,7 +104,7 @@ private ResponseStatus modifyMovieList(SessionToken sessionToken, String listId,
public ResponseStatus deleteMovieList(SessionToken sessionToken, String listId) {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_LIST, listId);

apiUrl.addParam(TmdbAccount.PARAM_SESSION, sessionToken);
apiUrl.addPathParam(TmdbAccount.PARAM_SESSION, sessionToken);

return mapJsonResult(apiUrl, ResponseStatus.class, null, RequestType.DELETE);
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/info/movito/themoviedbapi/TmdbMovies.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public List<AlternativeTitle> getAlternativeTitles(int movieId, String country)
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_MOVIE, movieId, MovieMethod.alternative_titles);

if (StringUtils.isNotBlank(country)) {
apiUrl.addParam(PARAM_COUNTRY, country);
apiUrl.addPathParam(PARAM_COUNTRY, country);
}

return mapJsonResult(apiUrl, MoviesAlternativeTitles.class).getTitles();
Expand Down Expand Up @@ -186,7 +186,7 @@ public TmdbAccount.MovieListResultsPage getListsContaining(int movieId, SessionT
Integer page) {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_MOVIE, movieId, MovieMethod.lists);

apiUrl.addParam(PARAM_SESSION, sessionToken);
apiUrl.addPathParam(PARAM_SESSION, sessionToken);

apiUrl.addLanguage(language);

Expand Down Expand Up @@ -215,11 +215,11 @@ public ChangesItems getChanges(int movieId, String startDate, String endDate) {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_MOVIE, movieId, MovieMethod.changes);

if (StringUtils.isNotBlank(startDate)) {
apiUrl.addParam(PARAM_START_DATE, startDate);
apiUrl.addPathParam(PARAM_START_DATE, startDate);
}

if (StringUtils.isNotBlank(endDate)) {
apiUrl.addParam(PARAM_END_DATE, endDate);
apiUrl.addPathParam(PARAM_END_DATE, endDate);
}

return mapJsonResult(apiUrl, ChangesItems.class);
Expand Down Expand Up @@ -278,7 +278,7 @@ public MovieResultsPage getUpcoming(String language, Integer page, String region
apiUrl.addPage(page);

if (isNotBlank(region)) {
apiUrl.addParam(PARAM_REGION, region);
apiUrl.addPathParam(PARAM_REGION, region);
}

return mapJsonResult(apiUrl, MovieResultsPage.class);
Expand All @@ -297,7 +297,7 @@ public MovieResultsPage getNowPlayingMovies(String language, Integer page, Strin
apiUrl.addPage(page);

if (isNotBlank(region)) {
apiUrl.addParam(PARAM_REGION, region);
apiUrl.addPathParam(PARAM_REGION, region);
}

return mapJsonResult(apiUrl, MovieResultsPage.class);
Expand Down
Loading

0 comments on commit 30a0242

Please sign in to comment.