Inject the Http_Client, explicitly request the twitter_entities.
authorMichael J. Rubinsky <mrubinsk@horde.org>
Thu, 1 Jul 2010 01:18:10 +0000 (21:18 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Thu, 1 Jul 2010 01:18:10 +0000 (21:18 -0400)
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).

framework/Service_Twitter/lib/Horde/Service/Twitter.php
framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Basic.php
framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Oauth.php
horde/lib/Block/twitter_timeline.php

index f483cf2..77aaf7a 100644 (file)
@@ -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.
index d3361c2..f4dc34a 100644 (file)
@@ -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);
         }
index 2e9bd18..5845c55 100644 (file)
@@ -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);
         }
index 3a0a0e2..d1d9956 100644 (file)
@@ -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 .= ' <div class="fbstreambody">';
             $html .=  $body;
             $html .= '<div class="fbstreaminfo">' . sprintf(_("Posted %s via %s"), Horde_Date_Utils::relativeDateTime(strtotime($tweet->created_at), $GLOBALS['prefs']->getValue('date_format')), $appText) . '</div>';
+//            if (!empty($tweet->retweeted_status)) {
+//                $html .= '<div class="fbstreaminfo">' . sprintf(_("Retweeted by %s"), $tweet->user->screen_name) . '</div>';
+//            }
             $html .= '<div class="fbstreaminfo">' . Horde::link('#', '', '', '', 'Horde.twitter.buildReply(\'' . $tweet->id . '\', \'' . $tweet->user->screen_name . '\', \'' . $tweet->user->name . '\')') .  _("Reply") . '</a>';
             $html .= '&nbsp;|&nbsp;' . Horde::link('#', '', '', '', 'Horde.twitter.retweet(\'' . $tweet->id . '\')') . _("Retweet") . '</a>';
             $html .= '</div><div class="clear">&nbsp;</div></div>';
@@ -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);