From 7fcd5aba11fdba3289dc1011f86edcbd7a924cd6 Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Wed, 30 Jun 2010 21:18:10 -0400 Subject: [PATCH] Inject the Http_Client, explicitly request the twitter_entities. Use a http_client configured with Horde defaults (fixes twitter requests not working when using a proxy). Also add twitter_entities to the request, until they are included by default in a future twitter API update). --- .../Service_Twitter/lib/Horde/Service/Twitter.php | 27 ++++++++++++++++++++++ .../lib/Horde/Service/Twitter/Request/Basic.php | 5 ++-- .../lib/Horde/Service/Twitter/Request/Oauth.php | 7 ++---- horde/lib/Block/twitter_timeline.php | 8 ++++++- 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/framework/Service_Twitter/lib/Horde/Service/Twitter.php b/framework/Service_Twitter/lib/Horde/Service/Twitter.php index f483cf2e7..77aaf7a0f 100644 --- a/framework/Service_Twitter/lib/Horde/Service/Twitter.php +++ b/framework/Service_Twitter/lib/Horde/Service/Twitter.php @@ -70,6 +70,13 @@ class Horde_Service_Twitter protected $_request; /** + * Hold the http client. + * + * @var Horde_Http_Client + */ + protected $_httpClient; + + /** * Const'r * * @param array $config Configuration parameters: @@ -115,6 +122,26 @@ class Horde_Service_Twitter } /** + * Set the http client. + * + * @param Horde_Http_Client $client The http client + */ + public function setHttpClient(Horde_Http_Client $client) + { + $this->_httpClient = $client; + } + + /** + * Get the http client. + * + * @return Horde_Http_Client + */ + public function getHttpClient() + { + return $this->_httpClient; + } + + /** * Lazy load the twitter classes. * * @param string $value The lowercase representation of the subclass. diff --git a/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Basic.php b/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Basic.php index d3361c2e1..f4dc34a5c 100644 --- a/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Basic.php +++ b/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Basic.php @@ -43,9 +43,8 @@ class Horde_Service_Twitter_Request_Basic extends Horde_Service_Twitter_Request if (!empty($cache) && $results = $cache->get($key, $this->_twitter->cacheLifetime)) { return $results; } - $client = new Horde_Http_Client(); try { - $response = $client->get($url, array('Authorization' => $this->_twitter->auth->buildAuthorizationHeader())); + $response = $this->_twitter->getHttpClient()->get($url, array('Authorization' => $this->_twitter->auth->buildAuthorizationHeader())); } catch (Horde_Http_Exception $e) { throw new Horde_Service_Twitter_Exception($e); } @@ -70,7 +69,7 @@ class Horde_Service_Twitter_Request_Basic extends Horde_Service_Twitter_Request { $client = new Horde_Http_Client(); try { - $response = $client->post($url, $params, array('Authorization' => $this->_twitter->auth->buildAuthorizationHeader())); + $response = $this->_twitter->getHttpClient()->post($url, $params, array('Authorization' => $this->_twitter->auth->buildAuthorizationHeader())); } catch (Horde_Http_Exception $e) { throw new Horde_Service_Twitter_Exception($e); } diff --git a/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Oauth.php b/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Oauth.php index 2e9bd1878..5845c5552 100644 --- a/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Oauth.php +++ b/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Oauth.php @@ -47,11 +47,10 @@ class Horde_Service_Twitter_Request_Oauth extends Horde_Service_Twitter_Request $request->sign($this->_twitter->auth->oauth->signatureMethod, $this->_twitter->auth->oauth, $this->_twitter->auth->getAccessToken()); - $client = new Horde_Http_Client(); $url = ($url instanceof Horde_Url) ? $url : new Horde_Url($url); $url->add($params); try { - $response = $client->get($url, array('Authorization' => $request->buildAuthorizationHeader('Twitter API'))); + $response = $this->_twitter->getHttpClient()->get($url, array('Authorization' => $request->buildAuthorizationHeader('Twitter API'))); } catch (Horde_Http_Exception $e) { throw new Horde_Service_Twitter_Exception($e); } @@ -79,10 +78,8 @@ class Horde_Service_Twitter_Request_Oauth extends Horde_Service_Twitter_Request $request->sign($this->_twitter->auth->oauth->signatureMethod, $this->_twitter->auth->oauth, $this->_twitter->auth->getAccessToken()); - - $client = new Horde_Http_Client(); try { - $response = $client->post($url, $params, array('Authorization' => $request->buildAuthorizationHeader('Twitter API'))); + $response = $this->_twitter->getHttpClient()->post($url, $params, array('Authorization' => $request->buildAuthorizationHeader('Twitter API'))); } catch (Horde_Http_Exception $e) { throw new Horde_Service_Twitter_Exception($e); } diff --git a/horde/lib/Block/twitter_timeline.php b/horde/lib/Block/twitter_timeline.php index 3a0a0e2e7..d1d99564c 100644 --- a/horde/lib/Block/twitter_timeline.php +++ b/horde/lib/Block/twitter_timeline.php @@ -127,7 +127,7 @@ class Horde_Block_Horde_twitter_timeline extends Horde_Block /* Fetch the stream data */ try { - $stream = Horde_Serialize::unserialize($twitter->statuses->homeTimeline(), Horde_Serialize::JSON); + $stream = Horde_Serialize::unserialize($twitter->statuses->homeTimeline(array('include_entities' => 'true')), Horde_Serialize::JSON); } catch (Horde_Service_Twitter_Exception $e) { $msg = Horde_Serialize::unserialize($e->getMessage(), Horde_Serialize::JSON); return $msg @@ -174,6 +174,9 @@ class Horde_Block_Horde_twitter_timeline extends Horde_Block $html .= '
'; $html .= $body; $html .= '
' . sprintf(_("Posted %s via %s"), Horde_Date_Utils::relativeDateTime(strtotime($tweet->created_at), $GLOBALS['prefs']->getValue('date_format')), $appText) . '
'; +// if (!empty($tweet->retweeted_status)) { +// $html .= '
' . sprintf(_("Retweeted by %s"), $tweet->user->screen_name) . '
'; +// } $html .= '
' . Horde::link('#', '', '', '', 'Horde.twitter.buildReply(\'' . $tweet->id . '\', \'' . $tweet->user->screen_name . '\', \'' . $tweet->user->name . '\')') . _("Reply") . ''; $html .= ' | ' . Horde::link('#', '', '', '', 'Horde.twitter.retweet(\'' . $tweet->id . '\')') . _("Retweet") . ''; $html .= '
 
'; @@ -315,8 +318,11 @@ EOF; $oauth = new Horde_Oauth_Consumer($params); /* Create the Twitter client */ + // @TODO: use a binder - especially once we start integrating other + // apps with Twitter $twitter = new Horde_Service_Twitter(array('oauth' => $oauth, 'cache' => $cache)); + $twitter->setHttpClient($GLOBALS['injector']->getInstance('Horde_Http_Client')); $auth_token = new Horde_Oauth_Token($token['key'], $token['secret']); $twitter->auth->setToken($auth_token); -- 2.11.0