feat: fetch api details from settings
This commit is contained in:
parent
ff19113677
commit
e093863c9e
@ -55,15 +55,16 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"fieldname": "result_key",
|
"fieldname": "result_key",
|
||||||
"fieldtype": "Data",
|
"fieldtype": "Table",
|
||||||
"label": "Result Key",
|
"label": "Result Key",
|
||||||
|
"options": "Currency Exchange Settings Result",
|
||||||
"reqd": 1
|
"reqd": 1
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"index_web_pages_for_search": 1,
|
"index_web_pages_for_search": 1,
|
||||||
"issingle": 1,
|
"issingle": 1,
|
||||||
"links": [],
|
"links": [],
|
||||||
"modified": "2021-09-02 15:18:29.198210",
|
"modified": "2021-09-03 13:21:16.397695",
|
||||||
"modified_by": "Administrator",
|
"modified_by": "Administrator",
|
||||||
"module": "Setup",
|
"module": "Setup",
|
||||||
"name": "Currency Exchange Settings",
|
"name": "Currency Exchange Settings",
|
||||||
|
@ -9,10 +9,13 @@ class CurrencyExchangeSettings(Document):
|
|||||||
def validate(self):
|
def validate(self):
|
||||||
if len(self.req_params) != 3:
|
if len(self.req_params) != 3:
|
||||||
frappe.throw(_("Make sure all the three mandatory parameters are filled."))
|
frappe.throw(_("Make sure all the three mandatory parameters are filled."))
|
||||||
|
transaction_date = '2021-08-01'
|
||||||
|
from_currency = 'USD'
|
||||||
|
to_currency = 'INR'
|
||||||
req_params={
|
req_params={
|
||||||
'transaction_date': '2021-08-01',
|
"transaction_date": transaction_date,
|
||||||
'from_currency': 'USD',
|
"from_currency": from_currency,
|
||||||
'to_currency': 'INR'
|
"to_currency": to_currency
|
||||||
}
|
}
|
||||||
params = {}
|
params = {}
|
||||||
for row in self.req_params:
|
for row in self.req_params:
|
||||||
@ -21,8 +24,14 @@ class CurrencyExchangeSettings(Document):
|
|||||||
req_params.pop(row.value)
|
req_params.pop(row.value)
|
||||||
except:
|
except:
|
||||||
frappe.throw(_("Make sure all the three mandatory parameters are filled."))
|
frappe.throw(_("Make sure all the three mandatory parameters are filled."))
|
||||||
|
for eparam in self.extra_params:
|
||||||
|
params[eparam.key] = eparam.value
|
||||||
import requests
|
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:
|
try:
|
||||||
response = requests.get(api_url, params=params)
|
response = requests.get(api_url, params=params)
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
@ -30,9 +39,14 @@ class CurrencyExchangeSettings(Document):
|
|||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
value = response.json()
|
value = response.json()
|
||||||
try:
|
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:
|
except KeyError:
|
||||||
frappe.throw(_("Invalid result key."))
|
frappe.throw(_("Invalid result key. Response: ") + response.text)
|
||||||
if not isinstance(rate, (int, float)):
|
if not isinstance(value, (int, float)):
|
||||||
frappe.throw(_("Returned exchange rate is neither integer not 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))
|
||||||
|
@ -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
|
||||||
|
}
|
@ -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
|
@ -98,15 +98,31 @@ def get_exchange_rate(from_currency, to_currency, transaction_date=None, args=No
|
|||||||
|
|
||||||
if not value:
|
if not value:
|
||||||
import requests
|
import requests
|
||||||
api_url = "https://api.exchangerate.host/convert"
|
settings = frappe.get_single('Currency Exchange Settings')
|
||||||
response = requests.get(api_url, params={
|
req_params={
|
||||||
"date": transaction_date,
|
"transaction_date": transaction_date,
|
||||||
"from": from_currency,
|
"from_currency": from_currency,
|
||||||
"to": to_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
|
# expire in 6 hours
|
||||||
response.raise_for_status()
|
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))
|
cache.setex(name=key, time=21600, value=flt(value))
|
||||||
return flt(value)
|
return flt(value)
|
||||||
except Exception:
|
except Exception:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user