• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

SDK Show info from an API call in the sectionsForTopOfDialog

Participant ,
Nov 09, 2022 Nov 09, 2022

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,

}

 

TOPICS
SDK

Views

120

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Nov 09, 2022 Nov 09, 2022

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.

Votes

Translate

Translate
LEGEND ,
Nov 09, 2022 Nov 09, 2022

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 11, 2022 Nov 11, 2022

Copy link to clipboard

Copied

LATEST

This works, thanks again

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines