Skip to content

Commit

Permalink
Ensure that cache directory exists (#1613)
Browse files Browse the repository at this point in the history
This will ensure that the cache directory exists prior to writing into it.
  • Loading branch information
rytilahti authored Dec 5, 2022
1 parent 4911ea2 commit 27cc3ad
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions miio/miot_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ def fetch_release_list(self):

return ReleaseList.parse_raw(data)

def _write_to_cache(self, file: Path, data: str):
"""Write given *data* to cache file *file*."""
file.parent.mkdir(exist_ok=True)
written = file.write_text(data)
_LOGGER.debug("Written %s bytes to %s", written, file)

def _fetch(self, url: str, target_file: Path, cache_hours=6):
"""Fetch the URL and cache results, if expired."""

Expand All @@ -106,7 +112,6 @@ def valid_cache():
content.raise_for_status()

response = content.text
written = target_file.write_text(response)
_LOGGER.debug("Written %s bytes to %s", written, target_file)
self._write_to_cache(target_file, response)

return response

0 comments on commit 27cc3ad

Please sign in to comment.