parent
f52142fb06
commit
b2f2bef029
@ -0,0 +1,28 @@
|
|||||||
|
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[data['date']] = base, rates
|
||||||
|
except Exception as e:
|
||||||
|
raise ExchangeRatesError(str(e))
|
||||||
|
return base, rates
|
Loading…
Reference in new issue