Skip to content

Commit

Permalink
Adding secure schema support, and removing un-used options support.
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Jan 18, 2014
1 parent a5a61ce commit 9920fa6
Showing 1 changed file with 51 additions and 29 deletions.
80 changes: 51 additions & 29 deletions lib/Tmdb/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,20 @@
* @package Tmdb
*/
class Client {
const TMDB_URI = 'http://api.themoviedb.org/3/';
/**
* Base API URI
*/
const TMDB_URI = '//api.themoviedb.org/3/';

/**
* Insecure schema
*/
const SCHEME_INSECURE = 'http';

/**
* Secure schema
*/
const SCHEME_SECURE = 'https';

/**
* Stores API authentication token
Expand All @@ -38,24 +51,33 @@ class Client {
*/
private $token;

/**
* Whether the request is supposed to use a secure schema
*
* @var bool
*/
private $secure = false;

/**
* Stores the HTTP Client
*
* @var HttpClientInterface
*/
private $httpClient;

private $options = array();

/**
* Construct our client
*
* @param ClientInterface $httpClient
* @param Token $token
* @param ClientInterface|null $httpClient
* @param Token|null $token
* @param boolean $secure
*/
public function __construct(Token $token, ClientInterface $httpClient = null)
public function __construct(Token $token, ClientInterface $httpClient = null, $secure = false)
{
$httpClient = $httpClient ?: new GuzzleClient(self::TMDB_URI);
$this->setToken($token);
$this->setSecure($secure);

$httpClient = $httpClient ?: new GuzzleClient($this->getBaseUrl());

if ($httpClient instanceof \Guzzle\Common\HasDispatcherInterface) {
$apiTokenPlugin = new ApiTokenPlugin($token);
Expand All @@ -65,8 +87,7 @@ public function __construct(Token $token, ClientInterface $httpClient = null)
$httpClient->addSubscriber($acceptJsonHeaderPlugin);
}

$this->httpClient = new HttpClient(self::TMDB_URI, array(), $httpClient);
$this->setToken($token);
$this->httpClient = new HttpClient($this->getBaseUrl(), array(), $httpClient);
}

/**
Expand Down Expand Up @@ -258,34 +279,35 @@ public function setHeaders(array $headers)
}

/**
* @param string $name
* Return the base url with preferred schema
*
* @return mixed
*
* @throws InvalidArgumentException
* @return string
*/
public function getOption($name)
private function getBaseUrl()
{
if (!array_key_exists($name, $this->options)) {
throw new InvalidArgumentException(sprintf('Undefined option called: "%s"', $name));
}

return $this->options[$name];
return sprintf(
'%s:%s',
$this->getSecure() ? self::SCHEME_SECURE : self::SCHEME_INSECURE,
self::TMDB_URI
);
}

/**
* @param string $name
* @param mixed $value
*
* @throws InvalidArgumentException
* @throws InvalidArgumentException
* @param boolean $secure
* @return $this
*/
public function setOption($name, $value)
public function setSecure($secure)
{
if (!array_key_exists($name, $this->options)) {
throw new InvalidArgumentException(sprintf('Undefined option called: "%s"', $name));
}
$this->secure = $secure;
return $this;
}

$this->options[$name] = $value;
/**
* @return boolean
*/
public function getSecure()
{
return $this->secure;
}

}

3 comments on commit 9920fa6

@Jip-Hop
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this make it possible to also get https urls to images? Currently I get links like this:

["http://image.tmdb.org/t/p/h632/rX4LRmkYshMRfQ6lVbeZVAfqVKI.jpg", "http://image.tmdb.org/t/p/h632/mv2DgT9RABBYCxOY4ZHYalYBiUt.jpg", "http://image.tmdb.org/t/p/h632/eZBAmGqwG2q2bFWD3xLTLt69QAb.jpg", "http://image.tmdb.org/t/p/h632/fxOwiq2A41huXU9ltq4zAhpSiEG.jpg"]

@wtfzdotnet
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it is direct communication only, if you are making use of the image helper it would require a change in the class.

There's a secure_base_url now.

@wtfzdotnet
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best way to go I think is to just strip the http: part and leave the protocol up to the browser.

Please sign in to comment.