1.5 Web Services in the Real World


Let's look at the applications in this book and see how they lend themselves to the web-services protocol they illustrate .

Chapter 3 gives a quick example of writing an XML-RPC client for the Meerkat news service from nothing more than an XML parser and an HTTP library. XML-RPC lends itself to this because there's no innate object orientation to the design of the news service, and the main audience for the news service is the programmers of the scripting languages that happen to best support XML-RPC.

Chapter 4 reimplements the Meerkat client using the three Perl XML-RPC toolkits to show how much simpler life is with a toolkit to do the heavy lifting for you. Each toolkit also implements another example, fetching an entry from one of several quote databases. This is a more complex example, with an API to implement as both server and client.

Here is a simple program that fetches and prints five Perl stories from Meerkat using the XMLRPC::Lite toolkit:

 #!/usr/bin/perl -w     use XMLRPC::Lite;     $client = XMLRPC::Lite   ->proxy('http://www.oreillynet.com/meerkat/xml-rpc/server.php')   ->on_fault(sub { die "Transport error: " .  $_[1]->faultstring });     $resp = $client->call('meerkat.getItems',                       { 'search' => '/[pP]erl/',                         num_items    => 5,                         descriptions => 75 })->result( );     foreach $story (@$resp) {   print $story->{description}, "\n";   print "  ", $story->{link}, "\n";   print "\n"; } 

This is the kind of output it produces:

 Deploy USE_GNOMENG infrastrcuture o USE_REINPLACE instead of PERL o Mark   http://www.FreshPorts.org/audio/freebirth/     Support for merging, speed improvements, support for XTMPath, and LTM.   http://www.garshol.priv.no/download/xmltools/prod/XTMBase.html     Michael Stevens compares two popular mail filtering tools, both written in   http://www.perl.com/pub/a/2002/08/27/filtering.html     Directory layouts of py-gtk and py-gnome packages have been changed, so tha   http://www.FreshPorts.org/mail/pmail/     Directory layouts of py-gtk and py-gnome packages have been changed, so tha   http://www.FreshPorts.org/editors/moleskine/ 

As you can tell from the program, the XMLRPC::Lite module encodes and decodes Perl values (we got back an array of hashes), sending the request via HTTP and parsing the response. The call( ) and result( ) methods hide the tricky stuff.

Chapter 6 shows two SOAP toolkits accessing the same web service, which translates numbers into words. Here's the SOAP::Lite client:

 #!/usr/bin/perl -w   use strict;   use SOAP::Lite;   my $num = shift; $num =~ /^\d+$/ or die "USAGE: 
 #!/usr/bin/perl -w   use strict;   use SOAP::Lite;   my $num = shift; $num =~ /^\d+$/ or die "USAGE: $0 num\n";   my $endpoint = 'http://www.tankebolaget.se/'                   .'scripts/NumToWords.dll/soap/INumToWords';   my $method_uri = 'urn:NumToWordsIntf-INumToWords';   my $num2words = SOAP::Lite     ->new(uri => $method_uri, proxy => $endpoint);   my $response = $num2words     ->NumToWords_English(SOAP::Data-> name (aNumber => $num));   die $response->faultstring if $response->fault;   print "$num may be expressed as ", $response->result, "\n"; 
num\n"; my $endpoint = 'http://www.tankebolaget.se/' .'scripts/NumToWords.dll/soap/INumToWords'; my $method_uri = 'urn:NumToWordsIntf-INumToWords'; my $num2words = SOAP::Lite ->new(uri => $method_uri, proxy => $endpoint); my $response = $num2words ->NumToWords_English(SOAP::Data->name(aNumber => $num)); die $response->faultstring if $response->fault; print "$num may be expressed as ", $response->result, "\n";

While more complex than the XML-RPC example, this shows fault handling ( if ($response->fault) ), method calls, and how to hardcode the type of a piece of data (the SOAP::Data object). If the SOAP::Lite methods look a lot like the XMLRPC::Lite methods, it's because they're built from a single framework.

The extended application developed in Chapter 7 is a shopping-cart system, which illustrates real-world issues of authentication, database abstraction, and interface design. Chapter 8 runs the shopping-cart system on transports other than HTTP (such as the Jabber message system) and shows how to do in other transports what you get for free with HTTP.

Chapter 9 builds a WSDL file to describe the shopping-cart API. You'll see how the APIs we designed in Chapter 7 translate to WSDL. We don't revisit the shopping cart for Chapter 11 on REST, though; instead, we develop a simple book catalog system to illustrate the principles of REST. Chapter 12 is an overview of the advanced topics in web services, such as security and performance.



Programming Web Services with Perl
Programming Web Services with Perl
ISBN: 0596002068
EAN: 2147483647
Year: 2000
Pages: 123

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net