internal AmazonAlbum(AmazonServer server, string awsAccessKeyId, string secretAccessKey) { if (string.IsNullOrWhiteSpace(awsAccessKeyId)) throw new ArgumentNullException("awsAccessKeyId"); if (string.IsNullOrWhiteSpace(secretAccessKey)) throw new ArgumentNullException("secretAccessKey"); _amazonServer = server; _awsAccessKeyId = awsAccessKeyId; _secretAccessKey = secretAccessKey; }
internal AmazonAlbum(AmazonServer server, string awsAccessKeyId, string secretAccessKey) { if (string.IsNullOrWhiteSpace(awsAccessKeyId)) { throw new ArgumentNullException("awsAccessKeyId"); } if (string.IsNullOrWhiteSpace(secretAccessKey)) { throw new ArgumentNullException("secretAccessKey"); } _amazonServer = server; _awsAccessKeyId = awsAccessKeyId; _secretAccessKey = secretAccessKey; }
/// <summary> /// Gets the domain for a specified AmazonServer. /// </summary> /// <param name="server">The server.</param> public static string GetDomain(AmazonServer server) { return(_amazonServers[server]); }
/// <summary> /// Search /// </summary> /// <param name="server">The server.</param> /// <param name="awsAccessKeyId">Required: Your AWSAccessKeyId.</param> /// <param name="secretAccessKey">Required: Your Secret Access Key.</param> /// <param name="artist">The artist.</param> /// <param name="album">The album.</param> /// <param name="keywords">The keywords.</param> /// <param name="page">The page.</param> public static AmazonSearchResponse Search(AmazonServer server, string awsAccessKeyId, string secretAccessKey, string artist, string album, string keywords, int page) { if (string.IsNullOrWhiteSpace(awsAccessKeyId)) { throw new ArgumentNullException("awsAccessKeyId"); } if (string.IsNullOrWhiteSpace(secretAccessKey)) { throw new ArgumentNullException("secretAccessKey"); } String amazonDomain = GetDomain(server); FixSearchString(ref artist); FixSearchString(ref album); FixSearchString(ref keywords); string sort = (String.IsNullOrEmpty(artist) ? "artistrank" : "titlerank"); List <PostData> postData = new List <PostData>(); postData.Add(new PostData("Service", "AWSECommerceService")); postData.Add(new PostData("AWSAccessKeyId", awsAccessKeyId)); postData.Add(new PostData("Operation", "ItemSearch")); postData.Add(new PostData("SearchIndex", "Music")); if (!string.IsNullOrEmpty(artist)) { postData.Add(new PostData("Artist", artist)); } if (!string.IsNullOrEmpty(album)) { postData.Add(new PostData("Title", album)); } if (!string.IsNullOrEmpty(keywords)) { postData.Add(new PostData("Keywords", keywords)); } postData.Add(new PostData("ItemPage", page.ToString())); postData.Add(new PostData("Sort", sort)); postData.Add(new PostData("Timestamp", string.Format("{0:yyyy-MM-dd}T{0:HH:mm:ss}Z", DateTime.UtcNow))); string hostHeader = string.Format("ecs.{0}", amazonDomain); string signature = GetSignature(postData, hostHeader, secretAccessKey); postData.Add(new PostData("Signature", signature)); string requestUri = string.Format("http://{0}{1}", hostHeader, HttpRequestUri); requestUri = Http.GetQueryString(requestUri, postData); byte[] byteResponse = Http.Get(requestUri); if (byteResponse == null) { throw new WebException(string.Format("Response from {0} was null", amazonDomain)); } string response = Encoding.UTF8.GetString(byteResponse); AmazonSearchResponse result = new AmazonSearchResponse(); result.TotalPages = 0; result.TotalResults = 0; XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(response); foreach (XmlNode node in xmlDocument.ChildNodes) { if (node.Name == "ItemSearchResponse") { foreach (XmlNode responseNode in node.ChildNodes) { if (responseNode.Name == "OperationRequest") { foreach (XmlNode opReqNode in responseNode.ChildNodes) { if (opReqNode.Name == "Errors") { string fullErrorMessage = ""; foreach (XmlNode errorNode in opReqNode.ChildNodes) { if (errorNode.Name == "Error") { string errorMessage = ""; string errorCode = ""; foreach (XmlNode errorItemNode in errorNode.ChildNodes) { if (errorItemNode.Name == "Code") { errorCode = errorItemNode.InnerText; } else if (errorItemNode.Name == "Message") { errorMessage = errorItemNode.InnerText; } } if (errorMessage != "" || errorCode != "") { if (errorCode != "") { errorMessage = string.Format("{0} ({1})", errorMessage, errorCode); } if (fullErrorMessage != "") { fullErrorMessage += "\n\n"; } fullErrorMessage += errorMessage; } } } if (fullErrorMessage != "") { throw new Exception(fullErrorMessage); } } } } else if (responseNode.Name == "Items") { foreach (XmlNode itemNode in responseNode.ChildNodes) { if (itemNode.Name == "TotalResults") { int totalResults; if (int.TryParse(itemNode.InnerText, out totalResults)) { result.TotalResults = totalResults; } } else if (itemNode.Name == "TotalPages") { int totalPages; if (int.TryParse(itemNode.InnerText, out totalPages)) { result.TotalPages = totalPages; } } else if (itemNode.Name == "Item") { AmazonAlbum albumItem = new AmazonAlbum(server, awsAccessKeyId, secretAccessKey); result.Items.Add(albumItem); foreach (XmlNode itemDetail in itemNode.ChildNodes) { if (itemDetail.Name == "ASIN") { albumItem.Asin = itemDetail.InnerText; } else if (itemDetail.Name == "DetailPageURL") { albumItem.DetailPageUrl = itemDetail.InnerText; } else if (itemDetail.Name == "ItemAttributes") { foreach (XmlNode itemAttribute in itemDetail.ChildNodes) { if (itemAttribute.Name == "Artist") { albumItem.Artist = itemAttribute.InnerText; } else if (itemAttribute.Name == "Manufacturer") { albumItem.Manufacturer = itemAttribute.InnerText; } else if (itemAttribute.Name == "Title") { albumItem.Album = itemAttribute.InnerText; } } } } } } } } } } return(result); }