comparison planemo/lib/python3.7/site-packages/cachecontrol/_cmd.py @ 0:d30785e31577 draft

"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author guerler
date Fri, 31 Jul 2020 00:18:57 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d30785e31577
1 import logging
2
3 import requests
4
5 from cachecontrol.adapter import CacheControlAdapter
6 from cachecontrol.cache import DictCache
7 from cachecontrol.controller import logger
8
9 from argparse import ArgumentParser
10
11
12 def setup_logging():
13 logger.setLevel(logging.DEBUG)
14 handler = logging.StreamHandler()
15 logger.addHandler(handler)
16
17
18 def get_session():
19 adapter = CacheControlAdapter(
20 DictCache(),
21 cache_etags=True,
22 serializer=None,
23 heuristic=None,
24 )
25 sess = requests.Session()
26 sess.mount('http://', adapter)
27 sess.mount('https://', adapter)
28
29 sess.cache_controller = adapter.controller
30 return sess
31
32
33 def get_args():
34 parser = ArgumentParser()
35 parser.add_argument('url', help='The URL to try and cache')
36 return parser.parse_args()
37
38
39 def main(args=None):
40 args = get_args()
41 sess = get_session()
42
43 # Make a request to get a response
44 resp = sess.get(args.url)
45
46 # Turn on logging
47 setup_logging()
48
49 # try setting the cache
50 sess.cache_controller.cache_response(resp.request, resp.raw)
51
52 # Now try to get it
53 if sess.cache_controller.cached_request(resp.request):
54 print('Cached!')
55 else:
56 print('Not cached :(')
57
58
59 if __name__ == '__main__':
60 main()