diff --git a/.gitignore b/.gitignore index 02d0b6e..b283fc2 100644 --- a/.gitignore +++ b/.gitignore @@ -60,3 +60,6 @@ target/ #Ipython Notebook .ipynb_checkpoints env +*.xml +*.iml +*.sqlite diff --git a/forex_python/converter.py b/forex_python/converter.py index bf465d0..8dfc3f1 100644 --- a/forex_python/converter.py +++ b/forex_python/converter.py @@ -2,6 +2,8 @@ from decimal import Decimal import simplejson as json import requests +import requests_cache +requests_cache.install_cache('forex_rates', backend='redis', namespace='forex', expire_after=86400) class RatesNotAvailableError(Exception): @@ -37,8 +39,10 @@ def _decode_rates(self, response, use_decimal=False, date_str=None): 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 (date_str and date_str != 'latest' + and date_str != decoded_data.get('date')): + raise RatesNotAvailableError("Currency Rates Source Not Ready") return decoded_data.get('rates', {}) def _get_decoded_rate( @@ -52,7 +56,7 @@ class CurrencyRates(Common): def get_rates(self, base_cur, date_obj=None): date_str = self._get_date_string(date_obj) - payload = {'base': base_cur, 'rtype': 'fpy'} + payload = {'base': base_cur} source_url = self._source_url() + date_str response = requests.get(source_url, params=payload) if response.status_code == 200: @@ -66,7 +70,7 @@ def get_rate(self, base_cur, dest_cur, date_obj=None): return Decimal(1) return 1. date_str = self._get_date_string(date_obj) - payload = {'base': base_cur, 'symbols': dest_cur, 'rtype': 'fpy'} + payload = {'base': base_cur, 'symbols': dest_cur} source_url = self._source_url() + date_str response = requests.get(source_url, params=payload) if response.status_code == 200: @@ -89,7 +93,7 @@ def convert(self, base_cur, dest_cur, amount, date_obj=None): return float(amount) date_str = self._get_date_string(date_obj) - payload = {'base': base_cur, 'symbols': dest_cur, 'rtype': 'fpy'} + payload = {'base': base_cur, 'symbols': dest_cur} source_url = self._source_url() + date_str response = requests.get(source_url, params=payload) if response.status_code == 200: @@ -103,6 +107,7 @@ def convert(self, base_cur, dest_cur, amount, date_obj=None): return converted_amount except TypeError: raise DecimalFloatMismatchError("convert requires amount parameter is of type Decimal when force_decimal=True") + raise RatesNotAvailableError("Currency Rates Source Not Ready") diff --git a/setup.py b/setup.py index de66932..8762426 100644 --- a/setup.py +++ b/setup.py @@ -32,6 +32,7 @@ install_requires=[ 'requests', 'simplejson', + 'requests_cache' ], classifiers=[ 'Intended Audience :: Developers', diff --git a/tests/test.py b/tests/test.py index 4add1db..909c8ad 100644 --- a/tests/test.py +++ b/tests/test.py @@ -1,5 +1,6 @@ import datetime from decimal import Decimal +import unittest from unittest import TestCase from forex_python.converter import (get_rates, get_rate, convert, get_symbol, get_currency_name, RatesNotAvailableError, @@ -196,3 +197,7 @@ def test_with_valid_currency_code(self): def test_with_invalid_currency_code(self): self.assertFalse(get_currency_name('XYZ')) + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file