diff --git a/lib/Tmdb/Api/AbstractApi.php b/lib/Tmdb/Api/AbstractApi.php index 6a8682a1..9dcfd9ff 100644 --- a/lib/Tmdb/Api/AbstractApi.php +++ b/lib/Tmdb/Api/AbstractApi.php @@ -87,6 +87,28 @@ public function post($path, $postBody = null, array $parameters = array(), $head return $response->json(); } + /** + * Send a POST request but json_encode the post body in the request + * + * @param $path + * @param null $postBody + * @param array $parameters + * @param array $headers + * @return mixed + */ + public function postJson($path, $postBody = null, array $parameters = array(), $headers = array()) + { + /** + * @var Response $response + */ + if (is_array($postBody)) { + $postBody = json_encode($postBody); + } + + $response = $this->client->getHttpClient()->postJson($path, $postBody, $parameters, $headers); + return $response->json(); + } + /** * Send a PUT request * diff --git a/lib/Tmdb/Api/Account.php b/lib/Tmdb/Api/Account.php index 54f15023..086076c1 100644 --- a/lib/Tmdb/Api/Account.php +++ b/lib/Tmdb/Api/Account.php @@ -12,8 +12,6 @@ */ namespace Tmdb\Api; -use Tmdb\Exception\NotImplementedException; - class Account extends AbstractApi { @@ -62,14 +60,13 @@ public function getFavoriteMovies($accountId, array $parameters = array(), array * @param integer $movieId * @param boolean $isFavorite * @return mixed - * @todo refuses to function? review */ public function favorite($accountId, $movieId, $isFavorite = true) { - return $this->post('account/' . $accountId . '/favorite', json_encode(array( + return $this->postJson('account/' . $accountId . '/favorite', array( 'movie_id' => $movieId, 'favorite' => $isFavorite - ))); + )); } /** @@ -105,13 +102,12 @@ public function getMovieWatchlist($accountId, array $parameters = array(), array * @param integer $movieId * @param boolean $isOnWatchlist * @return mixed - * @todo refuses to function? review */ public function watchlist($accountId, $movieId, $isOnWatchlist = true) { - return $this->post('account/' . $accountId . '/movie_watchlist', json_encode(array( + return $this->postJson('account/' . $accountId . '/movie_watchlist', array( 'movie_id' => $movieId, 'movie_watchlist' => $isOnWatchlist - ))); + )); } } diff --git a/lib/Tmdb/Api/Authentication.php b/lib/Tmdb/Api/Authentication.php index 94ac0cb5..95162e27 100644 --- a/lib/Tmdb/Api/Authentication.php +++ b/lib/Tmdb/Api/Authentication.php @@ -80,7 +80,6 @@ public function getNewSession($requestToken) * * If a guest session is not used for the first time within 24 hours, it will be automatically discarded. * - * @throws NotImplementedException * @return mixed */ public function getNewGuestSession() diff --git a/lib/Tmdb/Api/Lists.php b/lib/Tmdb/Api/Lists.php index b9d0db54..e4f81817 100644 --- a/lib/Tmdb/Api/Lists.php +++ b/lib/Tmdb/Api/Lists.php @@ -33,12 +33,15 @@ public function getList($list_id, array $parameters = array(), array $headers = /** * This method lets users create a new list. A valid session id is required. * - * @throws NotImplementedException + * @param string $name + * @param string $description + * @param array $parameters + * @param array $headers * @return mixed */ - public function createList() + public function createList($name, $description, array $parameters = array(), array $headers = array()) { - throw new NotImplementedException(__METHOD__ . ' has not been implemented yet.'); + return $this->postJson('list', array('name' => $name, 'description' => $description), $parameters, $headers); } /** @@ -57,33 +60,35 @@ public function getItemStatus($list_id, array $parameters = array(), array $head /** * Get the cast information for a specific list id. * - * @throws NotImplementedException + * @param integer $id + * @param string $mediaId * @return mixed */ - public function addMediaToList() + public function addMediaToList($id, $mediaId) { - throw new NotImplementedException(__METHOD__ . ' has not been implemented yet.'); + return $this->postJson('list/' . $id . '/add_item', array('media_id' => $mediaId)); } /** * Get the images (posters and backdrops) for a specific list id. * - * @throws NotImplementedException + * @param integer $id + * @param string $mediaId * @return mixed */ - public function removeMediaFromList() + public function removeMediaFromList($id, $mediaId) { - throw new NotImplementedException(__METHOD__ . ' has not been implemented yet.'); + return $this->postJson('list/' . $id . '/remove_item', array('media_id' => $mediaId)); } /** * Get the plot keywords for a specific list id. * - * @throws NotImplementedException + * @param integer $id * @return mixed */ - public function deleteList() + public function deleteList($id) { - throw new NotImplementedException(__METHOD__ . ' has not been implemented yet.'); + return $this->delete('list/' . $id); } } diff --git a/lib/Tmdb/Api/Movies.php b/lib/Tmdb/Api/Movies.php index c1e90fab..cb7667c2 100644 --- a/lib/Tmdb/Api/Movies.php +++ b/lib/Tmdb/Api/Movies.php @@ -254,6 +254,6 @@ public function getAccountStates($id) */ public function rateMovie($id, $rating) { - return $this->post('movie/' . $id . '/rating', json_encode(array('value' => (float) $rating))); + return $this->postJson('movie/' . $id . '/rating', array('value' => (float) $rating)); } } diff --git a/lib/Tmdb/HttpClient/HttpClient.php b/lib/Tmdb/HttpClient/HttpClient.php index 5eab9c77..66009723 100644 --- a/lib/Tmdb/HttpClient/HttpClient.php +++ b/lib/Tmdb/HttpClient/HttpClient.php @@ -110,6 +110,18 @@ public function post($path, $postBody, array $parameters = array(), array $heade ); } + /** + * {@inheritDoc} + */ + public function postJson($path, $postBody, array $parameters = array(), array $headers = array()) + { + $parameters = $this->buildQueryParameters($parameters); + $request = $this->client->post($path, $headers, null, $parameters); + $request->setBody($postBody, 'application/json'); + + return $this->request($request); + } + /** * {@inheritDoc} */ diff --git a/lib/Tmdb/HttpClient/HttpClientInterface.php b/lib/Tmdb/HttpClient/HttpClientInterface.php index f844d1f0..7b8b6e9b 100644 --- a/lib/Tmdb/HttpClient/HttpClientInterface.php +++ b/lib/Tmdb/HttpClient/HttpClientInterface.php @@ -41,6 +41,18 @@ public function get($path, array $parameters = array(), array $headers = array() */ public function post($path, $postBody, array $parameters = array(), array $headers = array()); + /** + * Compose a POST request but json_encode the body + * + * @param string $path Request path + * @param array $postBody The post BODY + * @param array $parameters POST Parameters + * @param array $headers Reconfigure the request headers for this call only + * + * @return Response Data + */ + public function postJson($path, $postBody, array $parameters = array(), array $headers = array()); + /** * Compose a PATCH request *