feat: fetch api details from settings

This commit is contained in:
rtdany10 2021-09-03 15:03:47 +05:30
parent ff19113677
commit e093863c9e
6 changed files with 88 additions and 18 deletions

View File

@ -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",

View File

@ -9,10 +9,13 @@ class CurrencyExchangeSettings(Document):
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 @@ class CurrencyExchangeSettings(Document):
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 @@ class CurrencyExchangeSettings(Document):
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))

View File

@ -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
}

View File

@ -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

View File

@ -98,15 +98,31 @@ def get_exchange_rate(from_currency, to_currency, transaction_date=None, args=No
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: