Skip to content

Commit

Permalink
Implementing remaining methods
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Feb 25, 2014
1 parent 2a16170 commit 5e0541d
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 22 deletions.
22 changes: 22 additions & 0 deletions lib/Tmdb/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
12 changes: 4 additions & 8 deletions lib/Tmdb/Api/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*/
namespace Tmdb\Api;

use Tmdb\Exception\NotImplementedException;

class Account
extends AbstractApi
{
Expand Down Expand Up @@ -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
)));
));
}

/**
Expand Down Expand Up @@ -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
)));
));
}
}
1 change: 0 additions & 1 deletion lib/Tmdb/Api/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
29 changes: 17 additions & 12 deletions lib/Tmdb/Api/Lists.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion lib/Tmdb/Api/Movies.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
12 changes: 12 additions & 0 deletions lib/Tmdb/HttpClient/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down
12 changes: 12 additions & 0 deletions lib/Tmdb/HttpClient/HttpClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit 5e0541d

Please sign in to comment.