Amazon Web Services

Many of the steps in using the Amazon web services mirror those of the Google web services. There is a beta kit that you download, you have to register in order to get a key, a Java library is provided along with a sample application, and so on.

The Amazon people have not gone to quite so much trouble to hide the underlying complexities, so the process of getting code running is a bit more involved than with Google. On the other hand, you get a more realistic experience of engaging with web services. There were some bugs which prevented compilation in the beta version of the Amazon kit at the time I downloaded it. However it is possible to resolve all these and get code running.

Start by visiting http://www.amazon.com/gp/browse.html/?node=3434641 which is the download page. If this 404's, then search Google for “Amazon Web Services”. When you get to the right Amazon page, notice the links on the left of that page for a FAQ, the license details, and obtaining the client key, which Amazon calls a token. Obtain your token by clicking on the link and filling in your email address and a password. Your browser will load a new page, which shows your 14-character token. This will also be emailed to the address you gave.

Although it is still in Beta, the license agreement for Amazon web services is a bit longer than Google's, because Amazon is encouraging people to deploy applications around this. Indeed, they even sell one such application, written by an early adopter in the Beta program. That developer could have been you, if you'd tackled this a few months ago!

Contents of the Amazon Beta Kit

After downloading the file kit.zip, unzip it to create a directory called “kit” that contains a directory AmazonWebServices with these contents:

  • A readmefirst file describing the following information, giving advice, and telling you how to run the demo.

  • A directory containing some information on writing programs to use the services. Click on index.html to enter at the correct place.

  • A directory containing the Java code for the demo

  • A directory containing SOAP request Java code examples.

  • A directory containing SOAP response Java code examples.

  • A directory of XML response examples.

  • A directory of XSLT style sheet examples, showing how to use the Extensible Stylesheet Language Transformations library to morph the XML into more convenient forms.

When you put all this together, what you get is programmatic access to all the features of the Amazon website. This would allow you to build your own interface to Amazon.com, specialized to your own business. Amazon still runs an affiliate program paying a small kickback to websites that bring purchasers too them. Another possibility is to search Amazon and suggest books to people who are using some other application of yours. Say you run a travel agent business. As the last step in booking a vacation package for someone, you could automatically offer them a choice of guidebooks for that area.

Running the example program

Amazon supports two alternative entry points to their web services, one simple, the other complicated:

  • A REST entry point. REST is an acronym for Representational State Transfer, and it is the simple approach. It means you send web service requests in the form of requests to URLs. If there are any parameters, they are tacked onto the end of the URL, or posted like an HTML form in XML. The result comes back as text or an XML file, and may include more URLs where additional details are available. The server is essentially guiding the client through a state machine in which each URL in a response represents a new state that can be transferred to (hence Representational State Transfer). The REST protocol lets developers start building Web services right away, without needing any toolkits beyond what they normally use for network application development.

  • A SOAP (Simple Object Access Protocol) entry point. This is the complicated approach. It requires specific knowledge of the SOAP XML specification, and a SOAP toolkit to form requests and parse the results.

Here's what you need to do to run Amazon's SOAP example. I don't recommend you do this, except to convince yourself of the practical drawbacks of SOAP. It's more complicated than it needs to be.

  1. Download and install the Apache Axis package. There are eight jar files that you have to make visible to Java. Put them in any directory, and then put the pathname of the directory on the commandline like this:

    java -Djava.ext.dirs=/your/path/to/axisjars  rest-of-line

  2. The Axis code will read a WSDL file, and generate a Java library of classes and methods to access the promised services. The site is http://ws.apache.org/axis/

  3. Download the Amazon WSDL file, and run it through Axis. The file is at http://soap.amazon.com/schemas3/AmazonWebServices.wsdl

  4. Compile all the Java code created in step 2. These files can be found at kitAmazonWebServicesJavaCodeSamplecomamazonsoapaxis*.java

    You must compile under JDK 1.4. JDK 1.5 will issue compiler warnings about the use of the word enum.

  5. Now compile the example application code in directory AmazonWebServicesJavaCodeSample

    You will need to fix compilation errors in seven files. These are caused by incorrect use of letter case in several variable names. E.g., a method is declared as setWishlist_id() but it is invoked in another file as setWishlist_Id(). Change the files where it is invoked, not the files where it is declared.

  6. Run the program, by typing java run. That will bring up a Swing window looking like Figure 28-1.

    Figure 28-1. Window displayed when you type java run

    image
  7. On the “XML” tab in the window, select “ASIN/UPC code”.

  8. Paste your Amazon token in the field marked “Developer Token”.

  9. Paste “0131774298 “ in the field labelled “AsinSearch”.

  10. Press “send”.

    More quickly than a browser reference (because it does not have to render images or HTML), the text details for book with ISBN 0-13-1774-298, the Ugly Fish C Book, will show up in the lower panel, under the JTree icon that is labelled “result”. If you click on that icon you can open up successive levels of detail on the book.

