summaryrefslogtreecommitdiff
path: root/examples/external_config.py
blob: 0ab6508e2bb9b793f641f78a007b7cf3f05ded5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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)