Web Service Toolkit

From Lazarus wiki
Revision as of 16:10, 3 May 2006 by Inoussa (talk | contribs)
Jump to navigationJump to search

"Web Service Toolkit” is a web services package for FPC and Lazarus; “Web Service Toolkit” is meant to ease web services consumption by FPC and Lazarus users.

Overview

“Web Service Toolkit” is made of two parts, a command line tool “ws_helper” and a collection of support units. Given an interface definition file( describing à web service ), “ws_helper” will create a FPC unit containing a proxy implementing that interface. At runtime when a call targeting the web service is issued, the proxy's role is to :

  • marshall the call paramaters,
  • make the call to the target web service,
  • receive the call return and unmarshall output parameters to the caller.

Behind the scene, the proxy will take care of the SOAP plumbing details.

Example

We will use the “google web api”, freely available for personal use at this adress “http://www.google.com/apis/”.In order to use this service, we have to translate its exposed WSDL interface to Pascal langage. By now “Web Service Toolkit” does not contain a WSDL to pascal compiler, so we will assume this translation available as below ( this is an incomplete translation, but it's enough for the sample ).

 Unit googlewebapi;
 {$mode objfpc}{$H+}
 interface
 uses SysUtils, Classes;
 Type
   IGoogleSearch = Interface
     function doSpellingSuggestion(
       const key:string;
       const phrase:string
     ):string;
   End;
 Implementation
 End.

Invoking “ws_helper” at the prompt with the file “googlewebapi.pas” as argument will produce a file “googlewebapiimpunit.pas” as below.

 Unit googlewebapiimpunit;
 {$mode objfpc}{$H+}
 Interface
 Uses SysUtils, Classes, service_intf, googlewebapi;
 Type
   TGoogleSearch_Proxy=class(TBaseProxy,IGoogleSearch)
     Protected
       function doSpellingSuggestion(
         Const key : string;
         Const phrase : string
       ):string;
   End;
 Implementation
 { TGoogleSearch_Proxy implementation }
 function TGoogleSearch_Proxy.doSpellingSuggestion(
   Const key : string;
   Const phrase : string
 ):string;
 Var
   locSerializer : TAbstractFormater;
   strPrmName : string;
   resTypeInfo : PTypeInfo;
 Begin
   locSerializer := GetSerializer();
   Try
     locSerializer.BeginCall('doSpellingSuggestion', GetTarget());
       locSerializer.Put('key', TypeInfo(string), key);
       locSerializer.Put('phrase', TypeInfo(string), phrase);
     locSerializer.EndCall();
     MakeCall();
     locSerializer.BeginCallRead();
       resTypeInfo := TypeInfo(string);
       If ( resTypeInfo^.Kind in [tkClass,tkObject,tkInterface] ) Then
         Pointer(Result) := Nil;
       strPrmName := 'return';
       locSerializer.Get(TypeInfo(string), strPrmName, result);
   Finally
     locSerializer.Clear();
   End;
 End;
 End.

Then we can build a sample program for the “IGoogleSearch” service(see below). In order to compile this program you must have Indy ( tested with Indy10 ) which can be found at this location “http://www.indyproject.org/Sockets/index.en.iwp” or ICS( tested with the latest ICS-V5 Distribution ) at that adress “http://www.overbyte.be/frame_index.html” as it is used for the HTTP protocol.

 program test_google_api;
 {$mode objfpc}{$H+}
 uses
   Classes, SysUtils,
   service_intf, soap_imp,
   //indy_http_protocol,
   ics_http_protocol,
   googlewebapi, googlewebapiimpunit;
 Const
   sADRESS = 'http:Adress=http://api.google.com/search/beta2';
   sTARGET = 'urn:GoogleSearch';
   sKEY    = '<your google key here>';
   sSERVICE_PROTOCOL = 'SOAP';
 Var
   tmpObj : IGoogleSearch;
   strBuffer : string;
 begin
   ICS_RegisterHTTP_Transport();
   WriteLn();
   WriteLn('Enter phrase to spell :');
   ReadLn(strBuffer);
   tmpObj := TGoogleSearch_Proxy.Create(sTARGET,sSERVICE_PROTOCOL,sADRESS);
   Try
     strBuffer := tmpObj.doSpellingSuggestion(sKEY,strBuffer);
     WriteLn('google spell >>> ',strBuffer);
   Except
     On E : Exception Do
       WriteLn(E.Message);
   End;
   ReadLn();
 end.

The units service_intf, soap_imp, indy_http_protocol, ics_http_protocol as provided with this toolkit; Below is the result of a execution session spelling for “freepscal lzarus” written with a missing letter 'a' beetwen the letter 'p' and the the letter 's' and a missing letter 'a' beetwen the letter 'L' and the the letter 'z' .

 > .\tests\google_api\test_google_api.exe
 Enter phrase to spell :
 freepscal lzarus
 google spell >>> freepascal lazarus

Google spells it correctly : “freepascal lazarus”!

Go further

The complete example is located in the “tests\google_api” folder. It demonstrate use of class and array data types by google searching. The core part of the toolkit is the units service_intf and soap_imp; The first one defines the basic interface while the second is a SOAP implementation( base on the FCL XML units).

Status

The toolkit is usable for simple types and for class types. The serialization is designed to allow customization of basic types and class types by implementing classes derived from “TBaseRemotable”. This classes have to be registered in the type registry.

TODO( near future )

  • Basic array support
  • XML-RPC formatter
  • More documentation and samples !

TODO

Extend the toolkit to Server side for :

  • XML serialization mostly for SOAP over HTTP and XML-RPC
  • Binary serialization
  • TCP transport implementation.

Download

http://inoussa12.googlepages.com/webservicetoolkitforfpc%26lazarus

Author

Inoussa OUEDRAOGO, http://inoussa12.googlepages.com/