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