Integrating Buddyscript and Web Services Part II
This is a continuation of Integrating Buddyscript and Web Services Part I.
SOAP Web Service Call
Step 1: Determine Web Service Call Format
By going to our Web Service’s URL in a web browser, we can see that the Web Service XML code for a SOAP call looks like this:
The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values.
Request:
POST /Service1.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/HelloWorld"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<HelloWorld xmlns="http://tempuri.org/">
<input>string</input>
</HelloWorld>
</soap:Body>
</soap:Envelope>
Response:
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<HelloWorldResponse xmlns="http://tempuri.org/">
<HelloWorldResult>string</HelloWorldResult>
</HelloWorldResponse>
</soap:Body>
</soap:Envelope>
Step 2: Create the Datasource in Buddyscript
Using the SOAP method for calling the Web Service is the most restricted and least recommended. However, if you would like to use SOAP, the following is an example how to do so:
// SOAP DataSource HelloWorld
datasource HelloWorldSoap(INPUT) => HelloWorldResult
soap
proxy http://localhost:54908/Service1.asmx
name HelloWorld
namespace http://tempuri.org/
action http://tempuri.org/HelloWorld
input
string input = INPUT
simple xml
HelloWorldResult
The datasource will return the output of the HelloWorld Web Service function call.
Step 2.1 Manually Create the Datasource in Buddyscript
Instead of using the method in Step 2 above, you can also manually create the Web Service call using Buddyscript. This is the longest and most difficult (and tedious) way to query the Web Service. To do this, we first have to build the request. The code to do that looks like this:
// Build HelloWorld XML Query
function BuildGatewayAPIPostData_HelloWorld(INPUT)
POST_DATA = '<?xml version="1.0" encoding="UTF-8" ?>\n'
POST_DATA = StringConcat(POST_DATA, '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">')
POST_DATA = StringConcat(POST_DATA, ' <soap:Body>')
POST_DATA = StringConcat(POST_DATA, ' <HelloWorld xmlns="http://tempuri.org/">')
POST_DATA = StringConcat(POST_DATA, ' <input>',INPUT,'</input>')
POST_DATA = StringConcat(POST_DATA, ' </HelloWorld>')
POST_DATA = StringConcat(POST_DATA, ' </soap:Body>')
POST_DATA = StringConcat(POST_DATA, '</soap:Envelope>')
return POST_DATA
You can see how each line of code adds a line of the Request XML onto the POST_DATA string.
Now that we can build a request, we need to be able to use it. The following datasource will send the request to the Web Service and receive and process the response:
// Call HelloWorld Web Service and get response
datasource HelloWorldSoap(INPUT) => HelloWorldResult {expire="now"}
preprocess
// Build the Request string
POST_DATA = BuildGatewayAPIPostData_HelloWorld(INPUT)
http
http://localhost:54908/Service1.asmx
header
Host: localhost
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://tempuri.org/HelloWorld"
postdata {encode=no}
POST_DATA
simple xml
Envelope
Body
HelloWorldResponse
HelloWorldResult
The preprocess section is used to set variables that will be used in the following sections.
The http section describes the source. The source can be one of the following: abgw, absql, table, file, http, soap, or Buddyscript. The section format varies depending on the source type.
The simple xml section is called the filter. It describes the filtering to be performed on the incoming data in order to build a table to return. The filter can be abgwtab, abgwxml, simple xml, xslt, or Buddyscript. The content of this section depends on the filter type.
For our particular datasource, the URL under http is the URL of our Web Service. The data in the header section comes from the Web Service web page that we have open (see Step 1). The simple xml section lists the tags in the Response hierarchy (see Response XML code in Step 1).
The result, stored in <HelloWorldResult>, will be returned.
Step 3: Use the datasource
Once again, we use the SOAP datasource in the same way as the GET and POST datasources. Here is a sample routine that can be used with either method used above for querying the Web Service:
? HelloWorldSoap INPUT=Anything
RESPONSE = HelloWorldSoap(INPUT)
- SOAP Web Service Response: "RESPONSE"
The query:
HelloWorldSoap soap test
Would return:
SOAP Web Service Response: “you said soap test”
Conclusion
The ability to call Web Services from your Buddyscript project is a very powerful tool. Following these steps will allow you to quickly and easily set up your Web Service calls using a variety of different methods, and will afford you much greater flexibility in your Agents