Skip to content
This repository

Fixed #99 #100

Merged
merged 2 commits into from over 1 year ago

2 participants

Irfan Ahmad Mike Verdone
Irfan Ahmad

Introduced an optional parameter timeout which each API request.

Mike Verdone
Owner

Hi,

the feature looks good, but special parameters to twitter calls are usually marked by leading underscores. e.g. _id and _method so that they do not block any parameter names Twitter decides to use. I would prefer this parameter be called _timeout. If you make that change I will definitely accept this pull request.

Thanks,

Mike.

Irfan Ahmad

Hi Mike,
I completely agree with your argument. Please check the updated code.

Thanks,
Irfan

Mike Verdone
Owner

Quick coding! Thanks very much for your work.

Mike Verdone sixohsix merged commit 6ef18ed into from November 03, 2012
Mike Verdone sixohsix closed this November 03, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Nov 01, 2012
Irfan Ahmad Fixes #99. Introduced an optional timeout parameter for each API call a5aab11
Nov 04, 2012
Irfan Ahmad Changed special parameter timeout to _timeout so that it doesn't bloc…
…k any parameter names Twitter decides to use
8fd7289
This page is out of date. Refresh to see the latest.
1  AUTHORS
@@ -11,3 +11,4 @@ Mark Hammond (OAuth support in API)
11 11
 Prashant Pawar (IRC bot multi-channel support)
12 12
 David Bittencourt (python 2.3 support)
13 13
 Bryan Clark (HTTP encoding bugfix, improved exception logging)
  14
+Irfan Ahmad <http://twitter.com/erfaan> (Fixed #91 rate limit headers and #99 twitter API timeouts)
5  README
@@ -110,6 +110,11 @@ Examples::
110 110
     # into the middle of a call. You can also use replacement:
111 111
     t.user.list.members(user="tamtar", list="things-that-are-rad")
112 112
 
  113
+    # An *optional* `_timeout` parameter can also be used for API
  114
+    # calls which take much more time than normal or twitter stops
  115
+    # responding for some reasone
  116
+    t.users.lookup(screen_name=','.join(A_LIST_OF_100_SCREEN_NAMES), _timeout=1)
  117
+
113 118
 
114 119
 Searching Twitter::
115 120
 
20  twitter/api.py
@@ -167,6 +167,9 @@ def __call__(self, **kwargs):
167 167
         _id = kwargs.pop('_id', None)
168 168
         if _id:
169 169
             kwargs['id'] = _id
  170
+        
  171
+        # If an _timeout is specified in kwargs, use it
  172
+        _timeout = kwargs.pop('_timeout', None)
170 173
 
171 174
         secure_str = ''
172 175
         if self.secure:
@@ -188,11 +191,14 @@ def __call__(self, **kwargs):
188 191
                 body = arg_data.encode('utf8')
189 192
 
190 193
         req = urllib_request.Request(uriBase, body, headers)
191  
-        return self._handle_response(req, uri, arg_data)
  194
+        return self._handle_response(req, uri, arg_data, _timeout)
192 195
 
193  
-    def _handle_response(self, req, uri, arg_data):
  196
+    def _handle_response(self, req, uri, arg_data, _timeout=None):
  197
+        kwargs = {}
  198
+        if _timeout:
  199
+            kwargs['timeout'] = _timeout
194 200
         try:
195  
-            handle = urllib_request.urlopen(req)
  201
+            handle = urllib_request.urlopen(req, **kwargs)
196 202
             if handle.headers['Content-Type'] in ['image/jpeg', 'image/png']:
197 203
                 return handle
198 204
             elif handle.info().get('Content-Encoding') == 'gzip':
@@ -256,6 +262,14 @@ class Twitter(TwitterCall):
256 262
         # Note how the magic `_` method can be used to insert data
257 263
         # into the middle of a call. You can also use replacement:
258 264
         t.user.list.members(user="tamtar", list="things-that-are-rad")
  265
+        
  266
+        # An *optional* `_timeout` parameter can also be used for API
  267
+        # calls which take much more time than normal or twitter stops
  268
+        # responding for some reasone
  269
+        t.users.lookup(
  270
+            screen_name=','.join(A_LIST_OF_100_SCREEN_NAMES), \
  271
+            _timeout=1)
  272
+
259 273
 
260 274
 
261 275
     Searching Twitter::
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.