Exercise: Accessing Amazon ECS

Một phần của tài liệu Beginning ASP.NET 2.0 E-Commerce in C# 2005 From Novice to Professional PHẦN 10 potx (Trang 25 - 31)

1. Let’s begin by adding a Web Reference to the Amazon ECS. This is only required if you plan to access ECS through SOAP. Right-click the project entry in Solution Explorer and choose Add Web Reference.

2. In the dialog box that appears, enter the address of the ECS Web Service:

http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl 3. Click Go.

4. Change the Web Reference name to something easier to type, such as AmazonEcs, and click Add Reference (see Figure 17-5). A new folder in your project named App_WebReferences will contain your new reference to the Amazon ECS.

Figure 17-5. Adding a new Web Reference to your project

5. Open web.config and add the following configuration parameters:

<appSettings>

<add key="CartPersistDays" value="10" />

<add key="MailServer" value="localhost" />

<add key="EnableErrorLogEmail" value="false" />

<add key="ErrorLogEmail" value="cristian_darie@yahoo.com" />

<add key="ProductsPerPage" value="6"/>

<add key="ProductDescriptionLength" value="60"/>

<add key="SiteName" value="BalloonShop"/>

<add key="AmazonRestUrl"

value="http://webservices.amazon.com/onca/xml?Service=AWSECommerceService"/>

<add key="AmazonSubscriptionID" value="1R4EY7DQY0ATN521WQR2"/>

<add key="AmazonAssociateID" value="cristiand-20"/>

<add key="AmazonSearchKeywords" value="balloons"/>

<add key="AmazonSearchIndex" value="Toys"/>

<add key="AmazonResponseGroups" value="Request,Medium"/>

<add key="AmazonEcs.AWSECommerceService"

value="http://soap.amazon.com/onca/soap?Service=AWSECommerceService"/>

</appSettings>

6. Add the following properties to the BalloonShopConfiguration class to allow for easier retrieval of the data you saved to web.config:

// Amazon ECS REST URL

public static string AmazonRestUrl {

get {

return ConfigurationManager.AppSettings["AmazonRestUrl"];

} }

// Subscription ID to access ECS public static string SubscriptionId {

get {

return ConfigurationManager.AppSettings["AmazonSubscriptionID"];

} }

// the Amazon.com associate ID public static string AssociateId {

get {

return ConfigurationManager.AppSettings["AmazonAssociateID"];

} }

// keywords used to do the Amazon search public static string SearchKeywords {

get {

return ConfigurationManager.AppSettings["AmazonSearchKeywords"];

} }

// search location

public static string SearchIndex {

get {

return ConfigurationManager.AppSettings["AmazonSearchIndex"];

} }

// the Amazon response groups public static string ResponseGroups {

get {

return ConfigurationManager.AppSettings["AmazonResponseGroups"];

} }

7. Add a new class named AmazonAccess to your App_Code folder.

8. Start coding your new class by adding references to the System.Xml and AmazonEcs namespaces:

using System.Xml;

using AmazonEcs;

9. Continue by adding the GetResponseTable method, which returns a configured DataTable object to be filled with the Amazon search results. This DataTable will ultimately be returned to the presentation tier, which will display the product data to the visitors.

public class AmazonAccess {

public AmazonAccess() {

//

// TODO: Add constructor logic here //

}

// returns a configured DataTable object that can be read by the UI private static DataTable GetResponseTable()

{

DataTable dt = new DataTable();

dt.Columns.Add(new DataColumn("ASIN", Type.GetType("System.String")));

Columns.Add(new DataColumn("ProductName", Type.GetType("System.String")));

dt.Columns.Add(new DataColumn("ProductPrice", Type.GetType("System.String")));

dt.Columns.Add(new DataColumn("ProductImageUrl", Type.GetType ("System.String")));

return dt;

} }

10. Add the GetAmazonDataWithSoap method, which does pretty much what it says.

// perform the Amazon search with SOAP and return results as a DataTable public static DataTable GetAmazonDataWithSoap()

