-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathconverter.py
More file actions
196 lines (155 loc) · 6.95 KB
/
converter.py
File metadata and controls
196 lines (155 loc) · 6.95 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import os
from decimal import Decimal
import requests
import simplejson as json
class RatesNotAvailableError(Exception):
"""
Custom exception when https://theforexapi.com is down and not available for currency rates
"""
pass
class DecimalFloatMismatchError(Exception):
"""
A float has been supplied when force_decimal was set to True
"""
pass
class Common:
def __init__(self, force_decimal=False):
self._force_decimal = force_decimal
def _source_url(self):
return None
def _request(self, url, params):
try:
return requests.get(url, params=params)
except:
raise RatesNotAvailableError("Currency Rates Source Not Ready")
def _get_date_string(self, date_obj):
if date_obj is None:
return 'latest'
date_str = date_obj.strftime('%Y-%m-%d')
return date_str
def _decode_rates(self, response, use_decimal=False, date_str=None, base_cur=None):
if self._force_decimal or use_decimal:
decoded_data = json.loads(response.text, use_decimal=True)
else:
decoded_data = response.json()
# if (date_str and date_str != 'latest' and date_str != decoded_data.get('date')):
# raise RatesNotAvailableError("Currency Rates Source Not Ready")
if base_cur != None and base_cur != decoded_data['base']:
raise RatesNotAvailableError("Currency Rates Source Not Ready")
return decoded_data.get('rates', {})
def _get_decoded_rate(
self, response, dest_cur, use_decimal=False, date_str=None):
return self._decode_rates(
response, use_decimal=use_decimal, date_str=date_str).get(
dest_cur, None)
class CurrencyRatesBase(Common):
def get_rates(self, base_cur, date_obj=None):
date_str = self._get_date_string(date_obj)
payload = {'base': base_cur, 'rtype': 'fpy'}
source_url = self._source_url() + date_str
response = self._request(source_url, params=payload)
if response.status_code == 200:
rates = self._decode_rates(response, date_str=date_str)
return rates
raise RatesNotAvailableError("Currency Rates Source Not Ready")
def get_rate(self, base_cur, dest_cur, date_obj=None, use_decimal=False):
if base_cur == dest_cur:
if use_decimal or self._force_decimal:
return Decimal(1)
return 1.
date_str = self._get_date_string(date_obj)
payload = {'base': base_cur, 'symbols': dest_cur, 'rtype': 'fpy'}
source_url = self._source_url() + date_str
response = self._request(source_url, params=payload)
if response.status_code == 200:
rate = self._get_decoded_rate(response, dest_cur, date_str=date_str)
if not rate:
raise RatesNotAvailableError("Currency Rate {0} => {1} not available for Date {2}".format(
base_cur, dest_cur, date_str))
return rate
raise RatesNotAvailableError("Currency Rates Source Not Ready")
def convert(self, base_cur, dest_cur, amount, date_obj=None):
use_decimal = False
if isinstance(amount, Decimal):
use_decimal = True
elif self._force_decimal:
raise DecimalFloatMismatchError("Decimal is forced. Amount must be Decimal")
if base_cur == dest_cur: # Return same amount if both base_cur, dest_cur are same
return amount
rate = self.get_rate(base_cur, dest_cur, date_obj=date_obj, use_decimal=use_decimal)
return rate * amount
class CurrencyRatesForexAPI(CurrencyRatesBase):
def _source_url(self):
return "https://theforexapi.com/api/"
class CurrencyRatesExchangeRateHost(CurrencyRatesBase):
def _source_url(self):
return "https://api.exchangerate.host/"
def get_rates(self, base_cur, date_obj=None):
date_str = self._get_date_string(date_obj)
payload = {'base': base_cur, 'rtype': 'fpy'}
source_url = self._source_url() + date_str
response = self._request(source_url, params=payload)
if response.status_code == 200:
rates = self._decode_rates(response,date_str=date_str, base_cur=base_cur)
return rates
raise RatesNotAvailableError("Currency Rates Source Not Ready")
class CurrencyRates(CurrencyRatesBase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.providers = [
CurrencyRatesForexAPI(*args, **kwargs),
CurrencyRatesExchangeRateHost(*args, **kwargs)
]
def get_rate(self, base_cur, dest_cur, date_obj=None, use_decimal=False):
for provider in self.providers:
try:
return provider.get_rate(base_cur, dest_cur, date_obj=date_obj, use_decimal=use_decimal)
except RatesNotAvailableError:
continue
raise RatesNotAvailableError("Rates Not Available For Any Provider")
def get_rates(self, base_cur, date_obj=None):
for provider in self.providers:
try:
return provider.get_rates(base_cur, date_obj=date_obj)
except RatesNotAvailableError:
continue
raise RatesNotAvailableError("Rates Not Available For Any Provider")
_CURRENCY_FORMATTER = CurrencyRates()
get_rates = _CURRENCY_FORMATTER.get_rates
get_rate = _CURRENCY_FORMATTER.get_rate
convert = _CURRENCY_FORMATTER.convert
class CurrencyCodes:
def __init__(self):
self.__currency_data = None
@property
def _currency_data(self):
if self.__currency_data is None:
file_path = os.path.dirname(os.path.abspath(__file__))
with open(file_path + '/raw_data/currencies.json') as f:
self.__currency_data = json.loads(f.read())
return self.__currency_data
def _get_data(self, currency_code):
currency_dict = next((item for item in self._currency_data if item["cc"] == currency_code), None)
return currency_dict
def _get_data_from_symbol(self, symbol):
currency_dict = next((item for item in self._currency_data if item["symbol"] == symbol), None)
return currency_dict
def get_symbol(self, currency_code):
currency_dict = self._get_data(currency_code)
if currency_dict:
return currency_dict.get('symbol')
return None
def get_currency_name(self, currency_code):
currency_dict = self._get_data(currency_code)
if currency_dict:
return currency_dict.get('name')
return None
def get_currency_code_from_symbol(self, symbol):
currency_dict = self._get_data_from_symbol(symbol)
if currency_dict:
return currency_dict.get('cc')
return None
_CURRENCY_CODES = CurrencyCodes()
get_symbol = _CURRENCY_CODES.get_symbol
get_currency_name = _CURRENCY_CODES.get_currency_name
get_currency_code_from_symbol = _CURRENCY_CODES.get_currency_code_from_symbol