Ejemplo n.º 1
0
        /// <summary>
        ///	Update products quantities via batch request
        /// </summary>
        /// <param name="skusQuantities"></param>
        /// <param name="token"></param>
        /// <param name="mark"></param>
        /// <returns></returns>
        public async Task UpdateProductsQuantitiesBySkuAsync(Dictionary <string, int> skusQuantities, CancellationToken token, Mark mark = null)
        {
            if (skusQuantities == null || !skusQuantities.Any())
            {
                return;
            }

            if (mark == null)
            {
                mark = Mark.CreateNew();
            }

            if (token.IsCancellationRequested)
            {
                var exceptionDetails = CreateMethodCallInfo(base.Config.ApiBaseUrl, mark, additionalInfo: this.AdditionalLogInfo());
                var mivaException    = new MivaException(string.Format("{0}. Update products quantities request was cancelled", exceptionDetails));
                MivaLogger.LogTraceException(mivaException);
            }

            var chunks = skusQuantities.SplitToChunks(base.Config.InventoryUpdateBatchSize);

            foreach (var chunk in chunks)
            {
                var request   = new UpdateProductsInventoryBatchRequest(base.Config.Credentials, chunk);
                var responses = await base.PostAsync <IEnumerable <MivaResponse> >(request, token, mark).ConfigureAwait(false);

                if (responses != null && responses.Any())
                {
                    for (int i = 0; i < responses.Count(); i++)
                    {
                        var response = responses.ElementAt(i);

                        if (response.Success == 0)
                        {
                            MivaLogger.LogTrace(new MivaException(response.ErrorMessage, response.ErrorCode),
                                                string.Format("Failed to update product {0} quantity to {1}", chunk.ElementAt(i).Key, chunk.ElementAt(i).Value));
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///	Update product's quantity using sku
        /// </summary>
        /// <param name="sku"></param>
        /// <param name="quantity"></param>
        /// <param name="token"></param>
        /// <param name="mark"></param>
        /// <returns></returns>
        public async Task UpdateProductQuantityBySkuAsync(string sku, int quantity, CancellationToken token, Mark mark = null)
        {
            if (mark == null)
            {
                mark = Mark.CreateNew();
            }

            if (token.IsCancellationRequested)
            {
                var exceptionDetails = CreateMethodCallInfo(base.Config.ApiBaseUrl, mark, additionalInfo: this.AdditionalLogInfo());
                var mivaException    = new MivaException(string.Format("{0}. Update product's sku quantity request was cancelled", exceptionDetails));
                MivaLogger.LogTraceException(mivaException);
            }

            var request  = new UpdateProductInventoryRequest(base.Config.Credentials, sku, quantity);
            var response = await base.PostAsync <MivaResponse>(request, token, mark).ConfigureAwait(false);

            if (response.Success == 0)
            {
                MivaLogger.LogTrace(new MivaException(response.ErrorMessage, response.ErrorCode), string.Format("Failed to update product's quantity! Sku: {0}, New quantity: {1}", sku, quantity));
                return;
            }
        }