many changes

This commit is contained in:
Pablo Martin 2024-06-11 21:10:07 +02:00
parent 126ede37a4
commit 661941a65c
7 changed files with 102 additions and 21 deletions

View file

@ -56,16 +56,23 @@ def run_get_rates(
date_range: DateRange,
currencies: List[Currency],
dry_run: bool,
ignore_warnings: bool,
output: pathlib.Path,
) -> None:
logger.info("Getting rates")
process_state = GetRatesProcessState(output=output, dry_run=dry_run)
process_state = GetRatesProcessState(
output=output, dry_run=dry_run, ignore_warnings=ignore_warnings
)
rates = obtain_rates_from_source(
process_state, date_range=date_range, currencies=currencies
process_state,
date_range=date_range,
currencies=currencies,
)
logger.info("Rates obtained.")
write_rates_to_output(process_state, rates)
logger.info("Rates written to output.")
def obtain_rates_from_source(
@ -77,6 +84,21 @@ def obtain_rates_from_source(
date_range=date_range, currencies=currencies
)
large_api_call_planned = (
rates_fetcher.is_production_grade and len(currency_and_date_combinations) > 100
)
if large_api_call_planned and not process_state.ignore_warnings:
user_confirmation_string = "i understand"
user_response = input(
f"WARNING: you are about to execute a large call {len(currency_and_date_combinations)} to a metered API. Type '{user_confirmation_string}' to move forward."
)
if user_response != user_confirmation_string:
raise Exception("Execution aborted.")
logger.debug(
f"We are looking for the following rate combinations: {currency_and_date_combinations}"
)
rates = ExchangeRates()
for combination in currency_and_date_combinations:
try:
@ -97,13 +119,15 @@ def obtain_rates_from_source(
def write_rates_to_output(process_state, rates):
rates_writer = process_state.get_writer()
logger.info("Attempting writing rates to output.")
rates_writer.write_rates(rates)
class GetRatesProcessState:
def __init__(self, output: str, dry_run: bool) -> None:
def __init__(self, output: str, dry_run: bool, ignore_warnings: bool) -> None:
self.writer = self._select_writer(output)
self.fetcher = self._select_fetcher(dry_run)
self.ignore_warnings = ignore_warnings
@staticmethod
def _select_writer(output: str) -> CSVRateWriter:
@ -115,12 +139,14 @@ class GetRatesProcessState:
raise ValueError(f"Don't know how to handle passed output: {output}")
@staticmethod
def _select_fetcher(dry_run: bool) -> str:
def _select_fetcher(dry_run: bool) -> RateFetcher:
if dry_run:
return MockRateFetcher
logger.info("Dry-run activated. Running against MockRateFetcher.")
return MockRateFetcher()
if not dry_run:
return XERateFetcher
logger.info("Real run active. Running against XE.com's API.")
return XERateFetcher()
def get_fetcher(self) -> RateFetcher:
return self.fetcher