Skip to content

Commit

Permalink
Zend\Service: Updated all Services (not Twitter) to use Http Client d…
Browse files Browse the repository at this point in the history
…irectly
  • Loading branch information
Ralph Schindler committed Jul 11, 2012
1 parent 9702878 commit a4aabbf
Show file tree
Hide file tree
Showing 23 changed files with 780 additions and 633 deletions.
88 changes: 49 additions & 39 deletions library/Zend/Service/Delicious/Delicious.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
namespace Zend\Service\Delicious;

use DateTime;
use Zend\Rest\Client as RestClient;
use Zend\Http\Client as HttpClient;
use Zend\Http\Request as HttpRequest;

/**
* Zend_Service_Delicious is a concrete implementation of the del.icio.us web service
Expand Down Expand Up @@ -45,32 +46,30 @@ class Delicious
const JSON_URL = '/feeds/json/url/data';

/**
* Zend_Service_Rest instance
*
* @var Zend_Service_Rest
* @var HttpClient
*/
protected $_rest;
protected $httpClient = null;

/**
* Username
*
* @var string
*/
protected $_authUname;
protected $authUname;

/**
* Password
*
* @var string
*/
protected $_authPass;
protected $authPass;

/**
* Microtime of last request
*
* @var float
*/
protected static $_lastRequestTime = 0;
protected static $lastRequestTime = 0;

/**
* Constructs a new del.icio.us Web Services Client
Expand All @@ -79,32 +78,37 @@ class Delicious
* @param string $pass Client password
* @return void
*/
public function __construct($uname = null, $pass = null)
public function __construct($uname = null, $pass = null, HttpClient $httpClient = null)
{
$this->_rest = new RestClient\RestClient();
$this->_rest->getHttpClient()->setOptions(array('ssltransport' => 'ssl'));
$this->setAuth($uname, $pass);
$this->setHttpClient($httpClient ?: new HttpClient);
}

/**
* Set client username and password
*
* @param string $uname Client user name
* @param string $pass Client password
* @return Zend_Service_Delicious Provides a fluent interface
* @return Delicious Provides a fluent interface
*/
public function setAuth($uname, $pass)
{
$this->_authUname = $uname;
$this->_authPass = $pass;
$this->authUname = $uname;
$this->authPass = $pass;

return $this;
}

public function setHttpClient(HttpClient $httpClient)
{
$this->httpClient = $httpClient;
return $this;
}

/**
* Get time of the last update
*
* @throws Zend_Service_Delicious_Exception
* @throws Exception
* @return DateTime
*/
public function getLastUpdate()
Expand All @@ -128,21 +132,21 @@ public function getTags()
{
$response = $this->makeRequest(self::PATH_TAGS);

return self::_xmlResponseToArray($response, 'tags', 'tag', 'tag', 'count');
return self::xmlResponseToArray($response, 'tags', 'tag', 'tag', 'count');
}

/**
* Rename a tag
*
* @param string $old Old tag name
* @param string $new New tag name
* @return Zend_Service_Delicious Provides a fluent interface
* @return Delicious Provides a fluent interface
*/
public function renameTag($old, $new)
{
$response = $this->makeRequest(self::PATH_TAG_RENAME, array('old' => $old, 'new' => $new));

self::_evalXmlResult($response);
self::evalXmlResult($response);

return $this;
}
Expand All @@ -156,7 +160,7 @@ public function getBundles()
{
$response = $this->makeRequest(self::PATH_BUNDLES);

$bundles = self::_xmlResponseToArray($response, 'bundles', 'bundle', 'name', 'tags');
$bundles = self::xmlResponseToArray($response, 'bundles', 'bundle', 'name', 'tags');
foreach ($bundles as &$tags) {
$tags = explode(' ', $tags);
}
Expand All @@ -175,7 +179,7 @@ public function addBundle($bundle, array $tags)
$tags = implode(' ', (array) $tags);
$response = $this->makeRequest(self::PATH_BUNDLE_ADD, array('bundle' => $bundle, 'tags' => $tags));

self::_evalXmlResult($response);
self::evalXmlResult($response);

return $this;
}
Expand All @@ -190,7 +194,7 @@ public function deleteBundle($bundle)
{
$response = $this->makeRequest(self::PATH_BUNDLE_DELETE, array('bundle' => $bundle));

self::_evalXmlResult($response);
self::evalXmlResult($response);

return $this;
}
Expand All @@ -205,7 +209,7 @@ public function deletePost($url)
{
$response = $this->makeRequest(self::PATH_POST_DELETE, array('url' => $url));

self::_evalXmlResult($response);
self::evalXmlResult($response);

return $this;
}
Expand All @@ -227,7 +231,7 @@ public function getDates($tag = null)

