예제 #1
0
        private IList <ItemBuyBoxResponse> RequestItemsBuyBoxByProductApi(ILogService logger, IList <ItemDTO> items, AmazonApi api)
        {
            var results = new List <ItemBuyBoxResponse>();
            var index   = 0;
            var step    = 20;
            var sleep   = new StepSleeper(TimeSpan.FromSeconds(1), 10);

            /*
             * Maximum request quota	Restore rate	Hourly request quota
             *  20 requests	10 items every second	36000 requests per hour
             */
            while (index < items.Count)
            {
                var checkedItems = items.Skip(index).Take(step).ToList();
                var resp         = RetryHelper.ActionWithRetriesWithCallResult(() =>
                                                                               api.GetCompetitivePricingForSKU(checkedItems.Select(i => i.SKU).ToList()),
                                                                               logger);

                if (resp.IsFail)
                {
                    var message = resp.Message ?? "";

                    //Request is throttled ---> Amazon.OrdersApi.Runtime.MwsException: Request is throttled
                    //...
                    if (message.IndexOf("ServiceUnavailable", StringComparison.InvariantCultureIgnoreCase) >= 0 ||
                        message.IndexOf("throttled", StringComparison.InvariantCultureIgnoreCase) >= 0)
                    {
                        //do sleep
                    }
                    //NOTE: temporary do always
                    _log.Info("Sleep 5 min");
                    Thread.Sleep(5 * 60 * 1000);
                }
                else
                {
                    foreach (var item in resp.Data)
                    {
                        results.Add(new ItemBuyBoxResponse()
                        {
                            ASIN        = item.ASIN,
                            CheckedDate = DateTime.UtcNow,
                            Status      = item.BuyBoxStatus ?? BuyBoxStatusCode.NoWinner,
                            WinnerPrice = item.LowestPrice,
                        });
                    }

                    index += step;
                    sleep.NextStep();
                }
            }
            return(results);
        }