public LookupAmazonListingResponse LookupAmazonListings(LookupAmazonListingRequest lookupAmazonListingRequest)
        {
            Dictionary <string, decimal> amazonListings = new Dictionary <string, decimal>();

            foreach (IEnumerable <string> batchedAsins in lookupAmazonListingRequest.ASINs.Batch(c_asinBatchSize))
            {
                Dictionary <string, string> optionalParameters = new Dictionary <string, string>();

                optionalParameters["ItemId"]        = string.Join(",", batchedAsins);
                optionalParameters["IdType"]        = IdType.ASIN.ToString();
                optionalParameters["Operation"]     = Operation.ItemLookup.ToString();
                optionalParameters["ResponseGroup"] = ResponseGroup.OfferFull.ToString();
                optionalParameters["Condition"]     = "New";
                optionalParameters["MerchantId"]    = "Amazon";

                ProductAdvApiResponse response = SignAndIssueRequest(optionalParameters);

                Dictionary <string, decimal> batchedAmazonListings = ItemLookupUtility.ReadAmazonListingResponse(response.Nodes);

                foreach (KeyValuePair <string, decimal> amazonListing in batchedAmazonListings)
                {
                    if (!amazonListings.ContainsKey(amazonListing.Key))
                    {
                        amazonListings.Add(amazonListing.Key, amazonListing.Value);
                    }
                }
            }

            return(new LookupAmazonListingResponse
            {
                AmazonListings = amazonListings
            });
        }
Exemplo n.º 2
0
        public static ProductAdvApiResponse IssueRequest(string requestUrl, string namespaceWebServices)
        {
            Func <ProductAdvApiResponse> func = () =>
            {
                ProductAdvApiResponse returnValue;

                XmlDocument doc = new XmlDocument();

                try
                {
                    WebRequest  request  = WebRequest.Create(requestUrl);
                    WebResponse response = request.GetResponse();

                    Stream stream = response.GetResponseStream();

                    if (stream != null)
                    {
                        doc.Load(stream);
                    }

                    XmlNodeList nodeList = doc.GetElementsByTagName("Item");

                    List <XmlNode> nodes = new List <XmlNode>();

                    bool errored = true;

                    if (IsValidResponse(doc, namespaceWebServices))
                    {
                        errored = false;

                        nodes.AddRange(nodeList.Cast <XmlNode>());
                    }

                    returnValue = new ProductAdvApiResponse(nodes, errored);
                }
                catch (Exception)
                {
                    throw new RetryException("Throttled, attempt again after waiting.");
                }
                finally
                {
                    Thread.Sleep(c_millisecondsPerSecond / c_requestsPerSecond);                     //Make sure we don't run out of the allotted requests.
                }

                return(returnValue);
            };

            ProductAdvApiResponse productAdvApiResponse = func.Retry(c_numberOfRetries, c_timeToWaitToBecomeUnthrottled);

            if (productAdvApiResponse == null)
            {
                XmlNode node = new XmlDocument();

                productAdvApiResponse = new ProductAdvApiResponse(node, true);
            }

            return(productAdvApiResponse);
        }