summaryrefslogtreecommitdiff
path: root/examples/basic_patching.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/basic_patching.py')
-rwxr-xr-xexamples/basic_patching.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/examples/basic_patching.py b/examples/basic_patching.py
new file mode 100755
index 0000000..def0ead
--- /dev/null
+++ b/examples/basic_patching.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+# flake8: noqa: F841
+"""
+The same as `basic_sessions.py`, but using {ref}`patching`
+"""
+import time
+
+import requests
+
+import requests_cache
+
+# After installation, all requests functions and Session methods will be cached
+requests_cache.install_cache('example_cache', backend='sqlite')
+
+
+def main():
+ # The real request will only be made once; afterward, the cached response is used
+ for i in range(5):
+ response = requests.get('http://httpbin.org/get')
+
+ # This is more obvious when calling a slow endpoint
+ for i in range(5):
+ response = requests.get('http://httpbin.org/delay/2')
+
+ # Caching can be disabled if we want to get a fresh page and not cache it
+ with requests_cache.disabled():
+ print(requests.get('http://httpbin.org/ip').text)
+
+ # Get some debugging info about the cache
+ print(requests_cache.get_cache())
+ print('Cached URLS:', requests_cache.get_cache().urls)
+
+ # Uninstall to remove caching from all requests functions
+ requests_cache.uninstall_cache()
+
+
+if __name__ == "__main__":
+ t = time.time()
+ main()
+ print('Elapsed: %.3f seconds' % (time.time() - t))