Amazon Web Services Library

The Amazon ECS Library is an ActionScript library used to make requests to Amazon’s E-Commerce Service. It’s simple to use and allows you to get up and running really quickly. Amazon E-Commerce Service (ECS) exposes Amazon’s product data and e-commerce functionality. This allows developers, web site owners and merchants to leverage the data and functionality that Amazon uses to power its own e-commerce business. ECS 4.0 makes it extremely easy for developers to build rich, highly effective web sites and applications.

There are two libraries, one for Actionscript 2.0 and one for ActionScript 3.0. Follow the links below for information about either one.

ActionScript 2.0 Library

Download

Download the Actionscript 2.0 library for Amamzon ECS here.

How to use

Before you do anything you’ll need to place the “com” folder from the zip into your class path.
The first step after that is to import the needed classes:

import com.amazon.client.ECS;
import com.amazon.utils.ECSParamCollection;

The next thing you’ll need to do is instantiate the ECS class. The constructor takes one optional parameter for the ECS locale. So, for instance, if you wanted to use the Japanese version of the ECS you’d create your instance like this:

var client:ECS = new ECS("JP");

If you leave this parameter empty, like we will for this example, it will default to the US version of ECS.

var client:ECS = new ECS();

The next step is to set up the event listeners. To do so, you need to create a function for each operation type to wish to execute. It’s also recommended to implement a listener for errors as well. In this example we’re just working with the ItemSearch operation, so we’ll add a listener for that as well as an Error listener like this:

function onItemSearchResponse( evt:Object )
{
     for( var i in evt.response.Items.Item )
     {
          //output the title of all of the results
          trace( evt.response.Items.Item[i].ItemAttributes.Title.data );
     }
}
function onErrorResponse( evt:Object )
{
     //output the error
     trace( evt.response.Items.Request.Error.Error.Message.data);
}
client.addEventListener( ECS.ITEMSEARCH, mx.utils.Delegate.create( this, onItemSearchResponse ) );
client.addEventListener( "Error", mx.utils.Delegate.create( this, onErrorResponse ) );

As you can see the event listeners take an object as the parameter and the result data is stored in the “response” member of that object.

The next step is to build the request. Since there are a ton of parameters for the ECS api, the ECSParamColletion makes dealing with them very easy. The ECSParamColletion class takes all of the possible parameters you need for your request and formats the properly for the request.

var requestParams:ECSParamCollection = new ECSParamCollection();
requestParams.ResponseGroup = ECS.SMALL_RG;
requestParams.SearchIndex = ECS.BOOKS_SI;
requestParams.Keywords = "Cheese";

This example sets up the parameters to search for books with the keyword “Cheese” and will return the “Small” response group. All of the response groups, search indices, and operation types have been enumerated in the ECS class for convenience.

The last step is to actually execute the request. To execute the request call the function executeOperation(). This function takes two parameters. The first parameter is the operation type and the second parameter is the ECSParamCollection object that holds all of our request parameters. The following code will execute an ItemSearch request. Again all of the operation types are enumerated for convenience.

client.executeOperation(ECS.ITEMSEARCH, requestParams);

If everything goes as planned the onItemSearchResponse function should be called once the response has been received. If there was an error, the onErrorResponse function will be called.

Complete Example

import com.amazon.client.ECS;
import com.amazon.utils.ECSParamCollection;
 
var client:ECS = new ECS();
 
function onItemSearchResponse( evt:Object )
{
	for( var i in evt.response.Items.Item )
	{
		//output the title of all of the results
		trace( evt.response.Items.Item[i].ItemAttributes.Title.data );
	}
}
function onErrorResponse( evt:Object )
{
	//output the error
	trace( evt.response.Items.Request.Error.Error.Message.data);
}
client.addEventListener( ECS.ITEMSEARCH, mx.utils.Delegate.create( this, onItemSearchResponse ) );
client.addEventListener( "Error", mx.utils.Delegate.create( this, onErrorResponse ) );
 
var requestParams:ECSParamCollection = new ECSParamCollection();
requestParams.ResponseGroup = ECS.SMALL_RG;
requestParams.SearchIndex = ECS.BOOKS_SI;
requestParams.Keywords = "Cheese";
 
client.executeOperation(ECS.ITEMSEARCH, requestParams);

ActionScript 3.0 Library

Download

Download the Actionscript 2.0 library for Amamzon ECS here.

How to use

I tried to make the ActionScript 2.0 and ActionScript 3.0 libraries as similar as possible, so just read the instructions for the ActionScript 2.0 library first and then check back here for the differences.

Differences

  • Make sure to import the ECSEvent class like so:
    import com.amazon.events.ECSEvent;
  • Because of the changes in ActionScript 3.0 you don’t need to use the Delegate class for the event listeners. The addEventListener Statements should look like this:
    client.addEventListener( ECS.ITEMSEARCH, onItemSearchResponse );
    client.addEventListener( "Error", onErrorResponse );
  • The event listeners should now take an ECSEvent object as it’s parameter like this:
    function onItemSearchResponse( evt:ECSEvent ):void { ... }
    function onErrorResponse( evt:ECSEvent ):void { ... }
  • Lastly, since ActionScript 3.0 actually adheres to XML namespaces, you’ll need to add the following directive at the top of your file somewhere:
    default xml namespace = "http://ecs.amazonaws.com/AWSECommerceService/2004-03-19"

    This might not be the ideal way for you to deal with the namespace issue and if it’s not, I’ll be covering other ways to deal with namespaces on my blog shortly.

Complete Example

import com.amazon.client.ECS;
import com.amazon.utils.ECSParamCollection;
import com.amazon.events.ECSEvent;
 
default xml namespace = "http://ecs.amazonaws.com/AWSECommerceService/2004-03-19"
 
var client:ECS = new ECS();
 
function onItemSearchResponse( evt:ECSEvent ):void
{
	for( var i in evt.response.Items.Item )
	{
		//output the title of all of the results
		trace( evt.response.Items.Item[i].ItemAttributes.Title );
	}
}
function onErrorResponse( evt:Object ):void
{
	//output the error
	trace( evt.response.Items.Request.Error.Error.Message);
}
client.addEventListener( ECS.ITEMSEARCH, onItemSearchResponse );
client.addEventListener( "Error", onErrorResponse );
 
var requestParams:ECSParamCollection = new ECSParamCollection();
requestParams.ResponseGroup = ECS.SMALL_RG;
requestParams.SearchIndex = ECS.BOOKS_SI;
requestParams.Keywords = "Cheese";
 
client.executeOperation(ECS.ITEMSEARCH, requestParams);

Conclusion

The library should be very easy to use. Take a look at the code, read the ECS documentation, or email me at podman@gmail.com if you have any questions.

Leave a Reply