Add API.cached_result.
authorJoshua Roesslein <jroesslein@gmail.com>
Sun, 22 Dec 2013 01:42:09 +0000 (17:42 -0800)
committerJoshua Roesslein <jroesslein@gmail.com>
Sun, 22 Dec 2013 01:42:09 +0000 (17:42 -0800)
This will be True if the last result was cached.
Otherwise this will be False if the result was freshly
requested from the Twitter API servers.

tests/test_api.py
tweepy/binder.py

index d83a844e66eed13b2652f4faee2bb193f1891ce5..c21a11edd5b24b21aba3000f774166ad0390ba0a 100644 (file)
@@ -326,6 +326,13 @@ class TweepyAPITests(TweepyTestCase):
         }
         self.assertTrue(expected_dict in languages)
 
+    def testcachedresult(self):
+        self.api.cache = MemoryCache()
+        self.api.home_timeline()
+        self.assertFalse(self.api.cached_result)
+        self.api.home_timeline()
+        self.assertTrue(self.api.cached_result)
+
 class TweepyCacheTests(unittest.TestCase):
 
     timeout = 2.0
index 6c588478c79623835c98b88e722ce394d2a844a9..29eacaedf2d5d50040ddbeb464decd1f90f87a98 100644 (file)
@@ -104,6 +104,8 @@ def bind_api(**config):
                 self.path = self.path.replace(variable, value)
 
         def execute(self):
+            self.api.cached_result = False
+
             # Build the request URL
             url = self.api_root + self.path
             if len(self.parameters):
@@ -123,6 +125,7 @@ def bind_api(**config):
                     else:
                         if isinstance(cache_result, Model):
                             cache_result._api = self.api
+                    self.api.cached_result = True
                     return cache_result
 
             # Continue attempting request until successful