$response = $this->makeRequest(self::PATH_DATES, $parms);

return self::_xmlResponseToArray($response, 'dates', 'date', 'date', 'count');
return self::xmlResponseToArray($response, 'dates', 'date', 'date', 'count');
}

/**
Expand Down Expand Up @@ -256,7 +260,7 @@ public function getPosts($tag = null, DateTime $dt = null, $url = null)

$response = $this->makeRequest(self::PATH_POSTS_GET, $parms);

return $this->_parseXmlPostList($response);
return $this->parseXmlPostList($response);
}

/**
Expand All @@ -274,7 +278,7 @@ public function getAllPosts($tag = null)

$response = $this->makeRequest(self::PATH_POSTS_ALL, $parms);

return $this->_parseXmlPostList($response);
return $this->parseXmlPostList($response);
}

/**
Expand All @@ -296,7 +300,7 @@ public function getRecentPosts($tag = null, $count = 15)

$response = $this->makeRequest(self::PATH_POSTS_RECENT, $parms);

return $this->_parseXmlPostList($response);
return $this->parseXmlPostList($response);
}

/**
Expand Down Expand Up @@ -419,34 +423,40 @@ public function getUrlDetails($url)
* @return mixed decoded response from web service
* @throws Zend_Service_Delicious_Exception
*/
public function makeRequest($path, array $parms = array(), $type = 'xml')
public function makeRequest($path, array $params = array(), $type = 'xml')
{
// if previous request was made less then 1 sec ago
// wait until we can make a new request
$timeDiff = microtime(true) - self::$_lastRequestTime;
$timeDiff = microtime(true) - self::$lastRequestTime;
if ($timeDiff < 1) {
usleep((1 - $timeDiff) * 1000000);
}

$this->_rest->getHttpClient()->setAuth($this->_authUname, $this->_authPass);
$this->httpClient->setAuth($this->authUname, $this->authPass);
$this->httpClient->setOptions(array('ssltransport' => 'ssl'));

$request = new HttpRequest;
$request->setMethod(HttpRequest::METHOD_GET);

switch ($type) {
case 'xml':
$this->_rest->setUri(self::API_URI);
$request->setUri(self::API_URI);
break;
case 'json':
$parms['raw'] = true;
$this->_rest->setUri(self::JSON_URI);
$params['raw'] = true;
$request->setUri(self::JSON_URI);
break;
default:
throw new Exception('Unknown request type');
}

self::$_lastRequestTime = microtime(true);
$response = $this->_rest->restGet($path, $parms);
self::$lastRequestTime = microtime(true);

$request->getQuery()->fromArray($params);
$response = $this->httpClient->send($request);

if (!$response->isSuccessful()) {
throw new Exception("Http client reported an error: '{$response->getMessage()}'");
if (!$response->isSuccess()) {
throw new Exception("Http client reported an error: '{$response->getReasonPhrase()}'");
}

$responseBody = $response->getBody();
Expand Down Expand Up @@ -476,7 +486,7 @@ public function makeRequest($path, array $parms = array(), $type = 'xml')
* @return array
* @throws Zend_Service_Delicious_Exception
*/
private static function _xmlResponseToArray(\DOMDocument $response, $root, $child, $attKey, $attValue)
private static function xmlResponseToArray(\DOMDocument $response, $root, $child, $attKey, $attValue)
{
$rootNode = $response->documentElement;
$arrOut = array();
Expand Down Expand Up @@ -504,7 +514,7 @@ private static function _xmlResponseToArray(\DOMDocument $response, $root, $chil
* @return Zend_Service_Delicious_PostList
* @throws Zend_Service_Delicious_Exception
*/
private function _parseXmlPostList(\DOMDocument $response)
private function parseXmlPostList(\DOMDocument $response)
{
$rootNode = $response->documentElement;

Expand All @@ -522,7 +532,7 @@ private function _parseXmlPostList(\DOMDocument $response)
* @return void
* @throws Zend_Service_Delicious_Exception
*/
private static function _evalXmlResult(\DOMDocument $response)
private static function evalXmlResult(\DOMDocument $response)
{
$rootNode = $response->documentElement;

Expand Down
32 changes: 16 additions & 16 deletions library/Zend/Service/Delicious/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,27 @@ class Post extends SimplePost
*
* @var Zend_Service_Delicious
*/
protected $_service;
protected $service;

/**
* @var int Number of people that have the same post
*/
protected $_others;
protected $others;

/**
* @var DateTime Post date
*/
protected $_date;
protected $date;

/**
* @var bool Post share
*/
protected $_shared = true;
protected $shared = true;

/**
* @var string Post hash
*/
protected $_hash;
protected $hash;

/**
* Constructs a new del.icio.us post
Expand All @@ -58,10 +58,10 @@ class Post extends SimplePost
*/
public function __construct(Delicious $service, $values)
{
$this->_service = $service;
$this->service = $service;

if ($values instanceof \DOMElement) {
$values = self::_parsePostNode($values);
$values = self::parsePostNode($values);
}

if (!is_array($values) || !isset($values['url']) || !isset($values['title'])) {
Expand Down Expand Up @@ -152,7 +152,7 @@ public function removeTag($tag)
*/
public function getDate()
{
return $this->_date;
return $this->date;
}

/**
Expand All @@ -166,7 +166,7 @@ public function getDate()
*/
public function getOthers()
{
return $this->_others;
return $this->others;
}

/**
Expand All @@ -176,7 +176,7 @@ public function getOthers()
*/
public function getHash()
{
return $this->_hash;
return $this->hash;
}

/**
Expand All @@ -186,7 +186,7 @@ public function getHash()
*/
public function getShared()
{
return $this->_shared;
return $this->shared;
}

/**
Expand All @@ -197,7 +197,7 @@ public function getShared()
*/
public function setShared($isShared)
{
$this->_shared = (bool) $isShared;
$this->shared = (bool) $isShared;

return $this;
}
Expand All @@ -209,7 +209,7 @@ public function setShared($isShared)
*/
public function delete()
{
return $this->_service->deletePost($this->_url);
return $this->service->deletePost($this->_url);
}

/**
Expand All @@ -223,12 +223,12 @@ public function save()
'url' => $this->_url,
'description'=> $this->_title,
'extended' => $this->_notes,
'shared' => ($this->_shared ? 'yes' : 'no'),
'shared' => ($this->shared ? 'yes' : 'no'),
'tags' => implode(' ', (array) $this->_tags),
'replace' => 'yes'
);

return $this->_service->makeRequest(Delicious::PATH_POSTS_ADD, $parms);
return $this->service->makeRequest(Delicious::PATH_POSTS_ADD, $parms);
}

/**
Expand All @@ -237,7 +237,7 @@ public function save()
* @param DOMElement $node
* @return array
*/
protected static function _parsePostNode(\DOMElement $node)
protected static function parsePostNode(\DOMElement $node)
{
return array(
'url' => $node->getAttribute('href'),
Expand Down
Loading

0 comments on commit a4aabbf

Please sign in to comment.