-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from c-eg/guess-sessions-api
Guess sessions api
- Loading branch information
Showing
8 changed files
with
274 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/main/java/info/movito/themoviedbapi/TmdbGuestSessions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/test/java/info/movito/themoviedbapi/TmdbGuestSessionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
src/test/resources/api_responses/guest_sessions/rated_movies.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
src/test/resources/api_responses/guest_sessions/rated_tv.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
22 changes: 22 additions & 0 deletions
22
src/test/resources/api_responses/guest_sessions/rated_tv_episodes.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |