2014-03-29 00:23:55 -04:00
|
|
|
from requests.sessions import Session
|
|
|
|
|
|
|
|
class CacheControlSession(Session):
|
2014-03-30 00:54:23 -04:00
|
|
|
def __init__(self):
|
|
|
|
super(CacheControlSession, self).__init__()
|
2014-03-29 00:23:55 -04:00
|
|
|
|
2014-03-30 00:54:23 -04:00
|
|
|
def get(self, *args, **kw):
|
2014-03-29 00:23:55 -04:00
|
|
|
# auto-cache response
|
|
|
|
self.cache_auto = False
|
2014-03-30 01:02:32 -04:00
|
|
|
if kw.get('cache_auto'):
|
2014-03-29 00:23:55 -04:00
|
|
|
self.cache_auto = kw.pop('cache_auto')
|
|
|
|
|
|
|
|
# urls allowed to cache
|
2014-03-30 00:54:23 -04:00
|
|
|
self.cache_urls = []
|
2014-03-30 01:02:32 -04:00
|
|
|
if kw.get('cache_urls'):
|
2014-03-30 00:54:23 -04:00
|
|
|
self.cache_urls = [str(args[0])] + kw.pop('cache_urls')
|
2014-03-29 00:23:55 -04:00
|
|
|
|
2014-03-29 05:49:51 -04:00
|
|
|
# timeout for cached responses
|
2014-03-29 00:23:55 -04:00
|
|
|
self.cache_max_age = None
|
2014-03-30 01:02:32 -04:00
|
|
|
if kw.get('cache_max_age'):
|
2014-03-29 00:23:55 -04:00
|
|
|
self.cache_max_age = int(kw.pop('cache_max_age'))
|
|
|
|
|
2014-03-30 00:54:23 -04:00
|
|
|
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
|