public void AddItems()
        {
            if (TestData.itemIds != null)
            {
                //call endItems call
                new T_125_EndItemsLibrary().EndItems();
            }
            TestData.itemIds = new StringCollection();

            AddItemsCall api = new AddItemsCall(this.apiContext);

            AddItemRequestContainerTypeCollection itemsContainers = new AddItemRequestContainerTypeCollection();

            //add five item one time,this should be successed
            foreach (string category in CATEGORY_ID)
            {
                addItemToContainer(itemsContainers, category);
            }
            api.AddItemRequestContainerList = itemsContainers;

            api.Execute();

            //check whether the call is success.
            Assert.IsTrue(api.ApiResponse.Ack == AckCodeType.Success || api.ApiResponse.Ack == AckCodeType.Warning, "do not success!");
            Assert.IsNotNull(api.AddItemResponseContainerList);
            Assert.AreEqual(api.AddItemResponseContainerList.Count, 5);
            foreach (AddItemResponseContainerType containerType in api.AddItemResponseContainerList)
            {
                Assert.IsNotNull(containerType.Fees);
                Assert.IsTrue(containerType.Fees.Count > 0);
                Assert.IsTrue(containerType.ItemID != string.Empty);
                //cache item id
                TestData.itemIds.Add(containerType.ItemID);
            }
        }
Exemple #2
0
        public void SubmitProductsListingFeed(List <MarketplaceProductFeedDto> productFeeds, string submittedBy)
        {
            // take out the products which has no information for eBay
            var invalidProducts = productFeeds
                                  .Where(x => x.IsBlacklisted || x.eBayProductFeed == null || x.eBayProductFeed.CategoryId == null)
                                  .ToList();

            if (invalidProducts.Any())
            {
                _logger.LogWarning(LogEntryType.eBayProductListing, string.Format("{0}/{1} EIS products which will not be included to eBay product listing feed due to no eBay category id or no eBay information or blacklisted.",
                                                                                  invalidProducts.Count, productFeeds.Count));
                productFeeds.RemoveAll(x => x.IsBlacklisted || x.eBayProductFeed == null || x.eBayProductFeed.CategoryId == null);
            }

            // let's do not include the eBay products which have already item id
            var alreadyAddedProducts = productFeeds
                                       .Where(x => !string.IsNullOrEmpty(x.eBayProductFeed.ItemId))
                                       .ToList();

            if (alreadyAddedProducts.Any())
            {
                _logger.LogWarning(LogEntryType.eBayProductListing, string.Format("{0}/{1} EIS products which will not be included to eBay product listing feed since they are already added. Please do the product revise feed instead!",
                                                                                  alreadyAddedProducts.Count, productFeeds.Count));
                productFeeds.RemoveAll(x => !string.IsNullOrEmpty(x.eBayProductFeed.ItemId));
            }

            // determine if there's product feed to post
            if (!productFeeds.Any())
            {
                _logger.LogWarning(LogEntryType.eBayProductListing, "No eBay products for product listing.");
                return;
            }

            // set the log file name
            _context.ApiLogManager.ApiLoggerList.Add(new FileLogger(string.Format(_logDirectory, MethodBase.GetCurrentMethod().Name), false, true, true));
            var totalBatches  = Math.Ceiling(productFeeds.Count / 5.0);
            var failedBatches = new List <MarketplaceProductFeedDto>();

            for (var i = 0; i < totalBatches; i++)
            {
                // send the product listing feed by 5 items as required for eBay Trading API
                var batchedProducts = productFeeds.Skip(i * 5).Take(5).ToList();
                var itemRequestList = new List <AddItemRequestContainerType>();

                try
                {
                    foreach (var product in batchedProducts)
                    {
                        var itemRequest = new AddItemRequestContainerType();

                        // create the item request object for product
                        itemRequest.Item      = RequestHelper.CreateItemType(product, _credential.eBayDescriptionTemplate);
                        itemRequest.MessageID = product.EisSKU;

                        itemRequestList.Add(itemRequest);
                    }

                    // add it to the item collection object
                    var apiCall  = new AddItemsCall(_context);
                    var response = apiCall.AddItems(new AddItemRequestContainerTypeCollection(itemRequestList.ToArray()));

                    // parse the product's item id and fees from response object
                    var itemContainers = getParsedResponseItemContainers(response.ToArray());

                    // update the product eBay ItemId and log the posting fees
                    logAndUpdateProductItemId(itemContainers, LogEntryType.eBayProductListing);
                }
                catch (Exception ex)
                {
                    // add it to the faulty list
                    failedBatches.AddRange(batchedProducts);

                    var description = string.Format("Batch: {0}/{1} - Error in submitting PRODUCT_LISTING feed. \nError Message: {2} \nRequested by: {3}",
                                                    i + 1,
                                                    totalBatches,
                                                    EisHelper.GetExceptionMessage(ex),
                                                    submittedBy);
                    _logger.LogError(LogEntryType.eBayProductListing, description, ex.StackTrace);
                }
            }

            // resend the faulty product listing feed if there's any
            if (failedBatches.Any())
            {
                _logger.LogInfo(LogEntryType.eBayProductListing, string.Format("Starting sending PRODUCT_LISTING feed for failed batches. Count: {0}", failedBatches.Count));

                // iterate and send each item
                foreach (var item in failedBatches)
                {
                    SubmitSingleProductListingFeed(item, submittedBy);
                }

                _logger.LogInfo(LogEntryType.eBayProductListing, string.Format("Resubmission for {0} failed items for PRODUCT_LISTING feed has been completed.", failedBatches.Count()));
            }
            else
            {
                _logger.LogInfo(LogEntryType.eBayProductListing, string.Format("Successfully posted product listing feed for {0} - {1} product items. \nRequested by: {2}",
                                                                               ChannelName, productFeeds.Count, submittedBy));
            }
        }