Currency-Converter / frankfurter.py
bikas
Currency Converter First Deployment
1f6c124
raw
history blame
1.3 kB
import requests
BASE_URL = "https://api.frankfurter.app"
def get_latest_rate(from_currency, to_currency):
"""
Fetch the latest conversion rate from Frankfurter API.
Args:
from_currency: Currency code to convert from.
to_currency: Currency code to convert to.
Returns:
The latest conversion rate.
"""
response = requests.get(f"{BASE_URL}/latest?from={from_currency}&to={to_currency}")
if response.status_code == 200:
data = response.json()
return data['rates'][to_currency]
else:
raise Exception("Failed to fetch the latest rate")
def get_historical_rate(from_currency, to_currency, date):
"""
Fetch the historical conversion rate for a specific date from Frankfurter API.
Args:
from_currency: Currency code to convert from.
to_currency: Currency code to convert to.
date: Date for which to fetch the rate (in YYYY-MM-DD format).
Returns:
The historical conversion rate.
"""
response = requests.get(f"{BASE_URL}/{date}?from={from_currency}&to={to_currency}")
if response.status_code == 200:
data = response.json()
return data['rates'][to_currency]
else:
raise Exception("Failed to fetch historical rate")