Skip to content

Commit

Permalink
Merge pull request #171 from c-eg/guess-sessions-api
Browse files Browse the repository at this point in the history
Guess sessions api
  • Loading branch information
c-eg authored Feb 13, 2024
2 parents 180abc8 + 753b6c7 commit 8cef995
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/main/java/info/movito/themoviedbapi/TmdbAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public MovieListResultsPage getLists(Integer accountId, String sessionId, Intege
* @return The rated movies of the user.
* @throws TmdbException If there was an error making the request or mapping the response.
*/
public RatedMovieResultsPage getRatedMovies(Integer accountId, String sessionId, String language, Integer page,
public RatedMovieResultsPage getRatedMovies(int accountId, String sessionId, String language, Integer page,
AccountSortBy sortBy) throws TmdbException {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_ACCOUNT, accountId, "rated/movies");
apiUrl.addQueryParam(PARAM_SESSION, sessionId);
Expand All @@ -239,7 +239,7 @@ public RatedMovieResultsPage getRatedMovies(Integer accountId, String sessionId,
* @return The rated tv series of the user.
* @throws TmdbException If there was an error making the request or mapping the response.
*/
public RatedTvSeriesResultsPage getRatedTvSeries(Integer accountId, String sessionId, String language, Integer page,
public RatedTvSeriesResultsPage getRatedTvSeries(int accountId, String sessionId, String language, Integer page,
AccountSortBy sortBy) throws TmdbException {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_ACCOUNT, accountId, "rated/tv");
apiUrl.addQueryParam(PARAM_SESSION, sessionId);
Expand All @@ -262,7 +262,7 @@ public RatedTvSeriesResultsPage getRatedTvSeries(Integer accountId, String sessi
* @return The rated tv episodes of the user.
* @throws TmdbException If there was an error making the request or mapping the response.
*/
public RatedTvEpisodeResultsPage getRatedTvEpisodes(Integer accountId, String sessionId, String language, Integer page,
public RatedTvEpisodeResultsPage getRatedTvEpisodes(int accountId, String sessionId, String language, Integer page,
AccountSortBy sortBy) throws TmdbException {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_ACCOUNT, accountId, "rated/tv/episodes");
apiUrl.addQueryParam(PARAM_SESSION, sessionId);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/info/movito/themoviedbapi/TmdbApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public TmdbGenre getGenre() {
return new TmdbGenre(this);
}

public TmdbGuestSessions getGuestSessions() {
return new TmdbGuestSessions(this);
}

public TmdbKeywords getKeywords() {
return new TmdbKeywords(this);
}
Expand Down
86 changes: 86 additions & 0 deletions src/main/java/info/movito/themoviedbapi/TmdbGuestSessions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package info.movito.themoviedbapi;

import info.movito.themoviedbapi.model.rated.RatedMovieResultsPage;
import info.movito.themoviedbapi.model.rated.RatedTvEpisodeResultsPage;
import info.movito.themoviedbapi.model.rated.RatedTvSeriesResultsPage;
import info.movito.themoviedbapi.tools.ApiUrl;
import info.movito.themoviedbapi.tools.TmdbException;
import info.movito.themoviedbapi.tools.sortby.AccountSortBy;

/**
* The movie database api for guest sessions. See the
* <a href="https://developer.themoviedb.org/reference/guest-session-rated-movies">documentation</a> for more info.
*/
public class TmdbGuestSessions extends AbstractTmdbApi {
protected static final String TMDB_METHOD_GUEST_SESSIONS = "guest_session";

/**
* Create a new TmdbGuestSessions instance to call the guest sessions related TMDb API methods.
*/
public TmdbGuestSessions(TmdbApi tmdbApi) {
super(tmdbApi);
}

/**
* <p>Get the rated movies for a guest session.</p>
* <p>See the <a href="https://developer.themoviedb.org/reference/guest-session-rated-movies">documentation</a> for more info.</p>
*
* @param guestSessionId The guest session id of the user.
* @param language optional - The language to use for the results.
* @param page optional - The page to return.
* @param sortBy optional - The sort order of the results.
* @return The rated movies of the user.
* @throws TmdbException If there was an error making the request or mapping the response.
*/
public RatedMovieResultsPage getRatedMovies(int guestSessionId, String language, Integer page,
AccountSortBy sortBy) throws TmdbException {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_GUEST_SESSIONS, guestSessionId, "rated/movies");
apiUrl.addLanguage(language);
apiUrl.addPage(page);
apiUrl.addSortBy(sortBy);

return mapJsonResult(apiUrl, RatedMovieResultsPage.class);
}

/**
* <p>Get the rated tv shows for a guest session.</p>
* <p>See the <a href="https://developer.themoviedb.org/reference/guest-session-rated-tv">documentation</a> for more info.</p>
*
* @param guestSessionId The guest session id of the user.
* @param language optional - The language to use for the results.
* @param page optional - The page to return.
* @param sortBy optional - The sort order of the results.
* @return The rated tv series of the user.
* @throws TmdbException If there was an error making the request or mapping the response.
*/
public RatedTvSeriesResultsPage getRatedTvSeries(int guestSessionId, String language, Integer page,
AccountSortBy sortBy) throws TmdbException {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_GUEST_SESSIONS, guestSessionId, "rated/tv");
apiUrl.addLanguage(language);
apiUrl.addPage(page);
apiUrl.addSortBy(sortBy);

return mapJsonResult(apiUrl, RatedTvSeriesResultsPage.class);
}

/**
* <p>Get the rated tv episodes for a guest session.</p>
* <p>See the <a href="https://developer.themoviedb.org/reference/guest-session-rated-tv-episodes">documentation</a> for more info.</p>
*
* @param guestSessionId The guest session id of the user.
* @param language optional - The language to use for the results.
* @param page optional - The page to return.
* @param sortBy optional - The sort order of the results.
* @return The rated tv episodes of the user.
* @throws TmdbException If there was an error making the request or mapping the response.
*/
public RatedTvEpisodeResultsPage getRatedTvEpisodes(int guestSessionId, String language, Integer page,
AccountSortBy sortBy) throws TmdbException {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_GUEST_SESSIONS, guestSessionId, "rated/tv/episodes");
apiUrl.addLanguage(language);
apiUrl.addPage(page);
apiUrl.addSortBy(sortBy);

return mapJsonResult(apiUrl, RatedTvEpisodeResultsPage.class);
}
}
12 changes: 6 additions & 6 deletions src/test/java/info/movito/themoviedbapi/TmdbAccountTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,11 @@ public void testGetLists() throws TmdbException, IOException {
}

/**
* Test {@link TmdbAccount#getRatedMovies(Integer, String, String, Integer, AccountSortBy)} with an expected result.
* Test {@link TmdbAccount#getRatedMovies(int, String, String, Integer, AccountSortBy)} with an expected result.
*/
@Test
public void testGetRatedMovies() throws TmdbException, IOException {
Integer accountId = 1234;
int accountId = 1234;
String sessionId = "testSessionId";
String language = "en";
Integer page = 1;
Expand All @@ -280,11 +280,11 @@ public void testGetRatedMovies() throws TmdbException, IOException {
}

/**
* Test {@link TmdbAccount#getRatedTvSeries(Integer, String, String, Integer, AccountSortBy)} with an expected result.
* Test {@link TmdbAccount#getRatedTvSeries(int, String, String, Integer, AccountSortBy)} with an expected result.
*/
@Test
public void testGetRatedTvSeries() throws TmdbException, IOException {
Integer accountId = 1234;
int accountId = 1234;
String sessionId = "testSessionId";
String language = "en";
Integer page = 1;
Expand All @@ -309,11 +309,11 @@ public void testGetRatedTvSeries() throws TmdbException, IOException {
}

/**
* Test {@link TmdbAccount#getRatedTvEpisodes(Integer, String, String, Integer, AccountSortBy)} with an expected result.
* Test {@link TmdbAccount#getRatedTvEpisodes(int, String, String, Integer, AccountSortBy)} with an expected result.
*/
@Test
public void testGetRatedTvEpisodes() throws TmdbException, IOException {
Integer accountId = 1234;
int accountId = 1234;
String sessionId = "testSessionId";
String language = "en";
Integer page = 1;
Expand Down
96 changes: 96 additions & 0 deletions src/test/java/info/movito/themoviedbapi/TmdbGuestSessionsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package info.movito.themoviedbapi;

import info.movito.themoviedbapi.model.rated.RatedMovie;
import info.movito.themoviedbapi.model.rated.RatedMovieResultsPage;
import info.movito.themoviedbapi.model.rated.RatedTvEpisode;
import info.movito.themoviedbapi.model.rated.RatedTvEpisodeResultsPage;
import info.movito.themoviedbapi.model.rated.RatedTvSeries;
import info.movito.themoviedbapi.model.rated.RatedTvSeriesResultsPage;
import info.movito.themoviedbapi.tools.RequestType;
import info.movito.themoviedbapi.tools.TmdbException;
import info.movito.themoviedbapi.tools.sortby.AccountSortBy;
import info.movito.themoviedbapi.util.TestUtils;
import org.junit.jupiter.api.Test;

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

import static info.movito.themoviedbapi.TmdbGuestSessions.TMDB_METHOD_GUEST_SESSIONS;
import static info.movito.themoviedbapi.tools.ApiUrl.TMDB_API_BASE_URL;
import static info.movito.themoviedbapi.util.TestUtils.testForNullFieldsAndNewItems;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.when;

/**
* Tests for {@link TmdbGuestSessions}.
*/
public class TmdbGuestSessionsTest extends AbstractTmdbApiTest {
/**
* Tests the {@link TmdbGuestSessions#getRatedMovies(int, String, Integer, AccountSortBy)} with an expected result.
*/
@Test
public void testGetRatedMovies() throws IOException, TmdbException {
String body = TestUtils.readTestFile("api_responses/guest_sessions/rated_movies.json");
URL url = new URL(TMDB_API_BASE_URL + TMDB_METHOD_GUEST_SESSIONS + "/1/rated/movies?language=en&page=1&sort_by=created_at.desc");
when(getTmdbUrlReader().readUrl(url, null, RequestType.GET)).thenReturn(body);

TmdbGuestSessions guestSessions = new TmdbGuestSessions(getTmdbApi());
RatedMovieResultsPage ratedMovieResultsPage = guestSessions.getRatedMovies(1, "en", 1, AccountSortBy.CREATED_AT_DESC);
assertNotNull(ratedMovieResultsPage);
testForNullFieldsAndNewItems(ratedMovieResultsPage);

List<RatedMovie> ratedMovies = ratedMovieResultsPage.getResults();
assertFalse(ratedMovies.isEmpty());

RatedMovie ratedMovie = ratedMovies.get(0);
assertNotNull(ratedMovie);
testForNullFieldsAndNewItems(ratedMovie);
}

/**
* Tests the {@link TmdbGuestSessions#getRatedTvSeries(int, String, Integer, AccountSortBy)} with an expected result.
*/
@Test
public void testGetRatedTvSeries() throws IOException, TmdbException {
String body = TestUtils.readTestFile("api_responses/guest_sessions/rated_tv.json");
URL url = new URL(TMDB_API_BASE_URL + TMDB_METHOD_GUEST_SESSIONS + "/1/rated/tv?language=en&page=1&sort_by=created_at.desc");
when(getTmdbUrlReader().readUrl(url, null, RequestType.GET)).thenReturn(body);

TmdbGuestSessions guestSessions = new TmdbGuestSessions(getTmdbApi());
RatedTvSeriesResultsPage ratedTvSeriesResultsPage = guestSessions.getRatedTvSeries(1, "en", 1, AccountSortBy.CREATED_AT_DESC);
assertNotNull(ratedTvSeriesResultsPage);
testForNullFieldsAndNewItems(ratedTvSeriesResultsPage);

List<RatedTvSeries> ratedTvSeriess = ratedTvSeriesResultsPage.getResults();
assertFalse(ratedTvSeriess.isEmpty());

RatedTvSeries ratedTvSeries = ratedTvSeriess.get(0);
assertNotNull(ratedTvSeries);
testForNullFieldsAndNewItems(ratedTvSeries);
}

/**
* Tests the {@link TmdbGuestSessions#getRatedTvEpisodes(int, String, Integer, AccountSortBy)} with an expected result.
*/
@Test
public void testGetRatedTvEpisodes() throws IOException, TmdbException {
String body = TestUtils.readTestFile("api_responses/guest_sessions/rated_tv_episodes.json");
URL url = new URL(TMDB_API_BASE_URL + TMDB_METHOD_GUEST_SESSIONS +
"/1/rated/tv/episodes?language=en&page=1&sort_by=created_at.desc");
when(getTmdbUrlReader().readUrl(url, null, RequestType.GET)).thenReturn(body);

TmdbGuestSessions guestSessions = new TmdbGuestSessions(getTmdbApi());
RatedTvEpisodeResultsPage ratedTvEpisodesResultsPage = guestSessions.getRatedTvEpisodes(1, "en", 1, AccountSortBy.CREATED_AT_DESC);
assertNotNull(ratedTvEpisodesResultsPage);
testForNullFieldsAndNewItems(ratedTvEpisodesResultsPage);

List<RatedTvEpisode> ratedTvEpisodes = ratedTvEpisodesResultsPage.getResults();
assertFalse(ratedTvEpisodes.isEmpty());

RatedTvEpisode ratedTvEpisode = ratedTvEpisodes.get(0);
assertNotNull(ratedTvEpisode);
testForNullFieldsAndNewItems(ratedTvEpisode);
}
}
27 changes: 27 additions & 0 deletions src/test/resources/api_responses/guest_sessions/rated_movies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"page": 1,
"results": [
{
"adult": false,
"backdrop_path": "/ikR2qy9xJCHX7M8i5rcvuNfdYXs.jpg",
"genre_ids": [
18,
80
],
"id": 16,
"original_language": "en",
"original_title": "Dancer in the Dark",
"overview": "Selma, a Czech immigrant on the verge of blindness, struggles to make ends meet for herself and her son, who has inherited the same genetic disorder and will suffer the same fate without an expensive operation. When life gets too difficult, Selma learns to cope through her love of musicals, escaping life's troubles - even if just for a moment - by dreaming up little numbers to the rhythmic beats of her surroundings.",
"popularity": 14.684,
"poster_path": "/8Wdd3fQfbbQeoSfWpHrDfaFNhBU.jpg",
"release_date": "2000-06-30",
"title": "Dancer in the Dark",
"video": false,
"vote_average": 7.885,
"vote_count": 1549,
"rating": 8.5
}
],
"total_pages": 1,
"total_results": 1
}
30 changes: 30 additions & 0 deletions src/test/resources/api_responses/guest_sessions/rated_tv.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"page": 1,
"results": [
{
"adult": false,
"backdrop_path": "/2OMB0ynKlyIenMJWI2Dy9IWT4c.jpg",
"genre_ids": [
10765,
18,
10759
],
"id": 1399,
"origin_country": [
"US"
],
"original_language": "en",
"original_name": "Game of Thrones",
"overview": "Seven noble families fight for control of the mythical land of Westeros. Friction between the houses leads to full-scale war. All while a very ancient evil awakens in the farthest north. Amidst the war, a neglected military order of misfits, the Night's Watch, is all that stands between the realms of men and icy horrors beyond.",
"popularity": 404.299,
"poster_path": "/7WUHnWGx5OO145IRxPDUkQSh4C7.jpg",
"first_air_date": "2011-04-17",
"name": "Game of Thrones",
"vote_average": 8.436,
"vote_count": 21025,
"rating": 8.5
}
],
"total_pages": 1,
"total_results": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"page": 1,
"results": [
{
"air_date": "2011-04-17",
"episode_number": 1,
"id": 63056,
"name": "Winter Is Coming",
"overview": "Jon Arryn, the Hand of the King, is dead. King Robert Baratheon plans to ask his oldest friend, Eddard Stark, to take Jon's place. Across the sea, Viserys Targaryen plans to wed his sister to a nomadic warlord in exchange for an army.",
"production_code": "101",
"runtime": 62,
"season_number": 1,
"show_id": 1399,
"still_path": "/9hGF3WUkBf7cSjMg0cdMDHJkByd.jpg",
"vote_average": 7.843,
"vote_count": 286,
"rating": 8.5
}
],
"total_pages": 1,
"total_results": 1
}

0 comments on commit 8cef995

Please sign in to comment.