Hi all,
I’ve tried to implement a server side code that runs daily to fetch the current exchange rate and store it into Currency Exchange table. My code looks like this …
from frappe import utils
import requests
response = requests.get("https://api.polygon.io/v2/aggs/ticker/C:USDPYG/prev?adjusted=true&apiKey=########")
response.raise_for_status()
rates = response.json()["rates"][to_currency]
currFX = frappe.new_doc("Currency Exchange")
currFX.update({
"date": utils.today(),
"from_currency": "USD",
"to_currency": "PYG",
"exchange_rate": rates,
})
currFX.insert()
currFXrev = frappe.new_doc("Currency Exchange")
currFXrev.update({
"date": utils.today(),
"from_currency": "PYG",
"to_currency": "USD",
"exchange_rate": 1 / rates,
})
currFXrev.insert()
It’s a pretty straight forward code, and just trying to get my hands dirty before I start doing larger customizations. But as the code runs, it launches the following error…
Traceback (most recent call last):
File "apps/frappe/frappe/core/doctype/scheduled_job_type/scheduled_job_type.py", line 83, in execute
frappe.get_doc("Server Script", script_name).execute_scheduled_method()
File "apps/frappe/frappe/core/doctype/server_script/server_script.py", line 101, in execute_scheduled_method
safe_exec(self.script)
File "apps/frappe/frappe/utils/safe_exec.py", line 64, in safe_exec
exec(compile_restricted(script), exec_globals, _locals) # pylint: disable=exec-used
File "<unknown>", line 2, in <module>
ImportError: __import__ not found
It’s hard to follow up with this traceback, if anyone can offer some guidance will be greateful!
Thanks
Can’t you just use currency exchange settings to setup polygon.io as forex rate provider? That will be simpler 
The reason why your code doesn’t work is because importing external modules isn’t allowed in server scripts.
You can instead use frappe.make_get_request
which is pretty much same as request.get
1 Like
Wait what??
that’s an amazing feature. But I don’t find it. It doesn’t show up in search nor does it show up In accounting module. Perhaps this is a version 14 feature? I’m working on the latest version on frappe cloud.
Yeah, my bad. It’s part of the upcoming v14 release.
Hi @ankush
Fearing I may have to be spoon fed as the documentation isn’t very clear on what is allowed and what isn’t. Please take a look at the following:
response = frappe.make_get_request("https://api.polygon.io/v2/aggs/ticker/C:USDPYG/prev?adjusted=true&apiKey=#####")
data = response.json()
rate = data["results"][0]["c"]
currFX = frappe.new_doc("Currency Exchange")
currFX.update({
"date": "2022-05-01",
"from_currency": "USD",
"to_currency": "PYG",
"exchange_rate": rate,
})
currFX.insert()
I need to navigate the json object and extract the C value located within results (view json response below). My current error is now at line 3 (TypeError: ‘NoneType’ object is not callable). Any suggestions as to how to extract the C value from the below json in a way that pleases client scripting?
{
"ticker": "C:EURUSD",
"queryCount": 1,
"resultsCount": 1,
"adjusted": true,
"results": [
{
"T": "C:EURUSD",
"v": 5861,
"vw": 1.0543,
"o": 1.05262,
"c": 1.05348,
"h": 1.05509,
"l": 1.05262,
"t": 1651449599999,
"n": 5861
}
],
"status": "OK",
"request_id": "ec96b9eae22610427ce2aa8cdab183c2",
"count": 1
}