master
Stuart Boston 11 years ago
parent 50820a437f
commit 8522ac4171

@ -54,7 +54,7 @@ import com.omertron.themoviedbapi.model2.JobDepartment;
import com.omertron.themoviedbapi.model.keyword.Keyword;
import com.omertron.themoviedbapi.model.keyword.KeywordMovie;
import com.omertron.themoviedbapi.model.MovieDb;
import com.omertron.themoviedbapi.model.MovieDbList;
import com.omertron.themoviedbapi.model2.list.ListItem;
import com.omertron.themoviedbapi.model.MovieList;
import com.omertron.themoviedbapi.model.person.Person;
import com.omertron.themoviedbapi.model.person.PersonCredit;
@ -77,7 +77,6 @@ import com.omertron.themoviedbapi.results.TmdbResultsList;
import com.omertron.themoviedbapi.results.TmdbResultsMap;
import com.omertron.themoviedbapi.tools.HttpTools;
import com.omertron.themoviedbapi.tools.MethodBase;
import com.omertron.themoviedbapi.tools.MethodSub;
import java.net.URL;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
@ -806,7 +805,7 @@ public class TheMovieDbApi {
* @return The list and its items
* @throws MovieDbException
*/
public MovieDbList getList(String listId) throws MovieDbException {
public ListItem<MovieDb> getList(String listId) throws MovieDbException {
return tmdbList.getList(listId);
}
@ -824,54 +823,59 @@ public class TheMovieDbApi {
}
/**
* Check to see if a movie ID is already added to a list.
* This method lets users delete a list that they created. A valid session id is required.
*
* @param sessionId
* @param listId
* @param movieId
* @return true if the movie is on the list
* @return
* @throws MovieDbException
*/
public boolean isMovieOnList(String listId, Integer movieId) throws MovieDbException {
return tmdbList.isMovieOnList(listId, movieId);
public StatusCode deleteList(String sessionId, String listId) throws MovieDbException {
return tmdbList.deleteList(sessionId, listId);
}
/**
* This method lets users add new movies to a list that they created. A valid session id is required.
* Check to see if an item is already on a list.
*
* @param sessionId
* @param listId
* @param movieId
* @return true if the movie is on the list
* @param mediaId
* @return true if the item is on the list
* @throws MovieDbException
*/
public StatusCode addMovieToList(String sessionId, String listId, Integer movieId) throws MovieDbException {
return tmdbList.modifyMovieList(sessionId, listId, movieId, MethodSub.ADD_ITEM);
public boolean checkItemStatus(String listId, Integer mediaId) throws MovieDbException {
return tmdbList.checkItemStatus(listId, mediaId);
}
/**
* This method lets users remove movies from a list that they created. A valid session id is required.
* This method lets users add new items to a list that they created.
*
* A valid session id is required.
*
* @param sessionId
* @param listId
* @param movieId
* @param mediaId
* @return true if the movie is on the list
* @throws MovieDbException
*/
public StatusCode removeMovieFromList(String sessionId, String listId, Integer movieId) throws MovieDbException {
return tmdbList.modifyMovieList(sessionId, listId, movieId, MethodSub.REMOVE_ITEM);
public StatusCode addItemToList(String sessionId, String listId, Integer mediaId) throws MovieDbException {
return tmdbList.addItem(sessionId, listId, mediaId);
}
/**
* This method lets users delete a list that they created. A valid session id is required.
* This method lets users remove items from a list that they created.
*
* A valid session id is required.
*
* @param sessionId
* @param listId
* @return
* @param mediaId
* @return true if the movie is on the list
* @throws MovieDbException
*/
public StatusCode deleteMovieList(String sessionId, String listId) throws MovieDbException {
return tmdbList.deleteMovieList(sessionId, listId);
public StatusCode removeItemFromList(String sessionId, String listId, Integer mediaId) throws MovieDbException {
return tmdbList.removeItem(sessionId, listId, mediaId);
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Movies">

@ -19,9 +19,11 @@
*/
package com.omertron.themoviedbapi.methods;
import com.fasterxml.jackson.core.type.TypeReference;
import com.omertron.themoviedbapi.MovieDbException;
import com.omertron.themoviedbapi.model.ListItemStatus;
import com.omertron.themoviedbapi.model.MovieDbList;
import com.omertron.themoviedbapi.model.MovieDb;
import com.omertron.themoviedbapi.model2.list.ListItem;
import com.omertron.themoviedbapi.model.MovieDbListStatus;
import com.omertron.themoviedbapi.model2.StatusCode;
import com.omertron.themoviedbapi.tools.ApiUrl;
@ -61,7 +63,7 @@ public class TmdbLists extends AbstractMethod {
* @return The list and its items
* @throws MovieDbException
*/
public MovieDbList getList(String listId) throws MovieDbException {
public ListItem<MovieDb> getList(String listId) throws MovieDbException {
TmdbParameters parameters = new TmdbParameters();
parameters.add(Param.ID, listId);
@ -69,12 +71,35 @@ public class TmdbLists extends AbstractMethod {
String webpage = httpTools.getRequest(url);
try {
return MAPPER.readValue(webpage, MovieDbList.class);
return MAPPER.readValue(webpage, new TypeReference<ListItem<MovieDb>>(){});
} catch (IOException ex) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get list", url, ex);
}
}
/**
* Check to see if an ID is already on a list.
*
* @param listId
* @param mediaId
* @return true if the movie is on the list
* @throws MovieDbException
*/
public boolean checkItemStatus(String listId, int mediaId) throws MovieDbException {
TmdbParameters parameters = new TmdbParameters();
parameters.add(Param.ID, listId);
parameters.add(Param.MOVIE_ID, mediaId);
URL url = new ApiUrl(apiKey, MethodBase.LIST).setSubMethod(MethodSub.ITEM_STATUS).buildUrl(parameters);
String webpage = httpTools.getRequest(url);
try {
return MAPPER.readValue(webpage, ListItemStatus.class).isItemPresent();
} catch (IOException ex) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get item status", url, ex);
}
}
/**
* This method lets users create a new list. A valid session id is required.
*
@ -104,31 +129,33 @@ public class TmdbLists extends AbstractMethod {
}
/**
* Check to see if a movie ID is already added to a list.
* This method lets users delete a list that they created. A valid session id is required.
*
* @param sessionId
* @param listId
* @param movieId
* @return true if the movie is on the list
* @return
* @throws MovieDbException
*/
public boolean isMovieOnList(String listId, Integer movieId) throws MovieDbException {
public StatusCode deleteList(String sessionId, String listId) throws MovieDbException {
TmdbParameters parameters = new TmdbParameters();
parameters.add(Param.ID, listId);
parameters.add(Param.MOVIE_ID, movieId);
parameters.add(Param.SESSION, sessionId);
URL url = new ApiUrl(apiKey, MethodBase.LIST).setSubMethod(MethodSub.ITEM_STATUS).buildUrl(parameters);
String webpage = httpTools.getRequest(url);
URL url = new ApiUrl(apiKey, MethodBase.LIST).buildUrl(parameters);
String webpage = httpTools.deleteRequest(url);
try {
return MAPPER.readValue(webpage, ListItemStatus.class).isItemPresent();
return MAPPER.readValue(webpage, StatusCode.class);
} catch (IOException ex) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get media status", url, ex);
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to delete list", url, ex);
}
}
/**
* Modify a list
*
* This can be used to add or remove an item from the list
*
* @param sessionId
* @param listId
* @param movieId
@ -136,7 +163,7 @@ public class TmdbLists extends AbstractMethod {
* @return
* @throws MovieDbException
*/
public StatusCode modifyMovieList(String sessionId, String listId, Integer movieId, MethodSub operation) throws MovieDbException {
private StatusCode modifyMovieList(String sessionId, String listId, int movieId, MethodSub operation) throws MovieDbException {
TmdbParameters parameters = new TmdbParameters();
parameters.add(Param.SESSION, sessionId);
parameters.add(Param.ID, listId);
@ -151,31 +178,68 @@ public class TmdbLists extends AbstractMethod {
try {
return MAPPER.readValue(webpage, StatusCode.class);
} catch (IOException ex) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to remove movie from list", url, ex);
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to remove item from list", url, ex);
}
}
/**
* This method lets users delete a list that they created. A valid session
* id is required.
* This method lets users add new items to a list that they created.
*
* A valid session id is required.
*
* @param sessionId
* @param listId
* @param mediaId
* @return
* @throws MovieDbException
*/
public StatusCode deleteMovieList(String sessionId, String listId) throws MovieDbException {
public StatusCode addItem(String sessionId, String listId, int mediaId) throws MovieDbException {
return modifyMovieList(sessionId, listId, mediaId, MethodSub.ADD_ITEM);
}
/**
* This method lets users delete items from a list that they created.
*
* A valid session id is required.
*
* @param sessionId
* @param listId
* @param mediaId
* @return
* @throws MovieDbException
*/
public StatusCode removeItem(String sessionId, String listId, int mediaId) throws MovieDbException {
return modifyMovieList(sessionId, listId, mediaId, MethodSub.REMOVE_ITEM);
}
/**
* Clear all of the items within a list.
*
* This is a irreversible action and should be treated with caution.
*
* A valid session id is required. A call without confirm=true will return status code 29.
*
* @param sessionId
* @param listId
* @param confirm
* @return
* @throws MovieDbException
*/
public StatusCode clear(String sessionId, String listId, boolean confirm) throws MovieDbException {
TmdbParameters parameters = new TmdbParameters();
parameters.add(Param.ID, listId);
parameters.add(Param.SESSION, sessionId);
parameters.add(Param.ID, listId);
parameters.add(Param.CONFIRM, confirm);
URL url = new ApiUrl(apiKey, MethodBase.LIST).buildUrl(parameters);
String webpage = httpTools.deleteRequest(url);
URL url = new ApiUrl(apiKey, MethodBase.LIST).setSubMethod(MethodSub.CLEAR).buildUrl(parameters);
String webpage = httpTools.postRequest(url, "");
try {
return MAPPER.readValue(webpage, StatusCode.class);
} catch (IOException ex) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to delete movie list", url, ex);
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to clear list", url, ex);
}
}
}

@ -41,24 +41,24 @@ Discover (Done)
/discover/tv Discover TV shows by different types of data like average rating, number of votes, genres, the network they aired on and air dates.
Find
/find/{id} The find method makes it easy to search for objects in our database by an external id.
*** /find/{id} The find method makes it easy to search for objects in our database by an external id.
Genres (Done)
/genre/movie/list Get the list of movie genres.
/genre/tv/list Get the list of TV genres.
/genre/{id}/movies Get the list of movies for a particular genre by id.
Guest Sessions
Guest Sessions (Partial - in Account)
/guest_session/{guest_session_id}/rated_movies Get a list of rated movies for a specific guest session id.
Jobs (Done)
/job/list Get a list of valid jobs.
Keyword
Keyword (Done)
/keyword/{id} Get the basic information for a specific keyword id
/keyword/{id}/movies Get the list of movies for a particular keyword by id
Lists
Lists (Done)
/list/{id} Get a list by id.
/list/{id}/item_status Check to see if a movie ID is already added to a list.
/list This method lets users create a new list. A valid session id is required.

@ -17,19 +17,20 @@
* along with TheMovieDB API. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.omertron.themoviedbapi.model;
package com.omertron.themoviedbapi.model2.list;
import com.omertron.themoviedbapi.model2.AbstractJsonMapping;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.omertron.themoviedbapi.model2.AbstractJsonMapping;
import java.util.Collections;
import java.util.List;
/**
* Wrapper for the MovieDbList function
* Wrapper for the ListItem function
*
* @author stuart.boston
* @param <T> Type of list
*/
public class MovieDbList extends AbstractJsonMapping {
public class ListItem<T> extends AbstractJsonMapping {
@JsonProperty("id")
private String id;
@ -40,7 +41,7 @@ public class MovieDbList extends AbstractJsonMapping {
@JsonProperty("favorite_count")
private int favoriteCount;
@JsonProperty("items")
private List<MovieDb> items = Collections.emptyList();
private List<T> items = Collections.emptyList();
@JsonProperty("item_count")
private int itemCount;
@JsonProperty("iso_639_1")
@ -70,7 +71,7 @@ public class MovieDbList extends AbstractJsonMapping {
return favoriteCount;
}
public List<MovieDb> getItems() {
public List<T> getItems() {
return items;
}
@ -114,7 +115,7 @@ public class MovieDbList extends AbstractJsonMapping {
this.favoriteCount = favoriteCount;
}
public void setItems(List<MovieDb> items) {
public void setItems(List<T> items) {
this.items = items;
}

@ -40,6 +40,7 @@ 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 = "&";

@ -28,6 +28,7 @@ public enum MethodSub {
ALT_TITLES("alternative_titles"),
CASTS("casts"),
CHANGES("changes"),
CLEAR("clear"),
COLLECTION("collection"),
COMPANY("company"),
CREDITS("credits"),

@ -32,6 +32,7 @@ public enum Param {
ADULT("include_adult="),
API_KEY("api_key="),
APPEND("append_to_response="),
CONFIRM("confirm="),
COUNTRY("country="),
END_DATE("end_date="),
EXTERNAL_SOURCE("external_source="),

@ -20,20 +20,20 @@
package com.omertron.themoviedbapi.wrapper;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.omertron.themoviedbapi.model.MovieDbList;
import com.omertron.themoviedbapi.model2.list.ListItem;
import java.util.List;
public class WrapperMovieDbList extends AbstractWrapperAll {
@JsonProperty("results")
private List<MovieDbList> lists;
private List<ListItem> lists;
public List<MovieDbList> getLists() {
public List<ListItem> getLists() {
return lists;
}
public void setLists(List<MovieDbList> lists) {
public void setLists(List<ListItem> lists) {
this.lists = lists;
}
}

@ -21,25 +21,33 @@ package com.omertron.themoviedbapi.methods;
import com.omertron.themoviedbapi.AbstractTests;
import com.omertron.themoviedbapi.MovieDbException;
import com.omertron.themoviedbapi.model.MovieDbList;
import com.omertron.themoviedbapi.model2.StatusCode;
import com.omertron.themoviedbapi.tools.MethodSub;
import com.omertron.themoviedbapi.model2.list.ListItem;
import java.util.Random;
import org.apache.commons.lang3.StringUtils;
import org.junit.After;
import org.junit.AfterClass;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.junit.rules.ExpectedException;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
//@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TmdbListsTest extends AbstractTests {
private static TmdbLists instance;
private static final int ID_JUPITER_ASCENDING = 76757;
private static final int ID_BIG_HERO_6 = 177572;
// Status codes
private static final int SC_SUCCESS_UPD = 12;
private static final int SC_SUCCESS_DEL = 13;
// Expected exception
@Rule
public ExpectedException exception = ExpectedException.none();
public TmdbListsTest() {
}
@ -62,100 +70,120 @@ public class TmdbListsTest extends AbstractTests {
public void tearDown() {
}
@Test
public void testSuite() throws MovieDbException {
// Test the list creation
String listId = testCreateList();
// Add two items to the list
testAddItem(listId, ID_JUPITER_ASCENDING);
testAddItem(listId, ID_BIG_HERO_6);
// Get information on the list
testGetList(listId);
// Check item status on the list
testCheckItemStatus(listId, ID_JUPITER_ASCENDING);
// Delete an item from the list
testRemoveItem(listId, ID_BIG_HERO_6);
// Clear the list
testClear(listId);
// Delete the list
testDeleteList(listId);
}
/**
* Test of createList method, of class TmdbList.
* Test of getList method, of class TmdbLists.
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
@Ignore("Not working")
public void test1CreateList() throws MovieDbException {
private void testGetList(String listId) throws MovieDbException {
LOG.info("getList");
ListItem result = instance.getList(listId);
LOG.info("Found {} movies on list", result.getItems().size());
assertFalse("No movies in list", result.getItems().isEmpty());
}
/**
* Test of createList method, of class TmdbLists.
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
private String testCreateList() throws MovieDbException {
LOG.info("createList");
String name = "My Totally Awesome List";
String description = "This list was created to share all of the totally awesome movies I've seen.";
int r = new Random().nextInt();
String name = "Random list name #" + r;
String description = "This is random list number " + r + " used for testing purposes";
String result = instance.createList(getSessionId(), name, description);
LOG.info(result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
assertTrue("No list ID returned", StringUtils.isNotBlank(result));
return result;
}
/**
* Test of getList method, of class TheMovieDbApi.
* Test of checkItemStatus method, of class TmdbLists.
*
* @throws MovieDbException
* @throws com.omertron.themoviedbapi.MovieDbException
*/
@Test
public void test2GetList() throws MovieDbException {
LOG.info("getList");
String listId = "509ec17b19c2950a0600050d";
MovieDbList result = instance.getList(listId);
assertFalse("List not found", result.getItems().isEmpty());
private void testCheckItemStatus(String listId, int mediaId) throws MovieDbException {
LOG.info("checkItemStatus");
boolean expResult = true;
boolean result = instance.checkItemStatus(listId, mediaId);
assertEquals("Item is not on list!", expResult, result);
}
/**
* Test of addMovieToList method, of class TmdbList.
* Test of deleteList method, of class TmdbLists.
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
@Ignore("Not working")
public void test3AddMovieToList() throws MovieDbException {
LOG.info("addMovieToList");
String listId = "";
Integer movieId = null;
StatusCode expResult = null;
StatusCode result = instance.modifyMovieList(getSessionId(), listId, movieId, MethodSub.MOVIE);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
private void testDeleteList(String listId) throws MovieDbException {
LOG.info("deleteList");
StatusCode result = instance.deleteList(getSessionId(), listId);
LOG.info("Result: {}", result);
// We expect there to be an exception thrown here
exception.expect(MovieDbException.class);
ListItem result2 = instance.getList(listId);
LOG.info("Result: {}", result2);
}
/**
* Test of isMovieOnList method, of class TmdbList.
* Test of addItem method, of class TmdbLists.
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
@Ignore("Not working")
public void test4IsMovieOnList() throws MovieDbException {
LOG.info("isMovieOnList");
String listId = "";
Integer movieId = null;
boolean expResult = false;
boolean result = instance.isMovieOnList(listId, movieId);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
private void testAddItem(String listId, int mediaId) throws MovieDbException {
LOG.info("addItem");
StatusCode result = instance.addItem(getSessionId(), listId, mediaId);
assertEquals("Invalid response: " + result.toString(), SC_SUCCESS_UPD, result.getStatusCode());
}
/**
* Test of removeMovieFromList method, of class TmdbList.
* Test of removeItem method, of class TmdbLists.
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
@Ignore("Not working")
public void test5RemoveMovieFromList() throws MovieDbException {
LOG.info("removeMovieFromList");
String listId = "";
Integer movieId = null;
StatusCode expResult = null;
StatusCode result = instance.modifyMovieList(getSessionId(), listId, movieId, MethodSub.MOVIE);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
private void testRemoveItem(String listId, int mediaId) throws MovieDbException {
LOG.info("removeItem");
StatusCode result = instance.removeItem(getSessionId(), listId, mediaId);
assertEquals("Invalid response: " + result.toString(), SC_SUCCESS_DEL, result.getStatusCode());
}
/**
* Test of deleteMovieList method, of class TmdbList.
* Test of clear method, of class TmdbLists.
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
@Ignore("Not working")
public void test6DeleteMovieList() throws MovieDbException {
LOG.info("deleteMovieList");
String listId = "";
StatusCode expResult = null;
StatusCode result = instance.deleteMovieList(getSessionId(), listId);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
private void testClear(String listId) throws MovieDbException {
LOG.info("clear");
StatusCode result = instance.clear(getSessionId(), listId, true);
assertEquals("Invalid response: " + result.toString(), SC_SUCCESS_UPD, result.getStatusCode());
}
}

Loading…
Cancel
Save