August 13, 2007
Accessing web services with Sleep
For a while, I considered adding an XML-RPC layer to Slumber, but after examining a few libraries it quickly became obvious that such a feature would be better and more simple to implement with scripting (using the HOES syntax). So here’s a quick tutorial showing how to use the Apache XML-RPC library with sleep.
First we will need to download the library and make sure that all the necessary jar files are added to our classpath.
Then you need to add these imports to your script:
- import java.net.URL;
- import org.apache.xmlrpc.client.*;
Let’s start with writing a subroutine that will create and return an XML-RPC client to the caller:
- # Creates a XML-RPC client for the given url
- sub xmlRpcCreateClient
- {
- # Create config
- $config = [new XmlRpcClientConfigImpl];
- [$config setServerURL : [new URL : $1]];
- # Create new client and feed it configuration
- $client = [new XmlRpcClient];
- [$client setConfig : $config];
- return $client;
- }
This will enable us to simple call the subroutine xmlRpcCreateClient with an url string as parameter, and a client for executing remote procedures will be returned.
Next, we need a subroutine to actually execute a remote procedure on some client. Keep in mind that this code is specific to the Apache XML-RPC library:
Now to actually create a client and execute a remote procedure, just do something like the following:
- $client = xmlRpcCreateClient("http://example.com");
- println(xmlRpcExecute($client, 'some_procedure', 'param1', 'param2'));
That’s all folks! This topic will be covered further in a later article, which will show how to create and use an XML-RPC server efficiently.