fix: improve exchange rate fetching with retry logic and default return value
This commit is contained in:
parent
c84ac29332
commit
9761989ea4
@ -275,12 +275,16 @@ def get_exchange_rate(from_currency, to_currency, date=None):
|
|||||||
|
|
||||||
url = f"https://api.frankfurter.app/{date}?from={from_currency}&to={to_currency}"
|
url = f"https://api.frankfurter.app/{date}?from={from_currency}&to={to_currency}"
|
||||||
|
|
||||||
response = requests.get(url)
|
for _i in range(3):
|
||||||
|
response = requests.get(url)
|
||||||
|
if response.status_code == 200:
|
||||||
|
data = response.json()
|
||||||
|
rate = data["rates"].get(to_currency)
|
||||||
|
if rate:
|
||||||
|
return rate
|
||||||
|
|
||||||
if response.status_code == 200:
|
frappe.log_error(
|
||||||
data = response.json()
|
f"Failed to fetch exchange rate from {from_currency} to {to_currency} on {date}",
|
||||||
rate = data["rates"].get(to_currency)
|
title="Exchange Rate Fetch Error",
|
||||||
return rate
|
)
|
||||||
else:
|
return 1.0 # Default exchange rate if API call fails or no rate found
|
||||||
frappe.throw(_("Failed to fetch historical exchange rate from external API. Please try again later."))
|
|
||||||
return None
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user