//  Xb2.NET sample showing how to execute remote SOAP methods
    //------------------------------------------------------------------------------------
    
    #include "xb2net.ch"
    #include "common.ch"
    #include "inkey.ch"
    #include "achoice.ch"
    
    #pragma  library ("xb2net.lib")
    
    #xtranslate NTrim() =>  LTrim(Str())
    
    static GetList[0]
    
    //-----------------------------------------------------------------------------
    PROCEDURE Main()
       Local nItem, oResult, cColor1 := SetColor(), cColor2
    
       set scoreboard off
       set cursor on
    
       while (nItem := SelectItem()) > 0
    
          // execute the selected function
          cls
          oResult := &("Test" + NTrim(nItem) + "()")
    
          if LastKey() == K_ESC
             // canceled
          else
             cColor2 := iif(Empty(oResult:Action) .or. Upper(oResult:Action) == "FAULT", "w/r", "w/b")
             ? "Result..........: "
             SetColor(cColor2)
             ?? oResult:Action
             SetColor(cColor1)
             ?
             ? "Returned data...: "
             SetColor(cColor2)
             ?? oResult:GetVar()
             SetColor(cColor1)
             ?
             wait
          endif
    
          clear typeahead
       end
    
       Return
    
    //-----------------------------------------------------------------------------
    STATIC FUNCTION SelectItem()
       Local i
       Local aMenu := {"Get exchange rate between two currencies", ;
                       "Execute WHOIS query for a domain name", ;
                       "Use BabelFish to translate text between languages", ;
                       "Get current date and time of Xb2.NET server", ;
                       "Save a message on Xb2.NET server", ;
                       "Request Xb2.NET server to echo back all parameters" }
    
       cls
       @ 1,3 SAY "This is a sample program showing how to use the Xb2.NET SOAP client"
       @ 3,0 TO 3,80
       @ 3,2 SAY " SELECT TEST "
       i := AChoice(5,3,15,60, aMenu,,{|a,b,c|MenuUdf(a,b,c)})
    
       Return i
    
    
    //-----------------------------------------------------------------------------
    STATIC FUNCTION MenuUdf( nMode, nElement, nRow )
       Local nReturn := AC_CONT        // default: continues
       Local nKey    := LastKey()
    
       if nMode == AC_EXCEPT
          do case
          case nKey == K_RETURN
             nReturn := AC_SELECT      // select element
          case nKey == K_ESC
             nReturn := AC_ABORT       // terminate AChoice()
          case nKey != K_LEFT .and. nKey != K_RIGHT
             nReturn := AC_GOTO        // jump to element
          endcase
       endif
    
       Return nReturn
    
    //-----------------------------------------------------------------------------
    FUNCTION Test1()
       Local oAgent, oResult
       Local cCountry1 := "USA           "
       Local cCountry2 := "Euro          "
    
       @ 1,0 SAY "This method will get the currency exchange rate between two countries"
       @ 3,3 SAY "Enter Country 1:" GET cCountry1
       @ 4,3 SAY "Enter Country 2:" GET cCountry2
       READ
    
       if LastKey() != K_ESC
          oAgent := xbSOAPEnvelope():new()
    
          oAgent:NameSpace := "urn:xmethods-CurrencyExchange"
          oAgent:SetVar("Country1", cCountry1)
          oAgent:SetVar("Country2", cCountry2)
    
          @ 6,3 SAY "Executing request..."
    
          oResult := oAgent:Execute("http://services.xmethods.net:80/soap", "getRate")
    
          @ 6,0 SAY Space(80)
       endif
    
       Return oResult
    
    //-----------------------------------------------------------------------------
    FUNCTION Test2()
       Local oAgent, oResult
       Local cDomainName := "microsoft.com             "
    
       @ 1,0 SAY "This method will execute a WHOIS query for a domain name"
       @ 3,3 SAY "Enter Domain Name:" GET cDomainName
       READ
    
       if LastKey() != K_ESC
          oAgent := xbSOAPEnvelope():new()
    
          // this server doesn't need a specific namespace
          //oAgent:NameSpace := "http://www.SoapClient.com/xml/SQLDataSoap.xsd"
          oAgent:SetVar("SRLFile", "/xml/whois.sri")
          oAgent:SetVar("RequestName", "whois")
          oAgent:SetVar("Key", cDomainName)
    
          @ 5,3 SAY "Executing request..."
    
          oResult := oAgent:Execute("http://www.soapclient.com/xml/SQLDataSoap.WSDL", "ProcessSRL" )
    
          @ 5,0 SAY Space(80)
       endif
    
       Return oResult
    
    //-----------------------------------------------------------------------------
    FUNCTION Test3()
       Local oAgent, oResult
       Local cLanguage := "en_es"
       Local cText     := PadR("Enter your text to translate here", 65)
    
       @ 1,0 SAY "This method will translate text between a variety of languages"
       @ 2,0 SAY "en=English, fr=French, de=German, it=Italian, pt=Portugese, es=Spanish"
       @ 4,3 SAY "Language:" GET cLanguage
       @ 5,3 SAY "Text    :" GET cText
       READ
    
       if LastKey() != K_ESC
          oAgent := xbSOAPEnvelope():new()
    
          oAgent:NameSpace := "urn:xmethodsBabelFish"
          oAgent:SetVar("translationmode", cLanguage)
          oAgent:SetVar("sourcedata", cText)
    
          @ 7,3 SAY "Executing request..."
    
          oResult := oAgent:Execute("http://services.xmethods.net:80/perl/soaplite.cgi", "BabelFish")
    
          @ 7,0 SAY Space(80)
       endif
    
       Return oResult
    
    //-----------------------------------------------------------------------------
    FUNCTION Test4()
       Local oAgent, oResult
       Local cHost := PadR("http://localhost:80",40)
    
       @ 1,0 SAY "This method will get the current date and time of the Xb2.NET HTTP server"
       @ 3,3 SAY "Server name or IP address:" GET cHost
       READ
    
       if LastKey() != K_ESC
          oAgent := xbSOAPEnvelope():new()
    
          @ 5,3 SAY "Executing request..."
    
          oResult := oAgent:Execute( Trim(cHost), "GetDateTime" )
    
          @ 5,0 SAY Space(80)
       endif
    
       Return oResult
    
    //-----------------------------------------------------------------------------
    FUNCTION Test5()
       Local oAgent, oResult
       Local cHost    := PadR("http://localhost:80",40)
       Local cName    := space(40)
       Local cMessage := space(40)
    
       @ 1,0 SAY "This method will save a message on the Xb2.NET HTTP server"
       @ 3,3 SAY "Server name or IP address:" GET cHost
       @ 5,3 SAY "                Your name:" GET cName
       @ 6,3 SAY "     Text message to save:" GET cMessage
       READ
    
       if LastKey() != K_ESC
          oAgent := xbSOAPEnvelope():new()
          oAgent:SetVar("UserName", cName)
          oAgent:SetVar("Message" , cMessage)
    
          @ 8,3 SAY "Executing request..."
    
          oResult := oAgent:Execute( Trim(cHost), "SaveMessage" )
    
          @ 8,0 SAY Space(80)
       endif
    
       Return oResult
    
    //-----------------------------------------------------------------------------
    // *** NOTE ***
    // In order to pass Xbase++ objects reliably, both server and client must be
    // compiled with Xbase++ 1.8 or higher. Attempting to pass objects using a prior
    // version of Xbase++ may result in an "Internal Data Structures Corrupted" error.
    
    FUNCTION Test6()
       Local oAgent, oResult
       Local cHost    := PadR("http://localhost:80",40)
       Local cChar    := "This is a sample character string."
       Local dDate    := date()
       Local lLogical := .t.
       Local nInteger := 12345
       Local nDecimal := 1234.567
       Local aArray   := {{"This is subarray [1,1]", 10, .t.}, {"This is subarray [2,1]", 20.2, .f., date()}}
       Local cMessage := "Eureka!" + chr(10) + "I can pass a codeblock using SOAP!"
       Local bBlock   := {||MsgBox(cMessage)}
       Local cError   := "Just testing how to pass an object using SOAP"
       Local oObject
    
    
       @ 1,0 SAY "This method requests the Xb2.NET HTTP server to echo back all parameters"
       @ 3,3 SAY "Server name or IP address:" GET cHost
       @ 5,3 SAY "PARAMETERS:      MyString:" GET cChar
       @ 6,3 SAY "                   MyDate:" GET dDate
       @ 7,3 SAY "                MyLogical:" GET lLogical
       @ 8,3 SAY "                MyInteger:" GET nInteger
       @ 9,3 SAY "                MyDecimal:" GET nDecimal
       @10,3 SAY "      MyCodeBlock message:" GET cMessage
       @11,3 SAY "     MyObject description:" GET cError
    
       READ
    
       if LastKey() != K_ESC
          oAgent := xbSOAPEnvelope():new()
    
          oAgent:SetVar("MyString"   , cChar   )
          oAgent:SetVar("MyDate"     , dDate   )
          oAgent:SetVar("MyLogical"  , lLogical)
          oAgent:SetVar("MyInteger"  , nInteger)
          oAgent:SetVar("MyDecimal"  , nDecimal)
          oAgent:SetVar("MyArray"    , aArray  )
          oAgent:SetVar("MyCodeBlock", bBlock  )
    
          // create an object and save it in the SOAP envelope
          oObject := Error():New()
          oObject:subSystem   := "SOAP-TEST"
          oObject:subCode     := 999
          oObject:description := cError
          oObject:operation   := ":SetVar"
          oObject:severity    := 0
    
          oAgent:SetVar("MyObject"   , oObject )
    
          @ 13,3 SAY "Executing request..."
    
          oResult := oAgent:Execute( Trim(cHost), "EchoAll" )
    
          @ 13,0 SAY Space(80)
    
          if oResult:Action == "EchoAllResponse"
             ? "MyObject:description:", oResult:GetVar("MyObject"):description
             ?
             Eval(oResult:GetVar("MyCodeBlock"))
          endif
    
       endif
    
       Return oResult
    

    home     download     top of page