]> jfr.im git - z_archive/twitter.git/commitdiff
Merge pull request #255 from bbolli/speedup-method-for-uri
authorBenjamin Ooghe-Tabanou <redacted>
Mon, 29 Sep 2014 00:15:33 +0000 (02:15 +0200)
committerBenjamin Ooghe-Tabanou <redacted>
Mon, 29 Sep 2014 00:15:33 +0000 (02:15 +0200)
Speedup `method_for_uri()`

tests/test_sanity.py
twitter/api.py

index 4cff03463354251630544862a1aa6b8bee17267e..83d0f09a4da3ca4a21d9c8c08d77e144a805a1b4 100644 (file)
@@ -8,7 +8,7 @@ import pickle
 import json
 
 from twitter import Twitter, NoAuth, OAuth, read_token_file, TwitterHTTPError
-from twitter.api import TwitterDictResponse, TwitterListResponse
+from twitter.api import TwitterDictResponse, TwitterListResponse, POST_ACTIONS, method_for_uri
 from twitter.cmdline import CONSUMER_KEY, CONSUMER_SECRET
 
 noauth = NoAuth()
@@ -138,3 +138,9 @@ def test_jsonifability():
     res2 = json.loads(p)
     assert res == res2
     assert res2[2] == 3
+
+
+def test_method_for_uri():
+    for action in POST_ACTIONS:
+        assert method_for_uri(get_random_str() + '/' + action) == 'POST'
+    assert method_for_uri('statuses/timeline') == 'GET'
index 861d4dfde7a5b25ed7c506632dd2ec94cd44d735..fd33edd343e3ec6dc84ce17d45b7bacfff97f14c 100644 (file)
@@ -129,13 +129,13 @@ def wrap_response(response, headers):
         res = response
     return res
 
+
+POST_ACTIONS_RE = re.compile('(' + '|'.join(POST_ACTIONS) + r')(/\d+)?$')
+
 def method_for_uri(uri):
-    method = "GET"
-    for action in POST_ACTIONS:
-        if re.search("%s(/\d+)?$" % action, uri):
-            method = "POST"
-            break
-    return method
+    if POST_ACTIONS_RE.search(uri):
+        return "POST"
+    return "GET"
 
 class TwitterCall(object):