2017-08-17 50 views
1

在一个Lua文件中,我想调用两个API并同时从两个API中获取数据。到目前为止,我设法调用一个API并打印数据,但不知道如何调用第二个调用,使用从第一个API调用获取的字符串。Lua - 如何传递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调用的代码)。非常感谢!

回答

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); 
+0

太棒了!它工作正常。非常感谢你! – user7665853

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)