Skip to content

Commit

Permalink
First working version of communication with account
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Feb 25, 2014
1 parent b8f2174 commit 401550b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
24 changes: 24 additions & 0 deletions examples/account/api/account.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
require_once('../../../vendor/autoload.php');
require_once('../../../apikey.php');

$token = new \Tmdb\ApiToken(TMDB_API_KEY);
$client = new \Tmdb\Client($token);

$sessionToken = new \Tmdb\SessionToken(TMDB_SESSION_TOKEN);
$client->setSessionToken($sessionToken);

$account = $client->getAccountApi()->getAccount();

var_dump($account);
7 changes: 4 additions & 3 deletions lib/Tmdb/Api/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ class Account
/**
* Get the basic information for an account. You will need to have a valid session id.
*
* @throws NotImplementedException
* @param array $parameters
* @param array $headers
* @return mixed
*/
public function getAccount()
public function getAccount(array $parameters = array(), array $headers = array())
{
throw new NotImplementedException(__METHOD__ . ' has not been implemented yet.');
return $this->get('account', $parameters, $headers);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions lib/Tmdb/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Tmdb\ApiToken as Token;
use Tmdb\HttpClient\Plugin\AcceptJsonHeaderPlugin;
use Tmdb\HttpClient\Plugin\ApiTokenPlugin;
use Tmdb\HttpClient\Plugin\SessionTokenPlugin;

/**
* Simple wrapper for the Tmdb API
Expand Down Expand Up @@ -336,6 +337,11 @@ public function getSecure()
*/
public function setSessionToken($sessionToken)
{
if ($this->httpClient->getClient() instanceof \Guzzle\Common\HasDispatcherInterface) {
$sessionTokenPlugin = new SessionTokenPlugin($sessionToken);
$this->httpClient->getClient()->addSubscriber($sessionTokenPlugin);
}

$this->sessionToken = $sessionToken;
return $this;
}
Expand Down
44 changes: 44 additions & 0 deletions lib/Tmdb/HttpClient/Plugin/SessionTokenPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\HttpClient\Plugin;

use Guzzle\Common\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Tmdb\SessionToken;

class SessionTokenPlugin implements EventSubscriberInterface
{
/**
* @var \Tmdb\ApiToken
*/
private $token;

public function __construct(SessionToken $token)
{
$this->token = $token;
}

public static function getSubscribedEvents()
{
return array('request.before_send' => 'onBeforeSend');
}

public function onBeforeSend(Event $event)
{
$url = $event['request']->getUrl(true);

$url->getQuery()->set('session_id', $this->token->getToken());

$event['request']->setUrl($url);
}
}

0 comments on commit 401550b

Please sign in to comment.