From e4a8fbffd9888f194f59071b95d5a3db4d3ed873 Mon Sep 17 00:00:00 2001 From: Jordan Cook Date: Fri, 1 Apr 2022 13:34:09 -0500 Subject: Add example of loading settings from a config file --- examples/external_config.py | 29 +++++++++++++++++++++++++++++ examples/external_config.yml | 16 ++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 examples/external_config.py create mode 100644 examples/external_config.yml (limited to 'examples') 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 -- cgit v1.2.1