Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to apply base conversion on client side for free API plan #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions openexchangerates/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import decimal
import six

import requests

Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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)

47 changes: 41 additions & 6 deletions openexchangerates/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
"""

Expand All @@ -24,7 +25,8 @@ class TestOpenExchangeRates(unittest.TestCase):
"rates": {
"AED": 3.666311,
"AFN": 51.2281,
"ALL": 104.748751
"ALL": 104.748751,
"USD": 1
}
}
"""
Expand All @@ -37,7 +39,8 @@ class TestOpenExchangeRates(unittest.TestCase):
"rates": {
"AED": 3.666311,
"AFN": 51.2281,
"ALL": 104.748751
"ALL": 104.748751,
"USD": 1
}
}
"""
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
requests
requests
six