Skip to content

Commit 1087daf

Browse files
author
ravi
committed
Get all Countries Currency rates for specific Country
0 parents  commit 1087daf

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
env
2+
*.pyc
3+
*.*~

CurrencyPy/__init__.py

Whitespace-only changes.

CurrencyPy/converter.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import requests
2+
import datetime
3+
4+
5+
class RateNotFoundError(Exception):
6+
"""
7+
Custom exception when conversion rate not found for given Country
8+
"""
9+
pass
10+
11+
12+
class SourceNotReadyError(Exception):
13+
"""
14+
Custome Exception when http://fixer.io/ is Down are not available for currency rates
15+
"""
16+
pass
17+
18+
19+
class Common:
20+
21+
def __init__(self):
22+
pass
23+
24+
def _source_url(self):
25+
return "http://api.fixer.io/"
26+
27+
def _validate_date(self, date_str):
28+
try:
29+
datetime.datetime.strptime(date_str, '%Y-%m-%d')
30+
except ValueError:
31+
raise ValueError("Incorrect date String, date_str should be YYYY-MM-DD")
32+
33+
34+
class CurrencyRates(Common):
35+
36+
def get_all_rates(self, base_cur, date_str=None):
37+
38+
if date_str is None:
39+
date_str = 'latest'
40+
else:
41+
self._validate_date(date_str)
42+
date_str = str(date_str)
43+
44+
payload = {'base': base_cur}
45+
source_url = self._source_url() + date_str
46+
response = requests.get(source_url, params=payload)
47+
if response.status_code == 200:
48+
rates = response.json().get('rates', {})
49+
print type(rates)
50+
return rates
51+
raise SourceNotReadyError("Currency Rates Source Not Ready")

0 commit comments

Comments
 (0)