File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ env
2+ * .pyc
3+ * . * ~
Original file line number Diff line number Diff line change 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" )
You can’t perform that action at this time.
0 commit comments