Skip to content
Merged
Show file tree
Hide file tree
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: 5 additions & 4 deletions openeihttp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@

if response.status == 404:
raise UrlNotFound
elif response.status == 401:
if response.status == 401:
raise NotAuthorized
elif response.status != 200:
if response.status != 200:
_LOGGER.error( # pylint: disable-next=line-too-long
"An error reteiving data from the server, code: %s\nmessage: %s", # noqa: E501
response.status,
Expand All @@ -108,7 +108,7 @@
return message

except (TimeoutError, ServerTimeoutError):
_LOGGER.error("%s: %s", ERROR_TIMEOUT, self._url)
_LOGGER.error("%s: %s", ERROR_TIMEOUT, BASE_URL)

Check warning on line 111 in openeihttp/__init__.py

View check run for this annotation

Codecov / codecov/patch

openeihttp/__init__.py#L111

Added line #L111 was not covered by tests
message = {"error": ERROR_TIMEOUT}
except ContentTypeError as err:
_LOGGER.error("%s", err)
Expand Down Expand Up @@ -174,8 +174,10 @@
cache = OpenEICache()
# Load cached file if one exists
if await cache.cache_exists():
_LOGGER.debug("Cache file exists, reading...")
self._data = await cache.read_cache()
else:
_LOGGER.debug("Cache file missing, pulling API data...")
await self.update_data()
self._timestamp = datetime.datetime.now()
else:
Expand All @@ -188,7 +190,6 @@

async def update_data(self) -> None:
"""Update the data."""

params = {
"version": "latest",
"format": "json",
Expand Down
50 changes: 31 additions & 19 deletions openeihttp/cache.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,57 @@
""" Cache functions for python-openei."""

import aiofiles
import aiofiles.os
import json
import logging
from os.path import dirname, join
from os.path import dirname, exists, join, split
from typing import Any

import aiofiles
import aiofiles.os

_LOGGER = logging.getLogger(__name__)


class OpenEICache:
""" Represent OpenEI Cache manager."""
def __init__(self, cache_file: str = join(dirname(__file__), 'openei_cache')) -> None:
"""Represent OpenEI Cache manager."""

def __init__(self, cache_file: str = None) -> None:
"""Initialize."""
if not cache_file:
cache_file = join(dirname(__file__), "openei_cache")
self._cache_file = cache_file
self._directory, self._filename = split(cache_file)

async def write_cache(self, data: Any) -> None:
"""Write cache file."""
async with aiofiles.open(self._cache_file, mode='wb') as file:
if self._directory != "" and not exists(self._directory):
_LOGGER.debug("Directory missing creating: %s", self._directory)
await aiofiles.os.makedirs(self._directory)
async with aiofiles.open(self._cache_file, mode="wb") as file:
_LOGGER.debug("Writing file: %s", self._cache_file)
await file.write(data)

async def read_cache(self) -> Any:
"""Read cache file."""
async with aiofiles.open(self._cache_file, mode='r') as file:
_LOGGER.debug("Reading file: %s", self._cache_file)
value = await file.read()

try:
verify = json.loads(value)
return verify
except json.decoder.JSONDecodeError:
_LOGGER.info("Invalid JSON data")

return {}

_LOGGER.debug("Attempting to read file: %s", self._cache_file)
if exists(self._cache_file):
async with aiofiles.open(self._cache_file, mode="r") as file:
_LOGGER.debug("Reading file: %s", self._cache_file)
value = await file.read()

try:
verify = json.loads(value)
return verify
except json.decoder.JSONDecodeError:
_LOGGER.info("Invalid JSON data")
return {}
return {}

Check warning on line 46 in openeihttp/cache.py

View check run for this annotation

Codecov / codecov/patch

openeihttp/cache.py#L43-L46

Added lines #L43 - L46 were not covered by tests

async def cache_exists(self) -> bool:
"""Return bool if cache exists and contains data."""
check = await aiofiles.os.path.isfile(self._cache_file)
_LOGGER.debug("Cache file exists? %s", check)
if check:
size = await aiofiles.os.path.getsize(self._cache_file)
_LOGGER.debug("Checking cache file size: %s", size)
return size > 194
return False

Expand Down
6 changes: 5 additions & 1 deletion tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ async def test_get_rate_data(mock_aioclient, caplog):
repeat=True,
)
test_rates = openeihttp.Rates(
api="fakeAPIKey", lat="1", lon="1", plan="574613aa5457a3557e906f5b", cache_file="574613aa5457a3557e906f5b"
api="fakeAPIKey",
lat="1",
lon="1",
plan="574613aa5457a3557e906f5b",
cache_file=".cache/574613aa5457a3557e906f5b",
)
assert datetime.datetime.now() == datetime.datetime(2021, 8, 13, 10, 21, 34)
await test_rates.clear_cache()
Expand Down
Loading