feat: fetch api details from settings
diff --git a/erpnext/setup/doctype/currency_exchange_settings/currency_exchange_settings.json b/erpnext/setup/doctype/currency_exchange_settings/currency_exchange_settings.json
index b010437..a0dfe73 100644
--- a/erpnext/setup/doctype/currency_exchange_settings/currency_exchange_settings.json
+++ b/erpnext/setup/doctype/currency_exchange_settings/currency_exchange_settings.json
@@ -55,15 +55,16 @@
},
{
"fieldname": "result_key",
- "fieldtype": "Data",
+ "fieldtype": "Table",
"label": "Result Key",
+ "options": "Currency Exchange Settings Result",
"reqd": 1
}
],
"index_web_pages_for_search": 1,
"issingle": 1,
"links": [],
- "modified": "2021-09-02 15:18:29.198210",
+ "modified": "2021-09-03 13:21:16.397695",
"modified_by": "Administrator",
"module": "Setup",
"name": "Currency Exchange Settings",
diff --git a/erpnext/setup/doctype/currency_exchange_settings/currency_exchange_settings.py b/erpnext/setup/doctype/currency_exchange_settings/currency_exchange_settings.py
index b726d75..1993c9b 100644
--- a/erpnext/setup/doctype/currency_exchange_settings/currency_exchange_settings.py
+++ b/erpnext/setup/doctype/currency_exchange_settings/currency_exchange_settings.py
@@ -9,10 +9,13 @@
def validate(self):
if len(self.req_params) != 3:
frappe.throw(_("Make sure all the three mandatory parameters are filled."))
- req_params = {
- 'transaction_date': '2021-08-01',
- 'from_currency': 'USD',
- 'to_currency': 'INR'
+ transaction_date = '2021-08-01'
+ from_currency = 'USD'
+ to_currency = 'INR'
+ req_params={
+ "transaction_date": transaction_date,
+ "from_currency": from_currency,
+ "to_currency": to_currency
}
params = {}
for row in self.req_params:
@@ -21,8 +24,14 @@
req_params.pop(row.value)
except:
frappe.throw(_("Make sure all the three mandatory parameters are filled."))
+ for eparam in self.extra_params:
+ params[eparam.key] = eparam.value
import requests
- api_url = self.api_endpoint
+ api_url = self.api_endpoint.format(
+ transaction_date=transaction_date,
+ to_currency=to_currency,
+ from_currency=from_currency
+ )
try:
response = requests.get(api_url, params=params)
except requests.exceptions.RequestException as e:
@@ -30,9 +39,14 @@
response.raise_for_status()
value = response.json()
try:
- rate = value[str(self.result_key)]
+ for key in self.result_key:
+ value = value[str(key.key).format(
+ transaction_date=transaction_date,
+ to_currency=to_currency,
+ from_currency=from_currency
+ )]
except KeyError:
- frappe.throw(_("Invalid result key."))
- if not isinstance(rate, (int, float)):
+ frappe.throw(_("Invalid result key. Response: ") + response.text)
+ if not isinstance(value, (int, float)):
frappe.throw(_("Returned exchange rate is neither integer not float."))
- frappe.msgprint(_("Exchange rate of USD to INR on 01-08-2021 is ") + str(rate))
+ frappe.msgprint(_("Exchange rate of USD to INR on 01-08-2021 is ") + str(value))
diff --git a/erpnext/setup/doctype/currency_exchange_settings_result/__init__.py b/erpnext/setup/doctype/currency_exchange_settings_result/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/setup/doctype/currency_exchange_settings_result/__init__.py
diff --git a/erpnext/setup/doctype/currency_exchange_settings_result/currency_exchange_settings_result.json b/erpnext/setup/doctype/currency_exchange_settings_result/currency_exchange_settings_result.json
new file mode 100644
index 0000000..4203b80
--- /dev/null
+++ b/erpnext/setup/doctype/currency_exchange_settings_result/currency_exchange_settings_result.json
@@ -0,0 +1,31 @@
+{
+ "actions": [],
+ "creation": "2021-09-03 13:17:22.088259",
+ "doctype": "DocType",
+ "editable_grid": 1,
+ "engine": "InnoDB",
+ "field_order": [
+ "key"
+ ],
+ "fields": [
+ {
+ "fieldname": "key",
+ "fieldtype": "Data",
+ "in_list_view": 1,
+ "label": "Key",
+ "reqd": 1
+ }
+ ],
+ "index_web_pages_for_search": 1,
+ "istable": 1,
+ "links": [],
+ "modified": "2021-09-03 13:17:22.088259",
+ "modified_by": "Administrator",
+ "module": "Setup",
+ "name": "Currency Exchange Settings Result",
+ "owner": "Administrator",
+ "permissions": [],
+ "sort_field": "modified",
+ "sort_order": "DESC",
+ "track_changes": 1
+}
\ No newline at end of file
diff --git a/erpnext/setup/doctype/currency_exchange_settings_result/currency_exchange_settings_result.py b/erpnext/setup/doctype/currency_exchange_settings_result/currency_exchange_settings_result.py
new file mode 100644
index 0000000..2c4fb61
--- /dev/null
+++ b/erpnext/setup/doctype/currency_exchange_settings_result/currency_exchange_settings_result.py
@@ -0,0 +1,8 @@
+# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and contributors
+# For license information, please see license.txt
+
+# import frappe
+from frappe.model.document import Document
+
+class CurrencyExchangeSettingsResult(Document):
+ pass
diff --git a/erpnext/setup/utils.py b/erpnext/setup/utils.py
index 409f776..76e52cd 100644
--- a/erpnext/setup/utils.py
+++ b/erpnext/setup/utils.py
@@ -98,15 +98,31 @@
if not value:
import requests
- api_url = "https://api.exchangerate.host/convert"
- response = requests.get(api_url, params={
- "date": transaction_date,
- "from": from_currency,
- "to": to_currency
- })
+ settings = frappe.get_single('Currency Exchange Settings')
+ req_params={
+ "transaction_date": transaction_date,
+ "from_currency": from_currency,
+ "to_currency": to_currency
+ }
+ params = {}
+ for row in settings.req_params:
+ params[row.key] = req_params[row.value]
+ for eparam in settings.extra_params:
+ params[eparam.key] = eparam.value
+ response = requests.get(settings.api_endpoint.format(
+ transaction_date=transaction_date,
+ to_currency=to_currency,
+ from_currency=from_currency
+ ), params=params)
# expire in 6 hours
response.raise_for_status()
- value = response.json()["result"]
+ value = response.json()
+ for res_key in settings.result_key:
+ value = value[str(res_key.key).format(
+ transaction_date=transaction_date,
+ to_currency=to_currency,
+ from_currency=from_currency
+ )]
cache.setex(name=key, time=21600, value=flt(value))
return flt(value)
except Exception: