forked from deployed/tddworkshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexchange.py
More file actions
66 lines (49 loc) · 2.23 KB
/
exchange.py
File metadata and controls
66 lines (49 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import urllib
import json
class ExchangeException(Exception):
pass
class BaseCurrencyProvider(object):
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
def get_rate(self, currency_in, currency_out):
raise NotImplementedError
class DummyCurrencyProvider(BaseCurrencyProvider):
def __init__(self, exchange_rates=None, *args, **kwargs):
super(DummyCurrencyProvider, self).__init__(*args, **kwargs)
self.exchange_rates = exchange_rates or {
('eur', 'pln'): 4.24,
('pln', 'eur'): 0.25,
('usd', 'pln'): 3.8,
}
def get_rate(self, currency_in, currency_out):
rate = self.exchange_rates.get((currency_in, currency_out))
if not rate:
raise ExchangeException('%s -> %s is not defined in exchange_rates ' % (currency_in, currency_out))
return rate
class CurrencyProvider(BaseCurrencyProvider):
url_template = url = "https://currency-api.appspot.com/api/{currency_in}/{currency_out}.json"
def __getitem__(self, item):
return self.get_rate(*item)
def get_url(self, currency_in, currency_out):
return self.url_template.format(currency_in=currency_in, currency_out=currency_out)
def get_rate(self, currency_in, currency_out):
connection = urllib.urlopen(self.get_url(currency_in, currency_out))
result = connection.read()
connection.close()
result = json.loads(result)
if not result.get('success'):
raise ExchangeException(result.get('message'))
return result['rate']
class CurrencyExchanger(object):
def __init__(self, currency_provider_class=CurrencyProvider, *args, **kwargs):
self.default_currency = kwargs.get('default_currency', )
self.currency_provider_class = currency_provider_class
self.currency_provider = self.currency_provider_class(exchange_rates=kwargs.get('exchange_rates', ))
def exchange(self, *args):
if self.default_currency:
amount, currency_in = args
currency_out = self.default_currency
else:
amount, currency_in, currency_out = args
return amount * self.currency_provider.get_rate(currency_in, currency_out)