Skip to content

Commit

Permalink
Merge pull request python-twitter-tools#100 from erfaan/master
Browse files Browse the repository at this point in the history
Fixed python-twitter-tools#99
Introduced an optional parameter `timeout` which each API request.
  • Loading branch information
sixohsix committed Nov 3, 2012
2 parents 9457b2f + 8fd7289 commit 6ef18ed
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Mark Hammond (OAuth support in API)
Prashant Pawar (IRC bot multi-channel support)
David Bittencourt (python 2.3 support)
Bryan Clark (HTTP encoding bugfix, improved exception logging)
Irfan Ahmad <http://twitter.com/erfaan> (Fixed #91 rate limit headers and #99 twitter API timeouts)
5 changes: 5 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ Examples::
# into the middle of a call. You can also use replacement:
t.user.list.members(user="tamtar", list="things-that-are-rad")

# An *optional* `_timeout` parameter can also be used for API
# calls which take much more time than normal or twitter stops
# responding for some reasone
t.users.lookup(screen_name=','.join(A_LIST_OF_100_SCREEN_NAMES), _timeout=1)


Searching Twitter::

Expand Down
20 changes: 17 additions & 3 deletions twitter/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ def __call__(self, **kwargs):
_id = kwargs.pop('_id', None)
if _id:
kwargs['id'] = _id

# If an _timeout is specified in kwargs, use it
_timeout = kwargs.pop('_timeout', None)

secure_str = ''
if self.secure:
Expand All @@ -188,11 +191,14 @@ def __call__(self, **kwargs):
body = arg_data.encode('utf8')

req = urllib_request.Request(uriBase, body, headers)
return self._handle_response(req, uri, arg_data)
return self._handle_response(req, uri, arg_data, _timeout)

def _handle_response(self, req, uri, arg_data):
def _handle_response(self, req, uri, arg_data, _timeout=None):
kwargs = {}
if _timeout:
kwargs['timeout'] = _timeout
try:
handle = urllib_request.urlopen(req)
handle = urllib_request.urlopen(req, **kwargs)
if handle.headers['Content-Type'] in ['image/jpeg', 'image/png']:
return handle
elif handle.info().get('Content-Encoding') == 'gzip':
Expand Down Expand Up @@ -256,6 +262,14 @@ class Twitter(TwitterCall):
# Note how the magic `_` method can be used to insert data
# into the middle of a call. You can also use replacement:
t.user.list.members(user="tamtar", list="things-that-are-rad")
# An *optional* `_timeout` parameter can also be used for API
# calls which take much more time than normal or twitter stops
# responding for some reasone
t.users.lookup(
screen_name=','.join(A_LIST_OF_100_SCREEN_NAMES), \
_timeout=1)
Searching Twitter::
Expand Down

0 comments on commit 6ef18ed

Please sign in to comment.