Gem #102: SOAP/WSDL client part

by Pascal Obry —EDF R&D

Let's get started...

This is the second part of a two-part Gem series on SOAP and WSDL.

In this Gem we will be using a Web Service as described in a WSDL document. These services could be implemented in Java, C#, or Ada, because the WSDL is universal in the Web Services world.

In the previous Gem we generated a WSDL from a simple Ada spec. Let's use it to generate the necessary code to use these Web services. We again use the wsdl2aws tool, but this time to generate only the stubs:

$ wsdl2aws -f -noskel temperatures.wsdl

A set of packages is generated. Two are of interest to us at the moment, namely:

Package temperatures_service-types.ads, containing the types used by the Web services.

Package temperatures_service-client.ads, containing the Web services client spec.

For each Web Service routine, two specs are generated:

function To_Fahrenheit
  (C        : Celsius_Type;
   Endpoint : String := Temperatures_Service.URL;
   Timeouts : AWS.Client.Timeouts_Values := Temperatures_Service.Timeouts)
   return To_Fahrenheit_Result;

function To_Fahrenheit
  (Connection : AWS.Client.HTTP_Connection;
   C          : Celsius_Type)
   return To_Fahrenheit_Result;

--  Raises SOAP.SOAP_Error if the operation fails

The first connects and closes the connection for each call, whereas the second uses a persistent connection. The usage is straightforward. Now, let's build a small program which converts Celsius to Fahrenheit:

with Ada.Text_IO;
with Temperatures_Service.Client;
with Temperatures_Service.Types;

procedure SOAP_Client is
   use Ada;
   use Temperatures_Service;
   C : constant Types.Celsius_Type := 20.0;
   F : constant Types.Fahrenheit_Type := Client.To_Fahrenheit (C);
   package C_IO is new Text_IO.Float_IO (Types.Celsius_Type);
   package F_IO is new Text_IO.Float_IO (Types.Fahrenheit_Type);

begin
   Text_IO.Put ("Celsius    "); C_IO.Put (C, Aft => 1, Exp => 0);
   Text_IO.New_Line;
   Text_IO.Put ("Fahrenheit "); F_IO.Put (F, Aft => 1, Exp => 0);
   Text_IO.New_Line;
end SOAP_Client;

We can use the following simple project file to build this program:

with "aws";
project SOAP_Client is
   for Source_Dirs use (".");
   for Main use ("soap_client.adb");
end SOAP_Client;
$ gnatmake -gnat05 -Psoap_client

Now let's test it, first by starting the server we have built last week:

$ ./soap_server

Then running soap_client:

$ ./soap_client
Celsius    20.0
Fahrenheit 68.0

That's all there is to it. As we've shown, it's easy to use a Web Service in Ada when the WSDL is provided. It's still possible to use a Web Service without a WSDL, but in that case it would be necessary to hand-code it.

soap_client.zip