|
|
Tcl SOAP Client Utility Packages |
|
||||||||||
Contents
IntroductionI have been testing out using SOAP from Tcl and have come up with the following package to provide Tcl commands for SOAP remote procedure calls. The following code demonstrates the ease of use for simple RPC calls. This is a work in progress and some parts need more work. In particular the transport layer needs to be able to be configured to cope with proxy HTTP servers and authenticating proxys and the like. Still it may be usefull as-is. This example connects the Tcl procedure getTemp to the remote method hosted by XMethods. In all these examples Tcl commands are prefixed with the % character that is my prompt under tclsh. Results are printed without the preceeding prompt character. % package require SOAP
1.6
% SOAP::create getTemp \
-uri "urn:xmethods-Temperature" \
-proxy "http://services.xmethods.net/soap/servlet/rpcrouter" \
-params { "zipcode" "string" }
::getTemp
% getTemp 90810
41.2
We bring in the package and then define the method. The
configuration requires a URI for the XML namespace of the SOAP
method and a URL for the service. The params configure option
allows the Tcl procedure to do simple parameter checking without
passing duff packets over the network.
Another example, this time using the extremely clever SOAP::Lite Perl package as a server, a Celcius to Fahrenheit convertor. We shall test it using that peculiar temperature -40. This time I want the Tcl procedure to be called C2F. % package require SOAP
1.6
% SOAP::create C2F \
-uri "http://www.soaplite.com/Temperatures" \
-proxy "http://services.soaplite.com/temper.cgi" \
-params { "temp" "float"}\
-name c2f
::C2F
% C2F -40.0
-40
%
It will be possible to use different transport protocols to transfer the SOAP packets. At this time only HTTP is supported. You may need to configure the transport subsystem so the SOAP::configure command has a -transport option. For example, at work I live behind a Microsoft NT firewalling proxy web server. So I need to tell the SOAP transport about the proxy. This is an authenticating proxy, so I have to add a header to all HTTP requests using the Proxy-Authorization HTTP field. Here's how I set up my system and then create a command for the SOAP::Lite languages method. % package require SOAP
1.6
% SOAP::configure -transport http -proxy proxyhost:8080 \
-headers { "Proxy-Authorization" "Basic dXNlcm5hbWUgOiBwYXNzd29yZA==" }
% SOAP::create languages \
-uri "http://www.soaplite.com/Demo" \
-proxy "http://services.soaplite.com/hibye.cgi" \
-params {}
::languages
% languages
Perl sh C
%
To setup a method to use a different transport there is a -transport option for the method configure command. This should be set to the name of a procedure that will be called with the URL of the SOAP service and the XML data of the SOAP request. It should return the reply packet or error. For example, I have a dummy transport procedure that prints the SOAP request and doesn't send it anywhere. To see what is being generated for a method: # A dummy SOAP transport procedure to examine the SOAP requests generated.
proc SOAP::Transport::print::print {procVarName url soap} {
puts "$soap"
}
% SOAP::configure languages -transport SOAP::Transport::print::print
::languages
% languages
<?xml version='1.0'?>
<SOAP-ENV:Envelope
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Body>
<ns:languages xmlns:ns="http://www.soaplite.com/Demo" />
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
%
For debugging purposes it can be useful to see the XML that was
generated for the SOAP method request at each end. The
SOAP::dump command will return the XML for the last
transaction using HTTP for a named method.
Using Complex Variable TypesThe SOAP protocol specifies a number of variable types in the default encoding style. Most of these are simply mapped into Tcl however the Array and Struct types create a special problem for Tcl users. Given a string such as "The cat sat on the mat" there is no way in Tcl to determine if this is a string of 6 words, a list with 6 elements or the list representation of a three element array. In SOAP terms, these would be a string, an array of strings, or a struct. To properly implement SOAP clients we must provide a method by which the SOAP framework can determine what the parameters should be mapped into. This is achieved by the -params configuration option and the rpcvar package. The -params option is used the specify both the number of parameters, their names and their types. In SOAP, the names of the parameters is supposed to be more important than their positions. So:
Structs can be handled in a similar fashion. The simplest case of a structure of simple guessable types can be handled by specifying a type of struct. In this case the parameter is expected to be a list of name-value pairs. For more complex types, particularly structs containing structs we need to define a new type. This is done using the typedef procedure from the rpcvar package. package require rpcvar
namespace import -force rpcvar::typedef
typedef {
intValue int
floatValue float
stringValue string
} simpleStruct
SOAP::create a -params {myStruct simpleStruct}
a {intValue 2 stringValue "Hello, World!" floatValue 2.5}
An example using a nested struct type can be found in the samples/soapinterop.tcl file shipped with TclSOAP. Methods
DownloadThe package can be downloaded from the SourceForge project site . This file should be unpacked somewhere handy and you should set your TCLLIBPATH environment variable to suit. For windows users, you may as well unpack it to X:\Program Files\Tcl\lib\ You can also obtain the source via anonymous CVS or browse the CVS repository. You might also find recent news and update information on the TclSOAP project page. This is also the place to report bugs, submit patches and discuss any issues. If you would like to make a donation to support development of this software, you may do so via PayPal. This is not required and in no way affects the free licensing of this software. | ||||||||||||
| $Id: TclSOAP.html,v 1.21.2.1 2003/06/12 22:51:08 patthoyts Exp $ | ||||||||||||