fetching and writing rates

This commit is contained in:
Pablo Martin 2024-06-11 20:33:35 +02:00
parent 7012cbec97
commit 41eae45f68
5 changed files with 140 additions and 43 deletions

View file

@ -6,7 +6,10 @@ from typing import List
from money.currency import Currency
from xecd_rates_client import XecdClient
from xexe.utils import DateRange
from xexe.exchange_rates import ExchangeRates
from xexe.rate_fetching import MockRateFetcher, RateFetcher, XERateFetcher
from xexe.rate_writing import CSVRateWriter, RateWriter
from xexe.utils import DateRange, generate_currency_and_dates_combinations
logger = logging.getLogger()
@ -57,43 +60,70 @@ def run_get_rates(
) -> None:
logger.info("Getting rates")
process_state = GetRatesProcessState(output=output)
process_state = GetRatesProcessState(output=output, dry_run=dry_run)
rates_fetcher = get_rates_fetcher(process_state.fetcher_type)
rates = obtain_rates_from_source(
process_state, date_range=date_range, currencies=currencies
)
write_rates_to_output(process_state, rates)
try:
rates = rates_fetcher.fetch()
except Exception as e:
process_state.record_rate_fetching_error(e)
raise ConnectionError(f"Could not fetch rates. See logs.")
rates_writer = get_rates_writer(process_state.output_type)
def obtain_rates_from_source(
process_state, date_range: DateRange, currencies: List[Currency]
) -> ExchangeRates:
rates_fetcher = process_state.get_fetcher()
try:
rates_writer.write(rates)
except Exception as e:
process_state.record_rate_writing_error(e)
raise Exception(f"Could not write rates. See logs.")
currency_and_date_combinations = generate_currency_and_dates_combinations(
date_range=date_range, currencies=currencies
)
rates = ExchangeRates()
for combination in currency_and_date_combinations:
try:
rate = rates_fetcher.fetch_rate(
from_currency=combination["from_currency"],
to_currency=combination["to_currency"],
rate_date=combination["date"],
)
except Exception as e:
logger.error(f"Error while fetching rates.")
logger.error(e, exc_info=True)
raise ConnectionError(f"Could not fetch rates. See logs.")
rates.add_rate(rate)
return rates
def write_rates_to_output(process_state, rates):
rates_writer = process_state.get_writer()
rates_writer.write_rates(rates)
class GetRatesProcessState:
def __init__(self, output: str, dry_run: bool) -> None:
self.writer_type = self._infer_writer_type(output)
self.fetcher_type = self._infer_fetcher_type(dry_run)
self.writer = self._select_writer(output)
self.fetcher = self._select_fetcher(dry_run)
@staticmethod
def _infer_writer_type(output: str) -> str:
def _select_writer(output: str) -> CSVRateWriter:
output_is_csv_file_path = bool(pathlib.Path(output).suffix == ".csv")
if output_is_csv_file_path:
return "csv_file"
return CSVRateWriter(output_file_path=output)
raise ValueError(f"Don't know how to handle passed output: {output}")
@staticmethod
def _infer_fetcher_type(dry_run: bool) -> str:
def _select_fetcher(dry_run: bool) -> str:
if dry_run:
return MockFetcher
return MockRateFetcher
if not dry_run:
return XEFetcher
return XERateFetcher
def get_fetcher(self) -> RateFetcher:
return self.fetcher
def get_writer(self) -> RateWriter:
return self.writer