本文整理汇总了Python中cachecontrol.CacheControl类的典型用法代码示例。如果您正苦于以下问题:Python CacheControl类的具体用法?Python CacheControl怎么用?Python CacheControl使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CacheControl类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: sess
def sess(self, url, tmpdir):
self.url = url
self.cache = FileCache(str(tmpdir))
sess = CacheControl(requests.Session(), cache=self.cache)
yield sess
# closing session object
sess.close()
示例2: all_sites
def all_sites(sitemap_url='http://library.link/harvest/sitemap.xml'):
'''
>>> from librarylink.util import all_sites
>>> [ s.host for s in all_sites() if 'denverlibrary' in s.host ]
['link.denverlibrary.org']
'''
#FIXME: Avoid accumulating all the nodes, which will require improvements to xml.treesequence
@coroutine
def sink(accumulator):
while True:
e = yield
loc = next(select_name(e, 'loc'))
lastmod = next(select_name(e, 'lastmod'))
s = liblink_site()
s.sitemap = loc.xml_value
s.url, _, tail = s.sitemap.partition('harvest/sitemap.xml')
s.base_url = s.url #Legacy property name
#Early warning for funky URLs breaking stuff downstream
assert not tail
protocol, s.host, path, query, fragment = iri.split_uri_ref(s.sitemap)
s.lastmod = lastmod.xml_value
accumulator.append(s)
nodes = []
ts = xml.treesequence(('sitemapindex', 'sitemap'), sink(nodes))
if hasattr (all_sites, 'cachedir'):
sess = CacheControl(requests.Session(), cache=FileCache(all_sites.cachedir))
else:
sess = CacheControl(requests.Session())
result = sess.get(sitemap_url)
ts.parse(result.text)
yield from nodes
示例3: get_reader
def get_reader(self):
sess = CacheControl(requests.Session(),
cache=FileCache(gettempdir()))
req = sess.get(self.file)
# if the response is not 200, an exception will be raised
req.raise_for_status()
return io.BufferedReader(io.BytesIO(req.content))
示例4: sess
def sess(self, url):
self.etag_url = urljoin(url, "/etag")
self.update_etag_url = urljoin(url, "/update_etag")
self.cache = DictCache()
sess = CacheControl(
requests.Session(), cache=self.cache, serializer=NullSerializer()
)
yield sess
# closing session object
sess.close()
示例5: get_cached_session
def get_cached_session():
sess = CacheControl(requests.Session(),
cache=FileCache(CACHE_DIR), heuristic=LastModifiedNoDate(require_date=False))
original_get = sess.get
def wrapped_get(*args, **kwargs):
try:
return original_get(*args, **kwargs)
except (OSError, IOError) as e:
return requests.get(*args, **kwargs)
sess.get = wrapped_get
return sess
示例6: send_answer
def send_answer(self, answer, key):
s = requests.session()
s = CacheControl(s, cache=FileCache(os.path.expanduser('~/.tst/cache')))
url = "%s/%s/answers" % (self.url, key)
data = data2json(answer).encode('utf-8')
tokens = JsonFile(os.path.expanduser('~/.tst/tokens.json'))
headers = {"Authorization": "Bearer %s" % tokens.get(self.name)}
try:
response = s.post(url, headers=headers, data=data, allow_redirects=True)
except requests.ConnectionError:
_assert(False, "Connection failed... check your internet connection (1)")
return response
示例7: fetch_file
def fetch_file(url, encoding=None):
s = requests.session()
s = CacheControl(s, cache=FileCache(os.path.expanduser('~/.tst/cache')))
try:
response = s.get(url, headers={})
except requests.ConnectionError:
_assert(False, "Connection failed... check your internet connection")
_assert(response.ok, "%s\nFile request failed: %s (%d)" % (url, response.reason, response.status_code))
if encoding:
response.encoding = encoding
return response.text
示例8: TestStream
class TestStream(object):
def setup(self):
self.sess = CacheControl(requests.Session())
def test_stream_is_cached(self, url):
resp_1 = self.sess.get(url + 'stream')
content_1 = resp_1.content
resp_2 = self.sess.get(url + 'stream')
content_2 = resp_1.content
assert not resp_1.from_cache
assert resp_2.from_cache
assert content_1 == content_2
示例9: JSONLocator
class JSONLocator(Locator):
def __init__(self, url=PYPI_JSON_URL):
self.url = url
self.session = CacheControl(requests.session())
def versions(self, distribution):
url = "{}/{}/json".format(self.url, distribution)
response = self.session.get(url)
ret = []
j = response.json()['releases']
return [v for v, d in j.items() if len(d) > 0]
def get(self, distribution, version):
url = "{}/{}/json".format(self.url, distribution)
response = self.session.get(url)
# Reformat the data...
return response.json()['releases'][version]
示例10: test_not_modified_releases_connection
def test_not_modified_releases_connection(self, server):
sess = CacheControl(requests.Session())
etag_url = urljoin(server.application_url, "/etag")
sess.get(etag_url)
resp = Mock(status=304, headers={})
# This is how the urllib3 response is created in
# requests.adapters
response_mod = "requests.adapters.HTTPResponse.from_httplib"
with patch(response_mod, Mock(return_value=resp)):
sess.get(etag_url)
assert resp.read.called
assert resp.release_conn.called
示例11: __init__
def __init__(self, name, url, disable_cache=False):
if name == "pypi":
raise ValueError("The name [pypi] is reserved for repositories")
self._packages = []
self._name = name
self._url = url.rstrip("/")
self._cache_dir = Path(CACHE_DIR) / "cache" / "repositories" / name
self._cache = CacheManager(
{
"default": "releases",
"serializer": "json",
"stores": {
"releases": {"driver": "file", "path": str(self._cache_dir)},
"packages": {"driver": "dict"},
"matches": {"driver": "dict"},
},
}
)
self._session = CacheControl(
requests.session(), cache=FileCache(str(self._cache_dir / "_http"))
)
url_parts = urlparse.urlparse(self._url)
if not url_parts.username:
self._session.auth = get_http_basic_auth(self.name)
self._disable_cache = disable_cache
示例12: LDClient
class LDClient(object):
def __init__(self, apiKey, config=Config.default()):
self._apiKey = apiKey
self._config = config
self._session = CacheControl(requests.Session())
def get_flag(self, key, user, default=False):
try:
return self._get_flag(key, user, default)
except:
logging.exception('Unhandled exception in get_flag. Returning default value for flag.')
return default
def _get_flag(self, key, user, default):
hdrs = {'Authorization': 'api_key ' + self._apiKey,
'User-Agent': 'PythonClient/' + __version__}
uri = self._config._base_uri + '/api/eval/features/' + key
r = self._session.get(uri, headers=hdrs, timeout = (self._config._connect, self._config._read))
dict = r.json()
val = _evaluate(dict, user)
if val is None:
return default
else:
return val
示例13: __init__
def __init__(self):
self.s = requests.Session()
# We cache ALL responses for 60 min. so eg. inline lyrics request don't make two calls right after each other.
# This MAY have unforeseen consequences, but hopefully we can deal with those.
self.s = CacheControl(self.s, cache_etags=False, heuristic=ExpiresAfter(minutes=60))
self.s.headers.update({'Accept': 'application/json', 'User-Agent': VOCADB_USER_AGENT})
self.opts = {'nameMatchMode': 'Auto', 'getTotalCount': 'true'}
self._resources = {}
示例14: setup
def setup(self):
class DummyHeuristic(BaseHeuristic):
def update_headers(self, resp):
return {"x-dummy-header": "foobar"}
self.sess = CacheControl(Session(), heuristic=DummyHeuristic())
示例15: _get_filehandle
def _get_filehandle(filepath_or, *args, **kwargs):
"""Open file if `filepath_or` looks like a string/unicode/bytes, else
pass through.
"""
if _is_string_or_bytes(filepath_or):
if requests.compat.urlparse(filepath_or).scheme in {'http', 'https'}:
sess = CacheControl(requests.Session(),
cache=FileCache(gettempdir()))
req = sess.get(filepath_or, **kwargs)
# if the response is not 200, an exception will be raised
req.raise_for_status()
fh, own_fh = BytesIO(req.content), True
else:
fh, own_fh = open(filepath_or, *args, **kwargs), True
else:
fh, own_fh = filepath_or, False
return fh, own_fh