Permalink
Browse files

Fixes #99. Introduced an optional timeout parameter for each API call

  • Loading branch information...
1 parent d8441c0 commit a5aab114c7336ba11de71550d36957302e04d994 @erfaan erfaan committed Nov 1, 2012
Showing with 23 additions and 3 deletions.
  1. +1 −0 AUTHORS
  2. +5 −0 README
  3. +17 −3 twitter/api.py
View
@@ -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)
View
@@ -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::
View
@@ -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:
@@ -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':
@@ -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::

0 comments on commit a5aab11

Please sign in to comment.