Module:Apitest: Difference between revisions

From RetroMC
Jump to navigation Jump to search
(another test of possible included functionality)
No edit summary
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:




local p = {}


local http = require("socket.http") -- Load the HTTP library
local json = require("json")


function p.getRandomFact()
function p.getRandomFact()
   local apiUrl = 'https://catfact.ninja/fact'
   local apiUrl = 'https://catfact.ninja/fact'
   local response, status = http.reguest(apiUrl)
   local response, status, headers = mw.fetchUrl(apiUrl)




   if status == 200 then
   if status == 200 then
     local factData = json.decode(response)
     local success, factData = pcall(mw.text.jsonDecode, response)
     return factData.value
     if success and factData and factData.fact then
 
    return factData.fact
  else
    return 'Invalid API Response'
    end
   else
   else
    return 'Request Failed either the API is Down or the site ran into a unknown error please try again later'
    return 'Request Failed either the API is Down or the site ran into a unknown error please try again later'
  end
    end
end
end


return p
return p

Latest revision as of 21:51, 25 March 2024

Documentation for this module may be created at Module:Apitest/doc



local p = {}


function p.getRandomFact()
   local apiUrl = 'https://catfact.ninja/fact'
   local response, status, headers = mw.fetchUrl(apiUrl)


   if status == 200 then
    local success, factData = pcall(mw.text.jsonDecode, response)
    if success and factData and factData.fact then
     return factData.fact
   else
     return 'Invalid API Response'
    end
   else
     return 'Request Failed either the API is Down or the site ran into a unknown error please try again later'
    end
end

return p