Lua - 如何传递API调用获取的字符串以调用另一个API调用

时间:2017-08-17 15:45:09

标签: json api lua

在一个Lua文件中,我想调用两个API并同时从两个API获取数据。到目前为止,我设法调用一个API并打印数据,但是我不知道如何使用从第一次API调用中获得的字符串来调用第二个调用

local json = require( "json" )
local function networkListener( event )
    local res = json.prettify( event.response )
    local decoded = json.decode( res )  
    if ( event.isError ) then
        print( "--Network error-- ", ( res ) )
    else
        print( "Data: " .. ( res ) )
        print(decoded.results.bindings[1].person.value)
        print(decoded.results.bindings[1].type.value)
        local dbpuri = decoded.results.bindings[1].person.value
        local wikititle = string.gsub(dbpuri, "http://dbpedia.org/resource/", "")
        print (wikititle)
    end
end

local headers = {}
headers["Content-Type"] = "application/json"
local body = ""
local params = {}
params.headers = headers

params.body = body
network.request("https://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=select+*%0D%0Awhere+{%3Fperson+rdfs%3Alabel+%3Fperson_name+%3B+rdf%3Atype+%3Ftype+%3B+dbo%3AbirthDate+%3Fbirthdate+.%0D%0A++++bind(rand(1+%2B+strlen(str(%3Fperson))*0)+as+%3Frid)%0D%0AFILTER+regex(%3Ftype%2C+%22Person%22)%0D%0A}+order+by+%3Frid%0D%0ALIMIT+10&format=text%2Fhtml&CXML_redir_for_subjs=121&CXML_redir_for_hrefs=&timeout=30000&debug=on&format=json", "GET", networkListener, params)

这是第一个API调用(成功),但我想传递局部变量" wikititle"调用第二个API调用。我在前面的代码下面添加了一个几乎完全相同的代码(见下文),但是它出错:"尝试连接本地' wikititle' (零值)......"

local json = require( "json" )
local function networkListener( event )
    local res = json.prettify( event.response )
    local decoded = json.decode( res )  
    if ( event.isError ) then
        print( "--Network error-- ", ( res ) )
    else
        print( "Data: " .. ( res ) )
        print(decoded.query.pages.original.source)
        print(decoded.warnings.pageimages)
    end
end

local headers = {}
headers["Content-Type"] = "application/json"
local body = ""
local params = {}
params.headers = headers

params.body = body
network.request("https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&piprop=original&titles="..wikititle.."", params)

我是Lua的初学者,但有人能帮我找到一个好的解决方案吗? (我想我应该使用前向声明,最好是我可以避免重复代码中两个API调用的代码)。非常感谢!

2 个答案:

答案 0 :(得分:1)

在这种情况下,您需要使用嵌套回调。我也会将这个特殊情况分成它自己的函数,而不是试图使用一个单片函数来处理所有事情。在基于事件的世界中,传递未命名的功能来完成工作是相当标准的,在这个世界里,你不能完全控制'当'事情发生了

local function getWikipediaData(event)
    local res = json.prettify( event.response )
    local decoded = json.decode( res )  

    if ( event.isError ) then
        print( "--Network error-- ", ( res ) )
        return
    end

    local dbpuri = decoded.results.bindings[1].person.value
    local wikititle = string.gsub(dbpuri, "http://dbpedia.org/resource/", "")
    network.request(
        "https://en.wikipedia.org/.../&titles=".. wikititle,
        "GET",
        function(event) 
            -- handle the wikipedia response here
        end,
        params)
end

network.request(
    "https://dbpedia.org/sparql?...", 
    "GET", 
    getWikipediaData,
    params);

答案 1 :(得分:0)

这是我最终得到的答案:

local json = require( "json" )
local function getWikipediaData( event )
    local res = json.prettify( event.response )
    local decoded = json.decode( res )  
    if ( event.isError ) then
        print( "--Network error-- ", ( res ) )
        return
    else
        print( "Data: " .. ( res ) )
        print(decoded.results.bindings[1].person.value)
        print(decoded.results.bindings[1].type.value)
        local dbpuri = decoded.results.bindings[1].person.value
        local wikititle = string.gsub(dbpuri, "http://dbpedia.org/resource/", "")
        print (wikititle)

        network.request(
                "https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&piprop=original&titles="..wikititle,
                "GET",
                function(event) 
                    local res2 = json.prettify( event.response )
                    local decoded2 = json.decode( res2 )
                    print( "Data2: " .. ( res2 ) )
                    print(decoded2.query.normalized[1].to)
                end,
                params)
    end
end

local headers = {}
headers["Content-Type"] = "application/json"
local body = ""
local params = {}
params.headers = headers

params.body = body
network.request("https://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=select+*%0D%0Awhere+{%3Fperson+rdfs%3Alabel+%3Fperson_name+%3B+rdf%3Atype+%3Ftype+%3B+dbo%3AbirthDate+%3Fbirthdate+.%0D%0A++++bind(rand(1+%2B+strlen(str(%3Fperson))*0)+as+%3Frid)%0D%0AFILTER+regex(%3Ftype%2C+%22Person%22)%0D%0A}+order+by+%3Frid%0D%0ALIMIT+10&format=text%2Fhtml&CXML_redir_for_subjs=121&CXML_redir_for_hrefs=&timeout=30000&debug=on&format=json", "GET", networkListener, params)