summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoman Haritonov <reclosedev@gmail.com>2013-01-12 14:33:06 +0400
committerRoman Haritonov <reclosedev@gmail.com>2013-01-12 14:33:06 +0400
commitfc308ad1637ad75665836ef2fa24f66e4ac06438 (patch)
treef56974a3a6344e5d41dce693c81f709a863ef1ce
parentb52e21c361eec45b65cabee11ff908c1eba4920a (diff)
downloadrequests-cache-fc308ad1637ad75665836ef2fa24f66e4ac06438.tar.gz
move key creation to cache, add attributes to stored response
-rw-r--r--requests_cache/backends/base.py11
-rw-r--r--requests_cache/core.py27
-rw-r--r--tests/test_cache.py9
3 files changed, 20 insertions, 27 deletions
diff --git a/requests_cache/backends/base.py b/requests_cache/backends/base.py
index e8beb95..f52dd12 100644
--- a/requests_cache/backends/base.py
+++ b/requests_cache/backends/base.py
@@ -8,6 +8,7 @@
extended to support persistence.
"""
from datetime import datetime
+import hashlib
import requests
@@ -86,7 +87,7 @@ class BaseCache(object):
return key in self.responses or key in self.url_map
_response_attrs = ['_content', 'url', 'status_code', 'cookies',
- 'headers', 'encoding']
+ 'headers', 'encoding', 'request', 'reason']
def reduce_response(self, response):
""" Reduce response object to make it compatible with ``pickle``
@@ -108,6 +109,14 @@ class BaseCache(object):
result.history = [self.restore_response(r) for r in response.history]
return result
+ def create_key(self, request):
+ cache_data = request.method.upper() + request.url
+
+ if request.body: # body?
+ cache_data += str(request.body)
+
+ return hashlib.sha256(cache_data).hexdigest()
+
def __str__(self):
return 'urls: %s\nresponses: %s' % (self.url_map, self.responses)
diff --git a/requests_cache/core.py b/requests_cache/core.py
index 6a23927..49b0dc9 100644
--- a/requests_cache/core.py
+++ b/requests_cache/core.py
@@ -57,9 +57,11 @@ class CachedSession(Session):
def send(self, request, **kwargs):
if request.method not in self._cache_allowable_methods:
- return super(CachedSession, self).send(request, **kwargs)
+ response = super(CachedSession, self).send(request, **kwargs)
+ response.from_cache = False
+ return response
- cache_key = self._create_cache_key_from_request(request)
+ cache_key = self.cache.create_key(request)
def send_request_and_cache_response():
response = super(CachedSession, self).send(request, **kwargs)
@@ -90,30 +92,13 @@ class CachedSession(Session):
auth, timeout,
allow_redirects, proxies,
hooks, stream, verify, cert)
- main_key = self._create_cache_key_from_request(response.request)
+ main_key = self.cache.create_key(response.request)
for r in response.history:
self.cache.add_url_mapping(
- self._create_cache_key_from_request(r.request), main_key
+ self.cache.create_key(r.request), main_key
)
return response
- def _create_cache_key(self, url, params=None, data=None, method=None):
- method = method or ''
- cache_data = method.upper() + url
- if params:
- cache_data += str(params)
- if data:
- data = self._encode_params(data)
- if isinstance(data, tuple): # old requests versions
- data = data[1]
- cache_data += str(data)
-
- return hashlib.sha256(cache_data).hexdigest()
-
- def _create_cache_key_from_request(self, request):
- return self._create_cache_key(request.url,
- data=getattr(request, 'data', None),
- method=request.method)
def install_cached_session(session_factory=CachedSession):
diff --git a/tests/test_cache.py b/tests/test_cache.py
index 2eece9d..bedcc99 100644
--- a/tests/test_cache.py
+++ b/tests/test_cache.py
@@ -154,16 +154,15 @@ class CacheTestCase(unittest.TestCase):
self.assertEqual(r4, js(httpbin('cookies')))
def test_response_history(self):
- r1 = requests.get(httpbin('redirect/3'))
+ r1 = self.s.get(httpbin('redirect/3'))
def test_redirect_history(url):
- r2 = requests.get(url)
+ r2 = self.s.get(url)
for r11, r22 in zip(r1.history, r2.history):
self.assertEqual(r11.url, r22.url)
test_redirect_history(httpbin('redirect/3'))
test_redirect_history(httpbin('redirect/2'))
- with requests_cache.disabled():
- r3 = requests.get(httpbin('redirect/1'))
- self.assertEqual(len(r3.history), 1)
+ r3 = requests.get(httpbin('redirect/1'))
+ self.assertEqual(len(r3.history), 1)
def post(self, data):
return json.loads(requests.post(httpbin('post'), data=data).text)