def test_exception_is_not_propagated_but_logged(self, logging_mock): exception_message = "Capacity Error" self.api.GetUserTimeline.side_effect = twitter.TwitterError(exception_message) output, context = self.render_template( template="""{% load twitter_tag %}{% get_tweets for "twitter" as tweets %}""") self.assertEqual(output, '') self.assertEqual(context['tweets'], []) logging_mock.assert_called_with(self.logger_name) logging_mock.return_value.error.assert_called_with(exception_message)
def PostUpdate(self, text): if 'URLError' in text: raise URLError("URL Error") if 'Timeout' in text: raise HTTPError("http://none.com", 408, "Timeout", {}, None) elif 'Error' in text: raise twitter.TwitterError("Internal Error") else: persistentTwitter.storeMessage(text)
def GetFavorites(api, user=None, page=None): if user: url = 'http://twitter.com/favorites/%s.json' % user elif not user and not api._username: raise twitter.TwitterError( "User must be specified if API is not authenticated.") else: url = 'http://twitter.com/favorites.json' parameters = {} if page: parameters['page'] = page json = api._FetchUrl(url, parameters=parameters) data = simplejson.loads(json) api._CheckForTwitterError(data) return [twitter.Status.NewFromJsonDict(x) for x in data]
def test_get_from_cache_when_twitter_api_fails(self, logging_mock): exception_message = 'Technical Error' # it should be ok by now self.render_template( template="""{% load twitter_tag %}{% get_tweets for "jresig" as tweets %}""") cache_key = get_cache_key('jresig', 'tweets') self.assertEqual(len(cache.get(cache_key)), len(StubGenerator.TWEET_STUBS['jresig'])) self.assertEqual(cache.get(cache_key)[0].text, StubGenerator.TWEET_STUBS['jresig'][0]['text']) # when twitter api fails, should use cache self.api.GetUserTimeline.side_effect = twitter.TwitterError(exception_message) output, context = self.render_template( template="""{% load twitter_tag %}{% get_tweets for "jresig" as tweets %}""") self.assertEquals(len(context['tweets']), 1, 'jresig account has only one tweet') self.assertEqual(context['tweets'][0].text, StubGenerator.TWEET_STUBS['jresig'][0]['text']) logging_mock.assert_called_with(self.logger_name) logging_mock.return_value.error.assert_called_with(exception_message)
def PostDirectMessageWithImage(self, text, media_id, user_id=None, screen_name=None): """Post a twitter direct message from the authenticated user with image. Args: text: The message text to be posted. Must be less than 140 characters. user_id: The ID of the user who should receive the direct message. [Optional] screen_name: The screen name of the user who should receive the direct message. [Optional] Returns: A twitter.DirectMessage instance representing the message posted """ url = '%s/direct_messages/new.json' % self.base_url data = { 'text': text, "attachment": { "type": "media", "media": { "id": media_id } } } if user_id: data['user_id'] = user_id elif screen_name: data['screen_name'] = screen_name else: raise twitter.TwitterError( {'message': "Specify at least one of user_id or screen_name."}) resp = self._RequestUrl(url, 'POST', data=data) data = self._ParseAndCheckTwitter(resp.content.decode('utf-8')) print("Response: {}".format(data)) return twitter.DirectMessage.NewFromJsonDict(data)
def GetFriendIds(api, user_id=None, screen_name=None): '''Fetch an array of numeric IDs for every user the specified user is followed by. If called with no arguments, the results are follower IDs for the authenticated user. Note that it is unlikely that there is ever a good reason to use both of the kwargs. Args: user_id: Optional. Specfies the ID of the user for whom to return the followers list. screen_name: Optional. Specfies the screen name of the user for whom to return the followers list. ''' url = 'http://twitter.com/friends/ids.json' parameters = {} if user_id: parameters['user_id'] = user_id elif screen_name: parameters['screen_name'] = screen_name else: raise twitter.TwitterError( "One of user_id or screen_name must be specified.") json = api._FetchUrl(url, parameters=parameters) data = simplejson.loads(json) api._CheckForTwitterError(data) return data
def GetFriendIDs(self, **params: Any) -> List[str]: raise twitter.TwitterError('Rate limit exceeded')