7 changed files with 45 additions and 39 deletions
@ -1,23 +1,34 @@ |
|||
from requests.sessions import Session |
|||
|
|||
class CacheControlSession(Session): |
|||
def __init__(self, *args, **kw): |
|||
super(CacheControlSession, self).__init__(*args, **kw) |
|||
def __init__(self): |
|||
super(CacheControlSession, self).__init__() |
|||
|
|||
def request(self, *args, **kw): |
|||
def get(self, *args, **kw): |
|||
# auto-cache response |
|||
self.cache_auto = False |
|||
if kw.has_key('cache_auto'): |
|||
self.cache_auto = kw.pop('cache_auto') |
|||
|
|||
# urls allowed to cache |
|||
self.cache_urls = None |
|||
self.cache_urls = [] |
|||
if kw.has_key('cache_urls'): |
|||
self.cache_urls = [str(args[1])] + kw.pop('cache_urls') |
|||
self.cache_urls = [str(args[0])] + kw.pop('cache_urls') |
|||
|
|||
# timeout for cached responses |
|||
self.cache_max_age = None |
|||
if kw.has_key('cache_max_age'): |
|||
self.cache_max_age = int(kw.pop('cache_max_age')) |
|||
|
|||
return super(CacheControlSession, self).request(*args, **kw) |
|||
return super(CacheControlSession, self).get(*args, **kw) |
|||
|
|||
def prepare_request(self, *args, **kw): |
|||
# get response |
|||
req = super(CacheControlSession, self).prepare_request(*args, **kw) |
|||
|
|||
# attach params to request |
|||
req.cache_auto = self.cache_auto |
|||
req.cache_urls = self.cache_urls |
|||
req.cache_max_age = self.cache_max_age |
|||
|
|||
return req |
@ -1,11 +1,11 @@ |
|||
from cachecontrol.session import CacheControlSession |
|||
from cachecontrol.adapter import CacheControlAdapter |
|||
from cachecontrol.cache import DictCache |
|||
from cachecontrol.session import CacheControlSession |
|||
|
|||
def CacheControl(sess=None, cache=None, cache_etags=True): |
|||
sess = sess or CacheControlSession() |
|||
cache = cache or DictCache() |
|||
adapter = CacheControlAdapter(sess, cache, cache_etags=cache_etags) |
|||
adapter = CacheControlAdapter(cache, cache_etags=cache_etags) |
|||
sess.mount('http://', adapter) |
|||
|
|||
return sess |
|||
|
Loading…
Reference in new issue