implemented method to post movie ratings
This commit is contained in:
@@ -19,7 +19,7 @@
|
||||
*/
|
||||
package com.omertron.themoviedbapi;
|
||||
|
||||
import static com.omertron.themoviedbapi.tools.ApiUrl.*;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.omertron.themoviedbapi.MovieDbException.MovieDbExceptionType;
|
||||
import com.omertron.themoviedbapi.model.*;
|
||||
@@ -28,19 +28,23 @@ import com.omertron.themoviedbapi.results.TmdbResultsMap;
|
||||
import com.omertron.themoviedbapi.tools.ApiUrl;
|
||||
import com.omertron.themoviedbapi.tools.WebBrowser;
|
||||
import com.omertron.themoviedbapi.wrapper.*;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.yamj.api.common.http.CommonHttpClient;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.omertron.themoviedbapi.tools.ApiUrl.*;
|
||||
|
||||
/**
|
||||
* The MovieDb API <p> This is for version 3 of the API as specified here: http://help.themoviedb.org/kb/api/about-3
|
||||
*
|
||||
@@ -939,17 +943,35 @@ public class TheMovieDbApi {
|
||||
* A valid session id is required.
|
||||
*
|
||||
* @param sessionId
|
||||
* @param movieId
|
||||
* @param rating
|
||||
* @throws MovieDbException
|
||||
* @throws JsonProcessingException
|
||||
*/
|
||||
public boolean postMovieRating(String sessionId, String rating) throws MovieDbException {
|
||||
ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/rating");
|
||||
public boolean postMovieRating(String sessionId, Integer movieId, Integer rating) throws MovieDbException {
|
||||
ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, movieId+ "/rating");
|
||||
|
||||
apiUrl.addArgument(PARAM_SESSION, sessionId);
|
||||
apiUrl.addArgument(PARAM_VALUE, rating);
|
||||
|
||||
throw new MovieDbException(MovieDbExceptionType.UNKNOWN_CAUSE, "Not implemented yet");
|
||||
if(rating <0 || rating > 10) {
|
||||
throw new MovieDbException(MovieDbExceptionType.UNKNOWN_CAUSE, "rating out of range");
|
||||
}
|
||||
|
||||
String jsonBody;
|
||||
|
||||
// note exception will never be thrown
|
||||
try {
|
||||
jsonBody = new ObjectMapper().writeValueAsString(Collections.singletonMap("value", (double) rating));
|
||||
} catch (JsonProcessingException ignored) {
|
||||
throw new RuntimeException(ignored);
|
||||
}
|
||||
|
||||
URL url = apiUrl.buildUrl();
|
||||
String webpage = WebBrowser.request(url, jsonBody);
|
||||
|
||||
return webpage.contains("The item/record was updated successfully");
|
||||
}
|
||||
|
||||
//</editor-fold>
|
||||
|
||||
//<editor-fold defaultstate="collapsed" desc="Collection Functions">
|
||||
|
||||
@@ -20,10 +20,11 @@
|
||||
package com.omertron.themoviedbapi.tools;
|
||||
|
||||
import com.omertron.themoviedbapi.MovieDbException;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.StringWriter;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
@@ -35,9 +36,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Web browser with simple cookies support
|
||||
@@ -68,6 +66,7 @@ public final class WebBrowser {
|
||||
if (browserProperties.isEmpty()) {
|
||||
browserProperties.put("User-Agent", "Mozilla/5.25 Netscape/5.0 (Windows; I; Win95)");
|
||||
browserProperties.put("Accept", "application/json");
|
||||
browserProperties.put("Content-type", "application/json");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,6 +99,10 @@ public final class WebBrowser {
|
||||
}
|
||||
|
||||
public static String request(URL url) throws MovieDbException {
|
||||
return request(url, null);
|
||||
}
|
||||
|
||||
public static String request(URL url, String jsonBody) throws MovieDbException {
|
||||
StringWriter content = null;
|
||||
|
||||
try {
|
||||
@@ -111,6 +114,14 @@ public final class WebBrowser {
|
||||
cnx = openProxiedConnection(url);
|
||||
|
||||
sendHeader(cnx);
|
||||
|
||||
if (jsonBody != null) {
|
||||
cnx.setDoOutput(true);
|
||||
OutputStreamWriter wr = new OutputStreamWriter(cnx.getOutputStream());
|
||||
wr.write(jsonBody);
|
||||
wr.flush();
|
||||
}
|
||||
|
||||
readHeader(cnx);
|
||||
|
||||
in = new BufferedReader(new InputStreamReader(cnx.getInputStream(), getCharset(cnx)));
|
||||
|
||||
@@ -19,39 +19,19 @@
|
||||
*/
|
||||
package com.omertron.themoviedbapi;
|
||||
|
||||
import com.omertron.themoviedbapi.model.AlternativeTitle;
|
||||
import com.omertron.themoviedbapi.model.Artwork;
|
||||
import com.omertron.themoviedbapi.model.ChangedItem;
|
||||
import com.omertron.themoviedbapi.model.Collection;
|
||||
import com.omertron.themoviedbapi.model.CollectionInfo;
|
||||
import com.omertron.themoviedbapi.model.Company;
|
||||
import com.omertron.themoviedbapi.model.Discover;
|
||||
import com.omertron.themoviedbapi.model.Genre;
|
||||
import com.omertron.themoviedbapi.model.JobDepartment;
|
||||
import com.omertron.themoviedbapi.model.Keyword;
|
||||
import com.omertron.themoviedbapi.model.KeywordMovie;
|
||||
import com.omertron.themoviedbapi.model.MovieDb;
|
||||
import com.omertron.themoviedbapi.model.MovieDbList;
|
||||
import com.omertron.themoviedbapi.model.MovieList;
|
||||
import com.omertron.themoviedbapi.model.Person;
|
||||
import com.omertron.themoviedbapi.model.PersonCredit;
|
||||
import com.omertron.themoviedbapi.model.ReleaseInfo;
|
||||
import com.omertron.themoviedbapi.model.Reviews;
|
||||
import com.omertron.themoviedbapi.model.TmdbConfiguration;
|
||||
import com.omertron.themoviedbapi.model.TokenAuthorisation;
|
||||
import com.omertron.themoviedbapi.model.TokenSession;
|
||||
import com.omertron.themoviedbapi.model.Trailer;
|
||||
import com.omertron.themoviedbapi.model.Translation;
|
||||
import com.omertron.themoviedbapi.model.*;
|
||||
import com.omertron.themoviedbapi.results.TmdbResultsList;
|
||||
import com.omertron.themoviedbapi.results.TmdbResultsMap;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.*;
|
||||
import static org.junit.Assert.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Test cases for TheMovieDbApi API
|
||||
*
|
||||
@@ -78,6 +58,9 @@ public class TheMovieDbApiTest {
|
||||
private static final String LANGUAGE_ENGLISH = "en";
|
||||
private static final String LANGUAGE_RUSSIAN = "ru";
|
||||
|
||||
// session id of test users named 'apitests'
|
||||
private static final String SESSION_ID_APITESTS = "63c85deb39337e29b69d78265eb28d639cbd6f72";
|
||||
|
||||
public TheMovieDbApiTest() throws MovieDbException {
|
||||
}
|
||||
|
||||
@@ -613,16 +596,15 @@ public class TheMovieDbApiTest {
|
||||
*
|
||||
* TODO: Cannot be tested without a HTTP authorisation: http://help.themoviedb.org/kb/api/user-authentication
|
||||
*/
|
||||
@Ignore("Not ready yet")
|
||||
@Test
|
||||
public void testPostMovieRating() throws Exception {
|
||||
LOG.info("postMovieRating");
|
||||
String sessionId = "";
|
||||
String rating = "";
|
||||
boolean expResult = false;
|
||||
boolean result = tmdb.postMovieRating(sessionId, rating);
|
||||
assertEquals(expResult, result);
|
||||
// TODO review the generated test code and remove the default call to fail.
|
||||
fail("The test case is a prototype.");
|
||||
Integer movieID = 68724;
|
||||
Integer rating = 3;
|
||||
|
||||
boolean wasPosted = tmdb.postMovieRating(SESSION_ID_APITESTS, movieID, rating);
|
||||
|
||||
assertTrue(wasPosted);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user