lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I'm using LuaSOAP as well, but I created the following code to transform the response to an arrary, as PHP is doing, which is more easier to manage.

-[[
    Soap.lua

    Vigie's glue to query a web service using SOAP

    18/07/2012 LF - First version
    20/07/2012 LF - Make it more Lua oriented
--]]

local client = require "soap.client"

function Soap2Object(o) -- Convert LuaSoap's LMO to PHP like array
    local result
    local nbre = 0
    local nptag = {}
    for k,v in pairs(o) do
        if tonumber(k) ~= nil then      -- Ignore named field (tag, attr, ...)
            if type(v) == "table" then
                cont, tag = Soap2Object(v)
                if tag then             -- it's a sub object
                    if result == nil then   -- 1st result
                        result = {}
                        nbre = 1
                    elseif nbre == 1 then   -- contains already 1 object which is not a table
                        result = { result }
                        nbre = 2    -- we don't care about the real #, it has to be > 1
                    end
                    if result[tag] == nil then
                        result[tag] = cont
                        nptag[tag] = 1
                        nbre = 2    -- Even if we are the 1st element, we have created a table
                    else
                        if nptag[tag] == 1 then
                            result[tag] = { result[tag] }
                            nptag[tag] = 2
                        end
                        table.insert(result[tag], cont)
                    end
                end
            elseif v ~= "\n" then
                if result == nil then   -- 1st element
                    result = v
                    nbre = 1
                else
                    if type(result) == "string" then
                        result = { result }
                        nbre = 2
                    end
                    table.insert(result, v)
                end
            end
        end
    end
    return result, o.tag
end

function Query(soapparm)    -- Query the webserver
    assert(soapparm, "Soap parameters missing")

    local ok, error_msg, mt, resp = pcall(client.call, soapparm)
    if not ok then
        return error_msg or "Soap call failed"
    end

    return Soap2Object(resp)
end

function ExtractFromResponce( resp, fieldslist )    -- Extract requested info from the response
    for i=1, #fieldslist do
        if not resp or not resp[ fieldslist[i] ] then
            return false    -- Field not found == no error to report
        end
        resp = resp[ fieldslist[i] ]
    end
    return resp
end

return {    -- Expose public functions
    Query = Query,
    ExtractFromResponce = ExtractFromResponce
}


Part of a monitoring tool I'll release soon :)

 
The misspelling master is on the Web.
_________ 100 % Dictionnary Free !
/ /(
/ Dico / / Pleins d'autres fautes sur
/________/ /
(#######( / http://destroyedlolo.info
Quoi, des fautes d'orthographe! Pas possible ;-D.

From: José Francisco Luis Medina <luisjf1983@gmail.com>
To: lua-l@lists.lua.org
Sent: Wednesday, October 10, 2012 3:04 PM
Subject: Cliento SOAP in lua

Hello greetings to all members, I'm new to the language LUA, I have the need to consume a web service from a page and I need to use a SOAP client for this. What is the best and simplest?