Navigation Menu

Skip to content

Commit

Permalink
Fixes #99. Introduced an optional timeout parameter for each API call
Browse files Browse the repository at this point in the history
  • Loading branch information
erfaan committed Nov 1, 2012
1 parent d8441c0 commit a5aab11
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions AUTHORS
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
Expand Up @@ -110,6 +110,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
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 a5aab11

Please sign in to comment.