Dive into ECS
ECS is the Amazon service that most programmers use. It has great appeal for developers for several reasons:
- It provides access to the vast amount of information contained in Amazon's product databases.
- It can be used as an opportunity to make money through the Amazon Affiliates program.
- Access to ECS is free of charge.
Note that ECS is a read-only service: You can't submit information to Amazon using ECS. Instead, you must use alternate channels and APIs to add or modify product listings, customer reviews, and so on. The only exception is that you can create and fill shopping carts for users and submit those carts to Amazon for presentment and approval by the user.
ECS can be used by stand-alone Java applications or by web applications. The ECS Developer Guide lists a variety of possible uses for ECS:
- Presenting detailed information about Amazon products on your own site, including up-to-date prices
- Monitoring statistics for how well products are doing or how competitors are pricing their products
- Building a niche online store using Amazon's own technology
- Developing software products and services for other ECS users
Most of these uses are commercial in nature, but it doesn't have to be that way. If you're building software to organize book or music collections, for example, you can quickly download cover art for a given book or album using a few simple ECS calls. (Note: Be sure to read the AWS licensing terms closely for restrictions on using cover art and other copyrighted materials that Amazon provides.)
ECS is also a great way to experiment with web services in general, because the services that AWS provides are easily understood (everyone can relate to browsing and purchasing books and menus) and are free to use.
You can access all services available through ECS in one of two ways:
- As Representational State Transfer (REST) requests
- As SOAP requests
The first way -- through REST -- is the most simple to understand. REST uses HTTP as its underlying transport. You pass REST commands and options to ECS by using URL query parameters; ECS returns an XML document containing the result. This process makes it possible to invoke ECS services directly from a Web browser, as in this example:
The result is an XML document describing the book with ISBN 0321321146:
<?xml version="1.0" encoding="UTF-8"?> <ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2005-03-23"> <OperationRequest> <HTTPHeaders> <Header Name="UserAgent" Value="Mozilla/5.0 Gecko/20041001 Firefox/0.10.1"/> </HTTPHeaders> <RequestId>0PJ4DG95DYN2JGQ7ZSYE</RequestId> <Arguments> <Argument Name="Service" Value="AWSECommerceService"/> <Argument Name="Style" Value="http://www.ericgiguere.com/tohtml.xsl"/> <Argument Name="SubscriptionId" Value="xxxx"/> <Argument Name="ItemId" Value="0321321146"/> <Argument Name="Operation" Value="ItemLookup"/> </Arguments> <RequestProcessingTime>0.0185689926147461</RequestProcessingTime> </OperationRequest> <Items> <Request> <IsValid>True</IsValid> <ItemLookupRequest> <ItemId>0321321146</ItemId> </ItemLookupRequest> </Request> <Item> <ASIN>0321321146</ASIN> <DetailPageURL>http://www.amazon.com/exec/obidos/redirect?tag=ws%26link_code=xm2 %26camp=2025%26creative=165953%26path= http://www.amazon.com/gp/redirect.html%253fASIN=0321321146 %2526location= /o/ASIN/0321321146%25253FSubscriptionId=1WCK32SN05PGBJ0YV302</DetailPageURL> <ItemAttributes> <Author>Eric Giguere</Author> <ProductGroup>Book</ProductGroup> <Title>Make Easy Money with Google : Using the AdSense Advertising Program</Title> </ItemAttributes> </Item> </Items> </ItemLookupResponse>
You can even ask ECS to apply an Extensible Stylesheet Language Transformation (XSLT) stylesheet to the XML it returns, which is useful for transforming the Amazon data into a different format.
The more conventional approach to web services is to use SOAP. For each of its web sites, Amazon supplies Web Services Description Language (WSDL) documents that you use to import the ECS services into whatever web services toolkit you're using.
Which approach you take -- REST or SOAP -- is entirely up to you. Amazon supports both systems equally well.
Two general types of operation are available to ECS:
You use a
lookup to find detailed information about things like products, merchants, and even customers. When looking up products, you can request information for up to 10 products. The previous example looked up information for a single book by its ISBN using the
ItemLookup request. (Products are identified in a number of ways, chiefly by the Amazon Standard Identification Number (ASIN). For books, ASIN and ISBN values are equivalent.) The following
lookup operations are available in ECS 4.0:
As you might guess,
ItemLookup is the most common
search operation searches the Amazon databases for information using criteria you supply. For example, you can get a list of all the books in the Amazon database written by a specific author. The following
search operations are available in ECS 4.0:
The shopping cart operations are:
Finally, you can use a
Help operation to obtain help for other operations.
Options control how much information an operation returns and how that information is returned. As already mentioned, you can apply an XSLT stylesheet to the XML (for REST requests only) simply by passing the URL of the stylesheet (which must be available on a public Internet site) to Amazon as part of the request. You can use response groups to control the level of detail returned. Response groups tell AWS which subset of the possible data to return -- for example, do you just want basic information about a book (title, author, ranking, price) or do you want detailed information (customer reviews, availability from third-party stores, images).
To ensure that its systems are available for everyone's use, Amazon limits how quickly you can make successive requests to AWS. Each IP address is limited to one request per second. If your application is going to make several requests in a short span of time, you'll have to spread them out over several seconds, which can lead to unacceptable performance, especially if the Amazon servers are very busy. Thus, caching becomes an important part of any AWS-based application.
When making REST requests, for example, you can use a simple cache built around the
java.util.HashMap class, where the URL of the request acts as the key. Every time you make a successful AWS request, place the response data (ideally already transformed into whatever format makes sense for your application so that you don't have to keep re-parsing the XML) in the cache.
Note that Amazon restricts the amount of time you can cache information. According to the AWS licensing agreement, most data can be cached for no more than 24 hours, though some of the static product information -- things like author names, product names, and release dates -- can be cached for up to three months. Pricing and availability information can be cached for up to a week, but if you don't refresh the information on an hourly basis, you must include a timestamp with the information whenever it's displayed. Refer to Section D of the AWS licensing agreement for all the specifics.