[jsonrates api] add from global defaults

This commit is contained in:
Rushabh Mehta
2015-05-07 15:16:28 +05:30
parent 2682278ad9
commit 3545575c8b
3 changed files with 20 additions and 3 deletions

View File

@@ -60,14 +60,16 @@ def before_tests():
@frappe.whitelist()
def get_exchange_rate(from_currency, to_currency):
if frappe.conf.jsonrates_api_key:
jsonrates_api_key = frappe.conf.jsonrates_api_key or frappe.db.get_default("jsonrates_api_key")
if jsonrates_api_key:
cache = frappe.cache()
key = "currency_exchange_rate:{0}:{1}".format(from_currency, to_currency)
value = cache.get(key)
if not value:
import requests
response = requests.get("http://jsonrates.com/get/?from={0}&to={1}&apiKey={2}".format(from_currency,
to_currency, frappe.conf.jsonrates_api_key))
to_currency, jsonrates_api_key))
# expire in 24 hours
value = response.json().get("rate")
cache.setex(key, value, 24 * 60 * 60)