Skip to content

Commit

Permalink
File from the URL doesn't have to be a zip anymore (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
oittaa authored Nov 19, 2021
1 parent 1ec2e27 commit 29e409d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
15 changes: 10 additions & 5 deletions ibkr_report/exchangerates.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from typing import Iterable
from urllib.error import HTTPError
from urllib.request import urlopen
from zipfile import ZipFile
from zipfile import BadZipFile, ZipFile

from google.cloud import exceptions, storage # type: ignore

Expand Down Expand Up @@ -98,10 +98,15 @@ def download_official_rates(self, url: str) -> None:
log.warning(error_msg, err.code, err.reason)
retries += 1
log.debug("Successfully downloaded the latest exchange rates: %s", url)
with ZipFile(bytes_io) as rates_zip:
for filename in rates_zip.namelist():
with rates_zip.open(filename) as rates_file:
self.add_to_exchange_rates(rates_file)
try:
with ZipFile(bytes_io) as rates_zip:
for filename in rates_zip.namelist():
with rates_zip.open(filename) as rates_file:
self.add_to_exchange_rates(rates_file)
except BadZipFile:
bytes_io.seek(0)
self.add_to_exchange_rates(bytes_io)

log.debug("Parsed exchange rates from the retrieved data.")

def get_rate(self, currency_from: str, currency_to: str, date_str: str) -> Decimal:
Expand Down
11 changes: 10 additions & 1 deletion test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
from main import app

TEST_BUCKET = "test"
TEST_URL = "file://" + os.path.abspath(os.getcwd()) + "/test-data/eurofxref-hist.zip"
THIS_PATH = os.path.abspath(os.getcwd())
TEST_URL = f"file://{THIS_PATH}/test-data/eurofxref-hist.zip"
TEST_BROKEN_URL = f"file://{THIS_PATH}/test-data/eurofxref-broken.csv"


@patch("ibkr_report.exchangerates.BUCKET_ID", TEST_BUCKET)
Expand Down Expand Up @@ -282,6 +284,13 @@ def test_broken_input(self):
eur_usd = self.rates.get_rate("EUR", "USD", "2021-10-25")
self.assertEqual(eur_usd, Decimal("1.1603"))

def test_download_without_zip(self):
rates = ExchangeRates(TEST_BROKEN_URL)
eur_usd = rates.get_rate("EUR", "USD", "2021-10-26")
self.assertEqual(eur_usd, Decimal("1.1618"))
with self.assertRaises(ValueError):
eur_usd = rates.get_rate("EUR", "USD", "2021-10-25")


if __name__ == "__main__":
unittest.main()

0 comments on commit 29e409d

Please sign in to comment.