diff --git a/pom.xml b/pom.xml index c040c4a28..45d9f901c 100644 --- a/pom.xml +++ b/pom.xml @@ -120,7 +120,7 @@ org.yamj api-common - 1.3 + 1.4-SNAPSHOT diff --git a/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java b/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java index 7008ea927..e04cf8225 100644 --- a/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java +++ b/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java @@ -66,7 +66,7 @@ import static com.omertron.themoviedbapi.tools.ApiUrl.PARAM_SESSION; import static com.omertron.themoviedbapi.tools.ApiUrl.PARAM_START_DATE; import static com.omertron.themoviedbapi.tools.ApiUrl.PARAM_TOKEN; import static com.omertron.themoviedbapi.tools.ApiUrl.PARAM_YEAR; -import com.omertron.themoviedbapi.tools.WebBrowser; +import com.omertron.themoviedbapi.tools.HttpTools; import com.omertron.themoviedbapi.wrapper.WrapperAlternativeTitles; import com.omertron.themoviedbapi.wrapper.WrapperChanges; import com.omertron.themoviedbapi.wrapper.WrapperCollection; @@ -93,21 +93,16 @@ import com.omertron.themoviedbapi.wrapper.WrapperTranslations; import com.omertron.themoviedbapi.wrapper.WrapperVideos; import java.io.IOException; import java.net.MalformedURLException; -import java.net.URISyntaxException; import java.net.URL; -import java.nio.charset.Charset; import java.util.Collections; 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.apache.http.impl.client.CloseableHttpClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.yamj.api.common.exception.ApiExceptionType; -import org.yamj.api.common.http.DigestedResponse; -import org.yamj.api.common.http.DigestedResponseReader; import org.yamj.api.common.http.SimpleHttpClientBuilder; /** @@ -122,10 +117,8 @@ public class TheMovieDbApi { private static final Logger LOG = LoggerFactory.getLogger(TheMovieDbApi.class); private String apiKey; - private CloseableHttpClient httpClient; private TmdbConfiguration tmdbConfig; - private static final String DEFAULT_CHARSET = "UTF-8"; - private final Charset charset = Charset.forName(DEFAULT_CHARSET); + private HttpTools httpTools; // API Methods private static final String BASE_MOVIE = "movie/"; private static final String BASE_PERSON = "person/"; @@ -148,8 +141,6 @@ public class TheMovieDbApi { private static final int YEAR_LENGTH = 4; private static final int RATING_MAX = 10; private static final int POST_SUCCESS_STATUS_CODE = 12; - private static final int HTTP_STATUS_300 = 300; - private static final int HTTP_STATUS_500 = 500; /** * API for The Movie Db. @@ -170,11 +161,11 @@ public class TheMovieDbApi { */ public TheMovieDbApi(String apiKey, CloseableHttpClient httpClient) throws MovieDbException { this.apiKey = apiKey; - this.httpClient = httpClient; + this.httpTools = new HttpTools(httpClient); ApiUrl apiUrl = new ApiUrl(apiKey, "configuration"); URL configUrl = apiUrl.buildUrl(); - String webpage = requestWebPage(configUrl); + String webpage = httpTools.getRequest(configUrl); try { WrapperConfig wc = mapper.readValue(webpage, WrapperConfig.class); @@ -193,56 +184,6 @@ public class TheMovieDbApi { return apiKey; } - private String requestWebPage(URL url) throws MovieDbException { - return requestWebPage(url, null, Boolean.FALSE); - } - - private String requestWebPage(URL url, String jsonBody) throws MovieDbException { - return requestWebPage(url, jsonBody, Boolean.FALSE); - } - - private String requestWebPage(URL url, String jsonBody, boolean isDeleteRequest) throws MovieDbException { - String webpage; - // use HTTP client implementation - if (httpClient == null) { - // use web browser - webpage = WebBrowser.request(url, jsonBody, isDeleteRequest); - } else { - try { - HttpGet httpGet = new HttpGet(url.toURI()); - httpGet.addHeader("accept", "application/json"); - - if (StringUtils.isNotBlank(jsonBody)) { - // TODO: Add the json body to the request - throw new MovieDbException(ApiExceptionType.UNKNOWN_CAUSE, "Unable to proces JSON request", url); - } - - if (isDeleteRequest) { - //TODO: Handle delete request - throw new MovieDbException(ApiExceptionType.UNKNOWN_CAUSE, "Unable to proces delete request", url); - } - - final DigestedResponse response = DigestedResponseReader.requestContent(httpClient, httpGet, charset); - - if (response.getStatusCode() >= HTTP_STATUS_500) { - throw new MovieDbException(ApiExceptionType.HTTP_503_ERROR, response.getContent(), response.getStatusCode(), url, null); - } else if (response.getStatusCode() >= HTTP_STATUS_300) { - throw new MovieDbException(ApiExceptionType.HTTP_404_ERROR, response.getContent(), response.getStatusCode(), url, null); - } - - webpage = response.getContent(); - - } catch (URISyntaxException ex) { - throw new MovieDbException(ApiExceptionType.CONNECTION_ERROR, null, url, ex); - } catch (IOException ex) { - throw new MovieDbException(ApiExceptionType.CONNECTION_ERROR, null, url, ex); - } catch (RuntimeException ex) { - throw new MovieDbException(ApiExceptionType.HTTP_503_ERROR, "Service Unavailable", url, ex); - } - } - return webpage; - } - /** * Compare the MovieDB object with a title & year * @@ -396,7 +337,7 @@ public class TheMovieDbApi { ApiUrl apiUrl = new ApiUrl(apiKey, BASE_AUTH, "token/new"); URL url = apiUrl.buildUrl(); - String webpage = requestWebPage(url); + String webpage = httpTools.getRequest(url); try { return mapper.readValue(webpage, TokenAuthorisation.class); @@ -426,7 +367,7 @@ public class TheMovieDbApi { apiUrl.addArgument(PARAM_TOKEN, token.getRequestToken()); URL url = apiUrl.buildUrl(); - String webpage = requestWebPage(url); + String webpage = httpTools.getRequest(url); try { return mapper.readValue(webpage, TokenSession.class); @@ -459,7 +400,7 @@ public class TheMovieDbApi { ApiUrl apiUrl = new ApiUrl(apiKey, BASE_AUTH, "guest_session/new"); URL url = apiUrl.buildUrl(); - String webpage = requestWebPage(url); + String webpage = httpTools.getRequest(url); try { return mapper.readValue(webpage, TokenSession.class); @@ -483,7 +424,7 @@ public class TheMovieDbApi { apiUrl.addArgument(PARAM_SESSION, sessionId); URL url = apiUrl.buildUrl(); - String webpage = requestWebPage(url); + String webpage = httpTools.getRequest(url); try { return mapper.readValue(webpage, Account.class); @@ -498,7 +439,7 @@ public class TheMovieDbApi { apiUrl.addArgument(PARAM_SESSION, sessionId); URL url = apiUrl.buildUrl(); - String webpage = requestWebPage(url); + String webpage = httpTools.getRequest(url); try { return mapper.readValue(webpage, WrapperMovie.class).getMovies(); @@ -519,7 +460,7 @@ public class TheMovieDbApi { String jsonBody = convertToJson(body); URL url = apiUrl.buildUrl(); - String webpage = requestWebPage(url, jsonBody); + String webpage = httpTools.postRequest(url, jsonBody); try { return mapper.readValue(webpage, StatusCode.class); @@ -566,7 +507,7 @@ public class TheMovieDbApi { String jsonBody = convertToJson(body); URL url = apiUrl.buildUrl(); - String webpage = requestWebPage(url, jsonBody); + String webpage = httpTools.postRequest(url, jsonBody); try { return mapper.readValue(webpage, StatusCode.class); @@ -604,7 +545,7 @@ public class TheMovieDbApi { apiUrl.appendToResponse(appendToResponse); URL url = apiUrl.buildUrl(); - String webpage = requestWebPage(url); + String webpage = httpTools.getRequest(url); try { MovieDb movie = mapper.readValue(webpage, MovieDb.class); if (movie == null || movie.getId() == 0) { @@ -644,7 +585,7 @@ public class TheMovieDbApi { apiUrl.appendToResponse(appendToResponse); URL url = apiUrl.buildUrl(); - String webpage = requestWebPage(url); + String webpage = httpTools.getRequest(url); try { MovieDb movie = mapper.readValue(webpage, MovieDb.class); if (movie == null || movie.getId() == 0) { @@ -679,7 +620,7 @@ public class TheMovieDbApi { apiUrl.appendToResponse(appendToResponse); URL url = apiUrl.buildUrl(); - String webpage = requestWebPage(url); + String webpage = httpTools.getRequest(url); try { WrapperAlternativeTitles wrapper = mapper.readValue(webpage, WrapperAlternativeTitles.class); TmdbResultsList results = new TmdbResultsList(wrapper.getTitles()); @@ -708,7 +649,7 @@ public class TheMovieDbApi { apiUrl.appendToResponse(appendToResponse); URL url = apiUrl.buildUrl(); - String webpage = requestWebPage(url); + String webpage = httpTools.getRequest(url); try { WrapperMovieCasts wrapper = mapper.readValue(webpage, WrapperMovieCasts.class); @@ -742,7 +683,7 @@ public class TheMovieDbApi { apiUrl.appendToResponse(appendToResponse); URL url = apiUrl.buildUrl(); - String webpage = requestWebPage(url); + String webpage = httpTools.getRequest(url); try { WrapperImages wrapper = mapper.readValue(webpage, WrapperImages.class); @@ -773,7 +714,7 @@ public class TheMovieDbApi { apiUrl.appendToResponse(appendToResponse); URL url = apiUrl.buildUrl(); - String webpage = requestWebPage(url); + String webpage = httpTools.getRequest(url); try { WrapperMovieKeywords wrapper = mapper.readValue(webpage, WrapperMovieKeywords.class); @@ -804,7 +745,7 @@ public class TheMovieDbApi { apiUrl.appendToResponse(appendToResponse); URL url = apiUrl.buildUrl(); - String webpage = requestWebPage(url); + String webpage = httpTools.getRequest(url); try { WrapperReleaseInfo wrapper = mapper.readValue(webpage, WrapperReleaseInfo.class); @@ -840,11 +781,11 @@ public class TheMovieDbApi { apiUrl.appendToResponse(appendToResponse); URL url = apiUrl.buildUrl(); - String webpage = requestWebPage(url); + String webpage = httpTools.getRequest(url); try { WrapperVideos wrapper = mapper.readValue(webpage, WrapperVideos.class); - TmdbResultsList