Repository: ucfopen/canvasapi

Issue:
Add a Requester cache for debugging
We should add a cache of requests and responses onto `Requester` to make debugging easier.

This could be as simple as a tuple recording the last request that was sent and the last response received, or we could save several requests and responses.

Discussion:

Patch:
diff --git a/canvasapi/requester.py b/canvasapi/requester.py
index eff4b3a..c017a42 100644
--- a/canvasapi/requester.py
+++ b/canvasapi/requester.py
@@ -24,6 +24,7 @@ class Requester(object):
         self.base_url = base_url
         self.access_token = access_token
         self._session = requests.Session()
+        self._cache = []
 
     def request(
             self, method, endpoint=None, headers=None, use_auth=True,
@@ -84,6 +85,12 @@ class Requester(object):
         # Call the request method
         response = req_method(full_url, headers, _kwargs)
 
+        # Add response to internal cache
+        if len(self._cache) > 4:
+            self._cache.pop()
+
+        self._cache.insert(0, response)
+
         # Raise for status codes
         if response.status_code == 400:
             raise BadRequest(response.json())


Test patch:
diff --git a/tests/test_requester.py b/tests/test_requester.py
index d65f2d4..fb582a7 100644
--- a/tests/test_requester.py
+++ b/tests/test_requester.py
@@ -77,6 +77,23 @@ class TestRequester(unittest.TestCase):
         response = self.requester.request('PUT', 'fake_put_request')
         self.assertEqual(response.status_code, 200)
 
+    def test_request_cache(self, m):
+        register_uris({'requests': ['get']}, m)
+
+        response = self.requester.request('GET', 'fake_get_request')
+        self.assertEqual(response, self.requester._cache[0])
+
+    def test_request_cache_clear_after_5(self, m):
+        register_uris({'requests': ['get', 'post']}, m)
+
+        for i in range(5):
+            self.requester.request('GET', 'fake_get_request')
+
+        response = self.requester.request('POST', 'fake_post_request')
+
+        self.assertLessEqual(len(self.requester._cache), 5)
+        self.assertEqual(response, self.requester._cache[0])
+
     def test_request_400(self, m):
         register_uris({'requests': ['400']}, m)