/// <summary>
        ///	List all products that were modified since the specified date
        /// </summary>
        /// <param name="lastModifiedDateUtc"></param>
        /// <param name="token"></param>
        /// <param name="mark"></param>
        /// <returns></returns>
        public async Task <IEnumerable <MivaProduct> > GetProductsUpdatedAfterAsync(DateTime lastModifiedDateUtc, 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}. Get modified products request was cancelled", exceptionDetails));
                MivaLogger.LogTraceException(mivaException);
            }

            var products  = new List <MivaProduct>();
            var pageIndex = 0;

            while (true)
            {
                var productsFromPage = await CollectProductsFromPage(lastModifiedDateUtc, pageIndex, base.Config.ProductsPageSize, token, mark).ConfigureAwait(false);

                if (!productsFromPage.Any())
                {
                    break;
                }

                products.AddRange(productsFromPage);
                ++pageIndex;
            }

            return(products);
        }
Example #2
0
        public async Task <IEnumerable <MivaOrder> > GetModifiedOrdersFromQueueAsync(string queueName, 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}. Get modified orders request was cancelled", exceptionDetails));
                MivaLogger.LogTraceException(mivaException);
            }

            var orders    = new List <MivaOrder>();
            var pageIndex = 0;

            while (true)
            {
                var ordersFromPage = await CollectOrdersFromPage(queueName, pageIndex, base.Config.OrdersPageSize, token, mark).ConfigureAwait(false);

                if (!ordersFromPage.Any())
                {
                    break;
                }

                orders.AddRange(ordersFromPage);
                ++pageIndex;
            }

            return(orders);
        }
        /// <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));
                        }
                    }
                }
            }
        }
        /// <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;
            }
        }
Example #5
0
        public async Task AcknowledgeOrders(IEnumerable <long> ordersIds, 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}. Acknowledge orders request was cancelled", exceptionDetails));
                MivaLogger.LogTraceException(mivaException);
            }

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

            if (response.Success == 0 ||
                (response.Processed != null && response.Processed == 0))
            {
                throw new MivaException(response.ErrorMessage, response.ErrorCode);
            }
        }
        /// <summary>
        ///	Find product by sku
        /// </summary>
        /// <param name="sku"></param>
        /// <param name="token"></param>
        /// <param name="mark"></param>
        /// <returns></returns>
        public async Task <IEnumerable <MivaProduct> > FindProductBySku(string sku, 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}. Find product by sku request was cancelled", exceptionDetails));
                MivaLogger.LogTraceException(mivaException);
            }

            var request  = new FindProductBySkuRequest(base.Config.Credentials, sku);
            var response = await base.PostAsync <MivaDataResponse <IEnumerable <Product> > >(request, token, mark).ConfigureAwait(false);

            if (response.Data?.Data != null)
            {
                return(response.Data.Data.Select(r => r.ToSVProduct(base.Config.Credentials)));
            }

            return(Array.Empty <MivaProduct>());
        }