-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfile_cache.py
More file actions
34 lines (28 loc) · 1.26 KB
/
file_cache.py
File metadata and controls
34 lines (28 loc) · 1.26 KB
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
30
31
32
33
34
import json
import logging
from okdata.sdk.io_utils import write_to_okdata_cache, read_from_okdata_cache
log = logging.getLogger()
class FileCache:
def __init__(self, config):
self.credentials_cache_enabled = config.get("cacheCredentials")
self.env = config.get("env")
def write_credentials(self, credentials: str):
if self.credentials_cache_enabled:
filename = f"client_credentials-{self.env}.json"
log.debug(f"Writing credentials to cache: {filename}")
write_to_okdata_cache(content=credentials, filename=filename)
else:
log.debug("Skipping write_credentials: cache is not enabled")
def read_credentials(self):
if self.credentials_cache_enabled:
filename = f"client_credentials-{self.env}.json"
credentials = read_from_okdata_cache(filename=filename)
if credentials:
try:
log.debug(f"Reading credentials from cache: {filename}")
return json.loads(credentials)
except ValueError as ve:
log.debug(f"Could not read credentials from cache: {ve}")
else:
log.debug("Skipping write_credentials: cache is not enabled")
return None