diff --git a/openexchangerates/__init__.py b/openexchangerates/__init__.py index 74487d3..0a33874 100644 --- a/openexchangerates/__init__.py +++ b/openexchangerates/__init__.py @@ -1,4 +1,5 @@ import decimal +import six import requests @@ -27,7 +28,27 @@ def __init__(self, api_key): self.client = requests.Session() self.client.params.update({'app_id': api_key}) - def latest(self, base='USD'): + def _parsed_response(self, response, local_base=None): + data = response.json(parse_int=decimal.Decimal, + parse_float=decimal.Decimal) + + if local_base: + return self._local_conversion(data, local_base) + else: + return data + + def _local_conversion(self, data, base): + """Change base using local conversion, only useful for the free plan + """ + data['base'] = base + new_rates = {} + for curr, value in six.iteritems(data['rates']): + new_rates[curr] = round(value / data['rates'][base], 8) + + data['rates'] = new_rates + return data + + def latest(self, base='USD', local_base=None): """Fetches latest exchange rate data from service :Example Data: @@ -49,10 +70,9 @@ def latest(self, base='USD'): try: resp = self.client.get(self.ENDPOINT_LATEST, params={'base': base}) resp.raise_for_status() - except requests.exceptions.RequestException, e: + except requests.exceptions.RequestException as e: raise OpenExchangeRatesClientException(e) - return resp.json(parse_int=decimal.Decimal, - parse_float=decimal.Decimal) + return self._parsed_response(resp, local_base) def currencies(self): """Fetches current currency data of the service @@ -75,12 +95,12 @@ def currencies(self): """ try: resp = self.client.get(self.ENDPOINT_CURRENCIES) - except requests.exceptions.RequestException, e: + except requests.exceptions.RequestException as e: raise OpenExchangeRatesClientException(e) return resp.json() - def historical(self, date, base='USD'): + def historical(self, date, base='USD', local_base=None): """Fetches historical exchange rate data from service :Example Data: @@ -104,7 +124,7 @@ def historical(self, date, base='USD'): date.strftime("%Y-%m-%d"), params={'base': base}) resp.raise_for_status() - except requests.exceptions.RequestException, e: + except requests.exceptions.RequestException as e: raise OpenExchangeRatesClientException(e) - return resp.json(parse_int=decimal.Decimal, - parse_float=decimal.Decimal) + return self._parsed_response(resp, local_base) + diff --git a/openexchangerates/tests.py b/openexchangerates/tests.py index f837ac4..ae953fb 100644 --- a/openexchangerates/tests.py +++ b/openexchangerates/tests.py @@ -12,7 +12,8 @@ class TestOpenExchangeRates(unittest.TestCase): _FIXTURE_CURRENCIES = """{ "AED": "United Arab Emirates Dirham", "AFN": "Afghan Afghani", - "ALL": "Albanian Lek" + "ALL": "Albanian Lek", + "USD": "United States Dollar" } """ @@ -24,7 +25,8 @@ class TestOpenExchangeRates(unittest.TestCase): "rates": { "AED": 3.666311, "AFN": 51.2281, - "ALL": 104.748751 + "ALL": 104.748751, + "USD": 1 } } """ @@ -37,7 +39,8 @@ class TestOpenExchangeRates(unittest.TestCase): "rates": { "AED": 3.666311, "AFN": 51.2281, - "ALL": 104.748751 + "ALL": 104.748751, + "USD": 1 } } """ @@ -53,13 +56,15 @@ def test_historical(self): historical = client.historical(date) self.assertIn('rates', historical) rates = historical['rates'] - self.assertEqual(len(rates), 3) + self.assertEqual(len(rates), 4) self.assertIn('AED', rates) self.assertEqual(rates['AED'], Decimal('3.666311')) self.assertIn('AFN', rates) self.assertEqual(rates['AFN'], Decimal('51.2281')) self.assertIn('ALL', rates) self.assertEqual(rates['ALL'], Decimal('104.748751')) + self.assertIn('USD', rates) + self.assertEqual(rates['USD'], Decimal('1')) @httprettified def test_currencies(self): @@ -68,10 +73,11 @@ def test_currencies(self): HTTPretty.register_uri(HTTPretty.GET, client.ENDPOINT_CURRENCIES, body=self._FIXTURE_CURRENCIES) currencies = client.currencies() - self.assertEqual(len(currencies), 3) + self.assertEqual(len(currencies), 4) self.assertIn('AED', currencies) self.assertIn('AFN', currencies) self.assertIn('ALL', currencies) + self.assertIn('USD', currencies) @httprettified def test_latest(self): @@ -82,13 +88,42 @@ def test_latest(self): latest = client.latest() self.assertIn('rates', latest) rates = latest['rates'] - self.assertEqual(len(rates), 3) + self.assertEqual(len(rates), 4) self.assertIn('AED', rates) self.assertEqual(rates['AED'], Decimal('3.666311')) self.assertIn('AFN', rates) self.assertEqual(rates['AFN'], Decimal('51.2281')) self.assertIn('ALL', rates) self.assertEqual(rates['ALL'], Decimal('104.748751')) + self.assertIn('USD', rates) + self.assertEqual(rates['USD'], Decimal('1')) + + + @httprettified + def test_latest_with_local_base_conversion(self): + client = openexchangerates.OpenExchangeRatesClient('DUMMY_API_KEY') + HTTPretty.register_uri(HTTPretty.GET, client.ENDPOINT_LATEST, + body=self._FIXTURE_LATEST) + rates = client.latest(local_base='AED')['rates'] + self.assertEqual(rates['AED'], Decimal('1')) + self.assertEqual(rates['AFN'], Decimal('13.97265535')) + self.assertEqual(rates['ALL'], Decimal('28.57061253')) + self.assertEqual(rates['USD'], Decimal('0.27275373')) + + + @httprettified + def test_historical_with_local_base_conversion(self): + client = openexchangerates.OpenExchangeRatesClient('DUMMY_API_KEY') + date = Date.fromtimestamp(1358150409) + HTTPretty.register_uri(HTTPretty.GET, client.ENDPOINT_HISTORICAL % + date.strftime("%Y-%m-%d"), + body=self._FIXTURE_HISTORICAL) + rates = client.historical(date, local_base='AED')['rates'] + self.assertEqual(rates['AED'], Decimal('1')) + self.assertEqual(rates['AFN'], Decimal('13.97265535')) + self.assertEqual(rates['ALL'], Decimal('28.57061253')) + self.assertEqual(rates['USD'], Decimal('0.27275373')) + @httprettified def test_exception(self): diff --git a/requirements.txt b/requirements.txt index 663bd1f..640e3d4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -requests \ No newline at end of file +requests +six