You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
import datetime
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
|
|
|
|
API_URL = 'https://api.exchangeratesapi.io/latest'
|
|
|
|
|
|
|
|
|
|
|
|
class ExchangeRatesError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ExchangeRates(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.cache = {}
|
|
|
|
|
|
|
|
def query(self):
|
|
|
|
today = datetime.datetime.utcnow().date().isoformat()
|
|
|
|
try:
|
|
|
|
base, rates = self.cache[today]
|
|
|
|
except KeyError:
|
|
|
|
try:
|
|
|
|
data = requests.get(API_URL).json()
|
|
|
|
base, rates = data['base'], data['rates']
|
|
|
|
self.cache[today] = base, rates
|
|
|
|
except Exception as e:
|
|
|
|
raise ExchangeRatesError(str(e))
|
|
|
|
return base, rates
|