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