예제 #1
0
        /// <inheritdoc/>
        public ChannelReader <(Product product, Errors errors)> UpInsertBatch(ChannelReader <Product> channel, CancellationToken cancellationToken)
        {
            var output = Channel.CreateUnbounded <(Product product, Errors errors)>();

            Task.Run(async() =>
            {
                try
                {
                    async Task <ProductsCustomBatchRequest> RequestAsync(GoogleShoppingOptions options)
                    {
                        var batchRequest = new ProductsCustomBatchRequest
                        {
                            Entries = new List <ProductsCustomBatchRequestEntry>()
                        };

                        var batchId = 0;
                        while (await channel.WaitToReadAsync(cancellationToken))
                        {
                            var item = await channel.ReadAsync(cancellationToken);

                            var entry = new ProductsCustomBatchRequestEntry
                            {
                                BatchId    = batchId,
                                MerchantId = options.MerchantId,
                                Method     = "insert",
                                Product    = item
                            };
                            batchRequest.Entries.Add(entry);
                            ++batchId;
                        }

                        return(batchRequest);
                    }

                    var response = await ExecuteBatchAsync(RequestAsync, cancellationToken);

                    if (response.Kind == "content#productsCustomBatchResponse")
                    {
                        for (var i = 0; i < response.Entries.Count; i++)
                        {
                            var insertedProduct = response.Entries[i].Product;
                            await output.Writer.WriteAsync((insertedProduct, response.Entries[i].Errors), cancellationToken);
                        }
                    }
                    else
                    {
                        _logger.LogInformation("There was an error. Response: {responseCode}", response.ToString());
                    }
                }
                finally
                {
                    output.Writer.Complete();
                }
            });

            return(output);
        }
예제 #2
0
        /// <inheritdoc/>
        public async Task <IDictionary <long, string> > DeleteBatchAsync(IDictionary <long, string> productIdList, CancellationToken cancellationToken)
        {
            var result = new Dictionary <long, string>();

            ProductsCustomBatchRequest Request(GoogleShoppingOptions options)
            {
                var batchRequest = new ProductsCustomBatchRequest
                {
                    Entries = new List <ProductsCustomBatchRequestEntry>()
                };

                foreach (var item in productIdList)
                {
                    var entry = new ProductsCustomBatchRequestEntry
                    {
                        BatchId    = item.Key,
                        MerchantId = options.MerchantId,
                        Method     = "delete",
                        ProductId  = item.Value
                    };
                    batchRequest.Entries.Add(entry);
                }

                return(batchRequest);
            }

            var response = await ExecuteBatchAsync(Request, cancellationToken);

            if (response.Kind == "content#productsCustomBatchResponse")
            {
                for (var i = 0; i < response.Entries.Count; i++)
                {
                    var errors    = response.Entries[i].Errors;
                    var flatError = string.Empty;
                    if (errors != null)
                    {
                        for (var j = 0; j < errors.ErrorsValue.Count; j++)
                        {
                            flatError += errors.ErrorsValue[j].ToString();
                        }
                    }
                    else
                    {
                        _logger.LogDebug("Product deleted, batchId {0}", response.Entries[i].BatchId);
                    }

                    result.Add(response.Entries[i].BatchId ?? 0, flatError);
                }
            }
            else
            {
                _logger.LogError("There was an error. Response: {response}", response);
            }

            return(result);
        }
        /// <summary>
        /// Deletes several products from the specified account, using custombatch.
        /// </summary>
        private void DeleteProductCustombatch(ulong merchantId, List <String> productList)
        {
            Console.WriteLine("=================================================================");
            Console.WriteLine("Deleting products using custombatch");
            Console.WriteLine("=================================================================");

            ProductsCustomBatchRequest batchRequest = new ProductsCustomBatchRequest();

            batchRequest.Entries = new List <ProductsCustomBatchRequestEntry>();
            for (int i = 0; i < productList.Count; i++)
            {
                ProductsCustomBatchRequestEntry entry = new ProductsCustomBatchRequestEntry();
                entry.BatchId    = i;
                entry.MerchantId = merchantId;
                entry.Method     = "delete";
                entry.ProductId  = productList[i]; // Use the full product ID here, not the OfferId
                batchRequest.Entries.Add(entry);
            }

            ProductsCustomBatchResponse response = service.Products.Custombatch(batchRequest).Execute();

            if (response.Kind == "content#productsCustomBatchResponse")
            {
                for (int i = 0; i < response.Entries.Count; i++)
                {
                    Errors errors = response.Entries[i].Errors;
                    if (errors != null)
                    {
                        for (int j = 0; j < errors.ErrorsValue.Count; j++)
                        {
                            Console.WriteLine(errors.ErrorsValue[j].ToString());
                        }
                    }
                    else
                    {
                        Console.WriteLine("Product deleted, batchId {0}", response.Entries[i].BatchId);
                    }
                }
            }
            else
            {
                Console.WriteLine(
                    "There was an error. Response: {0}",
                    response);
            }
        }