{

// Create Amazon objects

AWSECommerceService amazonService = new AWSECommerceService();

ItemSearch itemSearch = new ItemSearch();

ItemSearchRequest itemSearchRequest = new ItemSearchRequest();

ItemSearchResponse itemSearchResponse;

// Setup Amazon objects

itemSearch.SubscriptionId = BalloonShopConfiguration.SubscriptionId;

itemSearchRequest.Keywords = BalloonShopConfiguration.SearchKeywords;

itemSearchRequest.SearchIndex = BalloonShopConfiguration.SearchIndex;

itemSearchRequest.ResponseGroup =

BalloonShopConfiguration.ResponseGroups.Split(',');

itemSearch.Request = new AmazonEcs.ItemSearchRequest[1]

{ itemSearchRequest };

// Will store search results

DataTable responseTable = GetResponseTable();

// If any problems occur, we prefer to send back empty result set // instead of throwing exception

try {

itemSearchResponse = amazonService.ItemSearch(itemSearch);

Item[] results = itemSearchResponse.Items[0].Item;

// Browse the results

foreach (AmazonEcs.Item item in results) {

// product with incomplete information will be ignored try

{

8213592a117456a340854d18cee57603

//create a datarow, populate it and add it to the table DataRow dataRow = responseTable.NewRow();

dataRow["ASIN"] = item.ASIN;

dataRow["ProductName"] = item.ItemAttributes.Title;

dataRow["ProductImageUrl"] = item.SmallImage.URL;

dataRow["ProductPrice"] = item.OfferSummary.LowestNewPrice.

FormattedPrice;

responseTable.Rows.Add(dataRow);

} catch {

// Ignore products with incomplete information }

} }

catch (Exception e) {

// ignore the error }

// return the results return responseTable;

}

11. Finally, add the GetAmazonDataWithRest method:

// perform the Amazon search with REST and return results as a DataTable public static DataTable GetAmazonDataWithRest()

{

// The response data table

DataTable responseTable = GetResponseTable();

// Compose the Amazon REST request URL

string amazonRequest = string.Format("{0}&SubscriptionId=

{1}&Operation=ItemSearch&Keywords={2}&SearchIndex={3}&ResponseGroup={4}", BalloonShopConfiguration.AmazonRestUrl,

BalloonShopConfiguration.SubscriptionId, BalloonShopConfiguration.SearchKeywords, BalloonShopConfiguration.SearchIndex, BalloonShopConfiguration.ResponseGroups);

// If any problems occur, we prefer to send back empty result set // instead of throwing exception

try {

// Load the Amazon response

XmlDocument responseXmlDoc = new XmlDocument();

responseXmlDoc.Load(amazonRequest);

// Prepare XML document for searching

XmlNamespaceManager xnm = new XmlNamespaceManager

(responseXmlDoc.NameTable);

xnm.AddNamespace("amz", "http://webservices.amazon.com/

AWSECommerceService/2005-07-26");

// Get the list of Item nodes

XmlNodeList itemNodes = responseXmlDoc.SelectNodes("/amz:Item SearchResponse/amz:Items/amz:Item", xnm);

// Copy node data to the DataTable foreach (XmlNode itemNode in itemNodes) {

try {

// Create a new datarow and populate it with data DataRow dataRow = responseTable.NewRow();

dataRow["ASIN"] = itemNode["ASIN"].InnerText;

dataRow["ProductName"] =

itemNode["ItemAttributes"]["Title"].InnerText;

dataRow["ProductImageUrl"] = itemNode["SmallImage"]["URL"].InnerText;

dataRow["ProductPrice"] = itemNode["OfferSummary"]

["LowestNewPrice"]["FormattedPrice"].InnerText;

// Add the row to the results table responseTable.Rows.Add(dataRow);

} catch {

// Ignore products with incomplete information }

} } catch {

// Ignore all possible errors }

return responseTable;

}

How It Works: Working with Amazon ECS

The important points you need to understand from this exercise follow:

• The search parameter commands were saved to web.config, so you can do various changes to the behavior of the “Amazon Balloons” pages without modifying any C# code.

• To enable accessing ECS, you added a Web Reference to its WDSL file. Visual Studio was kind enough to do the rest for you, giving you direct access to the classes exposed by Amazon. To make your life even easier, you wrote a reference to the AmazonEcs namespace at the beginning of your file.

• The heart of the AmazonAccess class is composed of the two GetAmazon... methods, which offer identical functionality to the presentation tier, but using different access technologies: one uses SOAP;

the other uses REST.

To understand these two methods, we suggest starting with the REST one. After composing the REST URL, the request is submitted to Amazon, and the results are saved to an XML document (which will look something like what you saw in Figure 17-1 and Figure 17-2).

// Load the Amazon response

XmlDocument responseXmlDoc = new XmlDocument();

responseXmlDoc.Load(amazonRequest);

The logic that follows is extremely simple. On the retrieved XML document we make an Xpath query to filter the Item nodes, because these contain the data we’re interested in. By analyzing the response hierarchy, you will know what nodes to ask for to get your data. We also catch and ignore the eventual exceptions that can happen in case one of the nodes we’re trying to query doesn’t exist.

Although the logic is simple enough, its implementation details can look a bit confusing if you’re not used to parsing and working with XML documents. We strongly recommend that you grab some additional documentation on that topic, as its importance to everyday development projects has increased dramatically in the past few years.

GetAmazonDataWithSoap does the exact same actions, but this time by using the Amazon ECS API and the classes provided by it, instead of manually parsing an XML response file.

Một phần của tài liệu Beginning ASP.NET 2.0 E-Commerce in C# 2005 From Novice to Professional PHẦN 10 potx (Trang 25 - 31)

Tải bản đầy đủ (PDF)

(74 trang)