-
Notifications
You must be signed in to change notification settings - Fork 3
/
exemplo00.lua
executable file
·64 lines (52 loc) · 2.63 KB
/
exemplo00.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
---Aplicação de exemplo que consome Web Service para conversão de moedas.
--@author Manoel Campos da Silva Filho<br>
--<a href="http://manoelcampos.com">http://manoelcampos.com</a>
--Adiciona o diretório lib ao path de bibliotecas, para que a aplicação encontre
--os módulos disponibilizados
package.path = package.path .. ';lib/?.lua'
local function main()
local ncluasoap = require "ncluasoap"
---Função para processar a resposta da requisição SOAP enviada ao WS
--@param result Resultado da chamada ao método remoto via SOAP.
--No caso deste Web Service, o resultado é uma variável primitiva simples (ou seja, contendo apenas um valor)
local function getResponse(result)
--Forma de uso com NCLua SOAP anterior a 0.5.6
--print("\n\n---------Cotação do Dolar em Reais:", result.ConversionRateResult, "\n\n")
print("\n\n\n--------------------------------RESULTADO--------------------------------")
print(" Cotação do Dolar em Reais:", result)
print("-------------------------------------------------------------------------\n\n\n")
--Finaliza o script lua. Um link no NCL finalizará a aplicação NCL quando o nó lua for finalizado
event.post {class="ncl", type="presentation", action="stop"}
end
--Cria uma table contendo os dados para envio da requisição SOAP ao WS
local msgTable = {
address = "http://www.webservicex.net/CurrencyConvertor.asmx",
--Namespace exatamente como especificado no WSDL, neste caso, terminando com /
namespace = "http://www.webserviceX.NET/",
operationName = "ConversionRate",
params = {
FromCurrency = "USD", --Dólar
ToCurrency = "BRL" --Real
}
}
ncluasoap.debug = true
--Executa o método remoto, definido dentro da msgTable,
--gerando uma requisição SOAP, enviando ao WS e obtendo o resultado.
--getResponse é uma função de callback que será executada
--automaticamente, assim que a resposta da chamada remota for obtida.
ncluasoap.call(msgTable, getResponse)
--Esta linha é executada automaticamente após a chamada de ncluasoap.call
--A chamada a ncluasoap.call retorna imediatamente, pois é uma chamada
--assíncrona, devido à particularidades do módulo TCP de NCLua.
--Assim, NÃO é possível obter o retorna do método remoto
--fazendo algo como retorno = ncluasoap.call(msgTable).
--Tal instrução não funciona.
print("---------------------------Chamou ncluasoap.call")
end
--Chama a função main que executará a chamada ao WS
--em modo protegido, permitindo tratar erros gerados.
local ok, res = pcall(main)
if not ok then
print("\n\nError: "..res, "\n\n")
return -1
end