A more RESTful way to access Amazon web services

Amazon's chief web services designer Jeff Barr has been quoted as saying 85% of Amazon's web service use is REST, and only 15% use SOAP. The REST approach is much, much simpler and quicker to get working. SOAP is more flexible if you need to do something unusual like secure http communication between three or more parties.

A SOAP envelope in XML will look something like this:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<gs:doAmazonSearch xmlns:gs="urn:AmazonSearch">
<token>myToken</token>
<asin>0-13-1774-298</asin>
</gs:doAmazonSearch>
</soap:Body>

</soap:Envelope>

The word “ASIN” is “Amazon Standard ID Number” which, for a book, is the ISBN. You can see how the SOAP includes the name of the method to call, and the names/values of the parameters.

REST (REpresentational State Transfer) is an architectural style, not a complicated over-the-wire protocol like SOAP. REST says that the web already has set of remote commands that can be applied to any URL on any server—the HTTP commands GET, POST, PUT, DELETE. These commands tell a web server which HTML page or image file to send. REST says that they can be used equally well to interact with programs. If the program needs data, HTTP/POST already allows the client to upload a stream of text. If there's only a small amount of data (a customer ID and an ISBN, say), tack it onto the end of the URL in the established practice of HTTP/GET.

Here's an example. Amazon supports REST queries. You can retrieve the same information by constructing the following URL. For ease of reading, I have broken it up onto several lines. To see it work without any programming at all, type the following text into a browser URL field. Use your actual client key, and do not type spaces or line feeds:

http://xml.amazon.com/onca/xml3?
   t=webservices-20
 & devt=<your client key>
 & AsinSearch=0131774298
 & type=lite
 & f=xml

The Amazon web service will respond with XML, the first few lines of which look like this:

<Request>
     <Args></Args>
</Request>
     <Details url="http://www.amazon.com/exec/obidos/ASIN/0131774298/webservices-20?dev-t=your key%26camp=2025%26link_code=xm2">
<Asin>0131774298</Asin>
<ProductName>Expert C Programming</ProductName>
<Catalog>Book</Catalog>
     <Authors>
        <Author>Peter van der Linden</Author>
     </Authors>
     <ReleaseDate>14 June, 1994</ReleaseDate>
     <Manufacturer>Prentice Hall PTR</Manufacturer>

     <ImageUrlSmall>
      http://images.amazon.com/images/P/0131774298.01.THUMBZZZ.jpg
     </ImageUrlSmall>

This is the same information sent to the demo SOAP program, which parsed it and displayed the results graphically. For learning purposes, it would probably be better if Amazon's SOAP program GUI just displayed the raw XML.

If you can assemble that URL, type it in manually and get results, it's trivial to see how a Java program can do the same thing. Chapter 27 shows code to do that, so I won't repeat it here.

As soon as your program reads the XML returned by the web service, it will parse it and extract the data of interest. The program may also use an XSLT (Extensible Stylesheet Language Transformation) library to transform the XML output into other markup languages, such as HTML or plain text. An XSLT stylesheet is a template governing the transformation, and is not related to the similarly named Cascading Style Sheet (CSS).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.235.23