예제 #4
0
        /// <inheritdoc/>
        public async Task <IDictionary <long, (Product product, Errors errors)> > UpInsertBatchAsync(
            IDictionary <long, Product> productList,
            CancellationToken cancellationToken)
        {
            var result = new Dictionary <long, (Product product, Errors errors)>();

            ProductsCustomBatchRequest Request(GoogleShoppingOptions options)
            {
                var batchRequest = new ProductsCustomBatchRequest
                {
                    Entries = new List <ProductsCustomBatchRequestEntry>()
                };

                foreach (var item in productList)
                {
                    var entry = new ProductsCustomBatchRequestEntry
                    {
                        BatchId    = item.Key,
                        MerchantId = options.MerchantId,
                        Method     = "insert",
                        Product    = item.Value
                    };
                    batchRequest.Entries.Add(entry);
                }

                return(batchRequest);
            }

            var response = await ExecuteBatchAsync(Request, cancellationToken);

            if (response.Kind == "content#productsCustomBatchResponse")
            {
                for (var i = 0; i < response.Entries.Count; i++)
                {
                    var insertedProduct = response.Entries[i].Product;
                    result.Add(response.Entries[i].BatchId ?? 0, (insertedProduct, response.Entries[i].Errors));
                }
            }
            else
            {
                _logger.LogInformation("There was an error. Response: {responseCode}", response.ToString());
            }

            return(result);
        }
        /// <summary>
        /// Inserts several products to the specified account, using custombatch.
        /// </summary>
        /// <returns>The list of product IDs, which can be used to get or delete them.</returns>
        private List <String> InsertProductCustombatch(ulong merchantId, string websiteUrl)
        {
            Console.WriteLine("=================================================================");
            Console.WriteLine("Inserting products using custombatch");
            Console.WriteLine("=================================================================");

            ProductsCustomBatchRequest batchRequest = new ProductsCustomBatchRequest();

            batchRequest.Entries = new List <ProductsCustomBatchRequestEntry>();
            for (int i = 0; i < 3; i++)
            {
                ProductsCustomBatchRequestEntry entry = new ProductsCustomBatchRequestEntry();
                entry.BatchId    = i;
                entry.MerchantId = merchantId;
                entry.Method     = "insert";
                entry.Product    = GenerateProduct(websiteUrl);
                batchRequest.Entries.Add(entry);
            }

            ProductsCustomBatchResponse response = service.Products.Custombatch(batchRequest).Execute();
            List <String> productsInserted       = new List <String>();

            if (response.Kind == "content#productsCustomBatchResponse")
            {
                for (int i = 0; i < response.Entries.Count; i++)
                {
                    Product product = response.Entries[i].Product;
                    productsInserted.Add(product.Id);
                    Console.WriteLine(
                        "Product inserted with ID \"{0}\" and title \"{1}\".",
                        product.OfferId,
                        product.Title);
                    shoppingUtil.PrintWarnings(product.Warnings);
                }
            }
            else
            {
                Console.WriteLine(
                    "There was an error. Response: {0}",
                    response.ToString());
            }
            return(productsInserted);
        }
예제 #6
0
        /// <inheritdoc/>
        public ChannelReader <(long batchId, string productId)> Delete(ChannelReader <string> channel, CancellationToken cancellationToken)
        {
            var output = Channel.CreateUnbounded <(long batchId, string productId)>();

            Task.Run(async() =>
            {
                try
                {
                    async Task <ProductsCustomBatchRequest> RequestAsync(GoogleShoppingOptions options)
                    {
                        var batchRequest = new ProductsCustomBatchRequest
                        {
                            Entries = new List <ProductsCustomBatchRequestEntry>()
                        };

                        var batchId = 0;
                        while (await channel.WaitToReadAsync(cancellationToken))
                        {
                            var item = await channel.ReadAsync(cancellationToken);

                            var entry = new ProductsCustomBatchRequestEntry
                            {
                                BatchId    = batchId,
                                MerchantId = options.MerchantId,
                                Method     = "delete",
                                ProductId  = item
                            };
                            batchRequest.Entries.Add(entry);
                            ++batchId;
                        }

                        return(batchRequest);
                    }

                    var response = await ExecuteBatchAsync(RequestAsync, cancellationToken);

                    if (response.Kind == "content#productsCustomBatchResponse")
                    {
                        for (var i = 0; i < response.Entries.Count; i++)
                        {
                            var errors    = response.Entries[i].Errors;
                            var flatError = string.Empty;
                            if (errors != null)
                            {
                                for (var j = 0; j < errors.ErrorsValue.Count; j++)
                                {
                                    flatError += errors.ErrorsValue[j].ToString();
                                }
                            }
                            else
                            {
                                _logger.LogDebug("Product deleted, batchId {0}", response.Entries[i].BatchId);
                            }

                            await output.Writer.WriteAsync((response.Entries[i].BatchId ?? 0, flatError), cancellationToken);
                        }
                    }
                    else
                    {
                        _logger.LogError("There was an error. Response: {response}", response);
                    }
                }
                finally
                {
                    output.Writer.Complete();
                }
            });

            return(output);
        }