-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.py
20 lines (18 loc) · 877 Bytes
/
provider.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from collections import namedtuple
import logging
class Provider(namedtuple('Provider', ['name', 'url'])):
def get_exchange_rate(self, from_currency_code, to_currency_code):
import requests
api_url = f'{self.url}{from_currency_code}'
currency_pair = f'{from_currency_code}{to_currency_code}'
try:
response = requests.get(api_url).json()
exchange_rate = response.get('rates').get(to_currency_code)
if exchange_rate is not None:
logging.info(f'{self.name}: {currency_pair} rate: {exchange_rate}')
else:
logging.warn(f'{self.name}: No rate for {currency_pair}')
return exchange_rate
except Exception as e:
logging.error(f'{self.name}: exception while getting quote for currency pair {currency_pair}: {e}')
return None