Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that cache directory exists #1613

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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