summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorJordan Cook <jordan.cook@pioneer.com>2022-04-01 13:34:09 -0500
committerJordan Cook <jordan.cook@pioneer.com>2022-04-01 13:55:09 -0500
commite4a8fbffd9888f194f59071b95d5a3db4d3ed873 (patch)
tree9ea4947bd41517aa65031452904bebd34f508bf7 /examples
parent185f5869958e52ec8cea03038e64dda0f86c2350 (diff)
downloadrequests-cache-e4a8fbffd9888f194f59071b95d5a3db4d3ed873.tar.gz
Add example of loading settings from a config file
Diffstat (limited to 'examples')
-rw-r--r--examples/external_config.py29
-rw-r--r--examples/external_config.yml16
2 files changed, 45 insertions, 0 deletions
diff --git a/examples/external_config.py b/examples/external_config.py
new file mode 100644
index 0000000..0ab6508
--- /dev/null
+++ b/examples/external_config.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+"""
+An example of loading CachedSession settings from an external config file.
+
+Limitations:
+* Does not include backend or serializer settings
+* Does not include settings specified as python expressions, for example `timedelta` objects or
+ callback functions
+"""
+from pathlib import Path
+
+import yaml
+
+from requests_cache import CachedSession, CacheSettings
+
+CONFIG_FILE = Path(__file__).parent / 'external_config.yml'
+
+
+def load_settings() -> CacheSettings:
+ """Load settings from a YAML config file"""
+ with open(CONFIG_FILE) as f:
+ settings = yaml.safe_load(f)
+ return CacheSettings(**settings['cache_settings'])
+
+
+if __name__ == '__main__':
+ session = CachedSession()
+ session.settings = load_settings()
+ print('Loaded settings:\n', session.settings)
diff --git a/examples/external_config.yml b/examples/external_config.yml
new file mode 100644
index 0000000..ce26e29
--- /dev/null
+++ b/examples/external_config.yml
@@ -0,0 +1,16 @@
+# See external_config.py for usage example
+cache_settings:
+ expire_after: 360
+ cache_control: True
+ stale_if_error: True
+ allowable_methods:
+ - GET
+ - HEAD
+ - POST
+ allowable_codes:
+ - 200
+ - 400
+ ignored_parameters:
+ - api_key
+ match_headers:
+ - Accept-Language