This repository has been archived by the owner on Aug 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add offline support and show list of currencies
Add supporting functions to show list of currencies available and add offline support by storing the exchange rates in a json file. Remove the json files for testing, and use the original files instead. Not commiting exhcange rates file, since we have a function to download the rates if not found. This closes #24, fixes #16.
- Loading branch information
Showing
9 changed files
with
126 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
.vscode | ||
venv | ||
env | ||
__pycache__ | ||
*.egg-info | ||
.cache | ||
.coverage | ||
.tox | ||
*.pyc | ||
fixer_rates.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"target": "NZD", "base": "CAD"} | ||
{"target": "CAD", "base": "PHP"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,35 @@ | ||
""" Functions related to file(JSON) handling. """ | ||
""" Functions related to default rates file. """ | ||
|
||
import json | ||
|
||
def set_default_base(new_base, filepath): | ||
def set_default_base(new_base, default_rates_filepath): | ||
""" set new default base currency """ | ||
with open(filepath) as json_file: | ||
with open(default_rates_filepath) as json_file: | ||
json_data = json.load(json_file) | ||
|
||
json_data['base'] = new_base | ||
|
||
with open(filepath, 'w') as json_file: | ||
json.dump(json_data, json_file, indent=4) | ||
with open(default_rates_filepath, 'w') as json_file: | ||
json.dump(json_data, json_file) | ||
|
||
def set_default_target(new_target, filepath): | ||
def set_default_target(new_target, default_rates_filepath): | ||
""" set new default arget currency """ | ||
with open(filepath) as json_file: | ||
with open(default_rates_filepath) as json_file: | ||
json_data = json.load(json_file) | ||
|
||
json_data['target'] = new_target | ||
|
||
with open(filepath, 'w') as json_file: | ||
with open(default_rates_filepath, 'w') as json_file: | ||
json.dump(json_data, json_file) | ||
|
||
def get_default_base(filepath): | ||
def get_default_base(default_rates_filepath): | ||
""" get the currenct default base currency """ | ||
with open(filepath) as json_file: | ||
with open(default_rates_filepath) as json_file: | ||
json_data = json.load(json_file) | ||
return json_data['base'] | ||
|
||
def get_default_target(filepath): | ||
def get_default_target(default_rates_filepath): | ||
""" get the current default target currency """ | ||
with open(filepath) as json_file: | ||
with open(default_rates_filepath) as json_file: | ||
json_data = json.load(json_file) | ||
return json_data['target'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,61 @@ | ||
""" helper functions to gather the currency rates """ | ||
|
||
import json | ||
import requests | ||
|
||
def fixer(base, target, value, date='latest'): | ||
"""get currency exchange rate from fixer.io in JSON""" | ||
def fixer(base, target, value, file_path): | ||
""" | ||
get currency exchange rate from fixer.io in JSON, | ||
take note that all the rates are stored as EUR as base | ||
""" | ||
|
||
main_api = 'http://api.fixer.io/{}?'.format(date) | ||
url = requests.get(main_api, params={'base':base}) | ||
try: | ||
""" | ||
try to find the json file which contains currency exchange rates | ||
""" | ||
with open(file_path) as json_file: | ||
json_rates = json.load(json_file) | ||
except FileNotFoundError: | ||
""" | ||
if file not found, 'fixer_sync' will download the latests rates, | ||
the range of 200-300 is for succesful HTTP code | ||
""" | ||
if fixer_sync(file_path) in range(200, 300): | ||
with open(file_path) as json_file: | ||
json_rates = json.load(json_file) | ||
|
||
try: | ||
json_data = requests.get(url.url).json() | ||
result = round(json_data['rates'][target] * value, 2) | ||
except requests.exceptions.ConnectionError: | ||
result = "Connection Error" | ||
eur_to_target = json_rates['rates'][target] | ||
eur_to_base = json_rates['rates'][base] | ||
result = eur_to_target/eur_to_base * value | ||
except KeyError: | ||
result = 1.00 if base == target else "KeyError: Invalid curreny" | ||
if base == 'EUR': | ||
result = json_rates['rates'][target] * value | ||
elif target == 'EUR': | ||
result = (1.0/json_rates['rates'][base]) * value | ||
else: | ||
result = "KeyError: Invalid curreny" | ||
|
||
return result | ||
|
||
def fixer_sync(file_path): | ||
""" | ||
downloads the rates JSON to the local location, | ||
'file_path' is the location where the fixer.io rates will the placed, | ||
returns status_code, if it is 200 then file successfully created | ||
""" | ||
url = 'http://api.fixer.io/latest' | ||
response = requests.get(url, stream=True) | ||
if response.status_code == 200: | ||
with open(file_path, 'wb') as json_file: | ||
json_file.write(response.content) | ||
|
||
return response.status_code | ||
|
||
# function to list currencies present | ||
# use the above sync method to down the file if not present | ||
# show the date of the file present | ||
|
||
#def google_finance_converter(base, target, value): | ||
# """ parse the Google Finance Converter html to extract the value""" | ||
# pass |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,35 @@ | ||
""" unit testing the helper functions """ | ||
|
||
import pkg_resources | ||
from exch.helpers import fixer | ||
|
||
FIXER_RATES_JSON_TEST = pkg_resources.resource_filename('exch', 'data/fixer_rates.json') | ||
|
||
|
||
def test_fixer_same_currency_code(): | ||
""" when the same currency is base as well as target """ | ||
assert fixer('USD', 'USD', 1) == 1.00 | ||
assert fixer('INR', 'INR', 1) == 1.00 | ||
assert fixer('USD', 'USD', 1, FIXER_RATES_JSON_TEST) == 1.00 | ||
assert fixer('INR', 'INR', 1, FIXER_RATES_JSON_TEST) == 1.00 | ||
|
||
def test_fixer_usd_to_inr(): | ||
""" fixer_io base: USD, target: INR, on date 2017-05-12 """ | ||
assert fixer('USD', 'INR', 1, date='2017-05-12') == round(64.307, 2) | ||
""" fixer_io base: USD, target: INR """ | ||
assert fixer('USD', 'INR', 1, FIXER_RATES_JSON_TEST) > 60 | ||
|
||
def test_fixer_usd_to_jpy(): | ||
""" fixer_io base: USD, target: JPY, on date 2017-05-12 """ | ||
assert fixer('USD', 'JPY', 1, date='2017-05-12') == round(113.85, 2) | ||
""" fixer_io base: USD, target: JPY """ | ||
assert fixer('USD', 'JPY', 1, FIXER_RATES_JSON_TEST) > 90 | ||
|
||
def test_fixer_gbp_to_php_value_99(): | ||
""" fixer_io base: GBP, target: PHP, on date 2017-05-12, value: 99 """ | ||
assert fixer('GBP', 'PHP', 99, date='2017-05-12') == round(63.942 * 99, 2) | ||
""" fixer_io base: GBP, target: PHP, value: 99 """ | ||
assert fixer('GBP', 'PHP', 99, FIXER_RATES_JSON_TEST) > 50 | ||
|
||
def test_fixer_eur_to_aud_value_99(): | ||
assert fixer('EUR', 'AUD', 99, FIXER_RATES_JSON_TEST) > 1.2 * 99 | ||
|
||
def test_fixer_nzd_to_eur_value(): | ||
""" checking since stored values are in terms of EUR as base """ | ||
assert fixer('NZD', 'EUR', 1, FIXER_RATES_JSON_TEST) > 0.5 | ||
|
||
def test_fixer_invalid_currency(): | ||
""" when invalid currency is passed to fixer.io """ | ||
assert fixer('USD', 'TTT', 1) == "KeyError: Invalid curreny" | ||
assert fixer('USD', 'TTT', 1, FIXER_RATES_JSON_TEST) == "KeyError: Invalid curreny" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters