Example #1
0
		/// <summary>
		/// Creates a list of the top ten products in a category, wraps those
		/// product names and Amazon Urls in a Product object and appends the
		/// Constants.AMAZON_TAG_IDENTIFIER onto the Url of the product link.
		/// </summary>
		/// <param name="category">the category id used to locate the list of
		/// top ten products.</param>
		/// <returns>a list of Product objects</returns>
		/// <remarks>
		/// IMPORTANT: the value indicated in Constants.AMAZON_TAG_IDENTIFIER
		/// MUST be included in the DetailPageURL of every product.
		/// </remarks>
		public List<Product> GetTopTenProducts(Category category)
		{
			List<Product> ret = new List<Product>();

			// create a list of request parameters
			Dictionary<string, string> requestParameters = new Dictionary<string, string>();
			requestParameters.Add("Service", "AWSECommerceService");
			requestParameters.Add("Version", Constants.AMAZON_API_VERSION);
			requestParameters.Add("Operation", "BrowseNodeLookup");
			requestParameters.Add("BrowseNodeId", category.CategoryId);
			requestParameters.Add("ResponseGroup", "TopSellers");

			string signedUrl = Amazon.Sign(requestParameters);
			XNamespace ns = Constants.AMAZON_DOCUMENT_NAMESPACE;

			// contact Amazon retrieve the list of BrowseNodes as XML
			var root = XElement.Load(signedUrl);

			// get the list of all children under BrowseNodes/BrowseNode/Children/BrowseNode
			var nodes = from node in root
							.Element(ns + "BrowseNodes")
							.Element(ns + "BrowseNode")
							.Element(ns + "TopItemSet")
							.Descendants(ns + "TopItem")
						select new
						{
							Asin = node.Element(ns + "ASIN").Value,
							Title = node.Element(ns + "Title").Value,
							DetailPageURL = node.Element(ns + "DetailPageURL").Value
						};

			List<string> asins = new List<string>();
			foreach (var node in nodes)
			{
				string url = GetAmazonUrlWithTag(node.DetailPageURL);

				Product product =
					new Product(node.Title,
						new Uri(url),
						node.Asin);
				ret.Add(product);
				asins.Add(product.Asin);
			}

			return ret;
		}