Copy link to clipboard
Copied
In the plugin manager I want to show information retrieved via an API.
I get the error "We can only wait from within a task".
Normally for the "normal" plug-in functionality I start the main function from within
LrFunctionContext.postAsyncTaskWithContext ("Photo settings", function( context )
LrDialogs.attachErrorDialogToFunctionContext( context )
main()
end)
but how can I get the info from the internet API in the plug-in manager.
local LrHttp = import "LrHttp"
local JSON = require 'JSON'
local function getIpAddress()
local headers = {
{ field = "Accept", value = "application/json" },
{ field = "Content-Type", value = "application/json" },
{ field = "User-Agent", value = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134" },
{ field = "X-Requested-With", value = "XMLHttpRequest" },
}
local responseBody, hdrs = LrHttp.get("https://ipinfo.io/161.185.160.93/geo", headers )
local responseTable = JSON:decode(responseBody)
return responseTable.ip
end
local function sectionsForTopOfDialog( f, _ )
local ipAddress = getIpAddress()
return {
-- Section for the top of the dialog.
{
title = LOC "$$$/CustomMetadata/PluginManager=Custom Metadata Sample",
f:row {
f:static_text {
title = "IP address",
},
f:static_text {
title = ipAddress,
fill_horizontal = 1,
},
},
},
}
end
return{
sectionsForTopOfDialog = sectionsForTopOfDialog,
}
A workaround in these situations is to fetch the desired value in a separate task and update the UI when the value arrives:
local function sectionsForTopOfDialog (f, prop)
prop.ipAddress = "<fetching IP address>"
LrTask.startAsyncTask (function ()
prop.ipAddress = getIpAddress ()
end)
...f:static_text {title = LrView.bind "ipAddress"}...
The user will see "<fetching IP address>" initially, which then gets replaced with the actual IP address when it arrives.
Copy link to clipboard
Copied
A workaround in these situations is to fetch the desired value in a separate task and update the UI when the value arrives:
local function sectionsForTopOfDialog (f, prop)
prop.ipAddress = "<fetching IP address>"
LrTask.startAsyncTask (function ()
prop.ipAddress = getIpAddress ()
end)
...f:static_text {title = LrView.bind "ipAddress"}...
The user will see "<fetching IP address>" initially, which then gets replaced with the actual IP address when it arrives.
Copy link to clipboard
Copied
This works, thanks again