BigCommerce" Put"请求中多个项目的属性

时间:2015-01-07 19:54:35

标签: c# json rest dotnet-httpclient bigcommerce

我正在使用我创建的REST服务使用BigCommerce API,并且我已经能够使用每个产品的产品ID成功更新产品的数量。这是我如何做的一个例子:

HttpClientHandler handler = new HttpClientHandler();
handler.Credentials = new NetworkCredential(username, api_key);
handler.Proxy = null;
handler.UseProxy = false;
HttpClient client = new HttpClient(handler);
client.BaseAddress = new Uri("MyStoresURL");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

string productInventory = "My Product Inventory";
var setQty = new { inventory_level = productInventory };
string productID = "Product ID to be updated";
string url = string.Format("products/{0}", productID);
var response = client.PutAsJsonAsync(url, setQty).Result;
//StatusCode: 200 "OK"

问题是,对于20,000个产品,这个过程可能需要数小时。我正在寻找一种方法来一次或至少批量更新所有产品库存。所有BigCommerce文档都解释了如何更新单个产品或订单,客户等,但不是一次性更新。

我发现这很奇怪,因为您可以通过商店的门户网站手动上传包含所有产品的CSV文件,BigCommerce将使用该门户网站更新您的广告资源。我正在寻找一种自动执行此操作的方法。这是我尝试过的一个例子,它导致状态400"错误请求"。

HttpClientHandler handler = new HttpClientHandler();
handler.Credentials = new NetworkCredential(username, 
HttpClient client = new HttpClient(handler);
client.BaseAddress = new Uri("My Store");
handler.Proxy = null;
string url = string.Format("products.json?page={0}", counter);

HttpResponseMessage response = new HttpResponseMessage();

List<ProductQTY> productQty = new List<ProductQTY>();

response = client.GetAsync(url).Result;
//This returns a json string with the first 50 products 
//and all their attributes including Quantity.

productQty = response.Content.ReadAsAsync<List<ProductQTY>>().Result;
//This converts the response into a list of 50 Products.
//Here I update the quantity of each product.

string json = Newtonsoft.Json.JsonConvert.SerializeObject(productQty);
//convert my product list back into json.

response = client.PutAsJsonAsync(url, json).Result;
//StatusCode: 400 "Bad Request"

上面的代码将进入一个循环,从每个页面下载50个产品。

我也尝试将我下载的50个计数json字符串上传回同一个网址,但它永远无法正常工作。

简而言之。这会使用HTTPClient

成功更新产品的广告资源
string inventory = "10";
string bcID = "10001";
var setInv = new { inventory_level = inventory };
string url = string.Format("products/{0}", bcID);
var response = client.PutAsJsonAsync(url, setInv).Result;

那么有没有办法一次更新多个产品?

2 个答案:

答案 0 :(得分:1)

分享这个,因为有人可能有同样的问题。我知道我已经搜索了这些信息的高低,但找不到它。

我直接从BigCommerce支持得到了答案。之前我曾经问过但没有给出答案,因为我们没有使用他们的API支持包,但BC的一位乐于助人的人终于给了我答案。引用:

&#34;目前无法通过API一次更新多个产品库存。它只能按产品完成。&#34;

他们确实提到他们正在努力在将来添加此功能。

这就是那个。我会手动更新库存,或者我会查看等待时间是否可以接受。

答案 1 :(得分:0)

它不是批处理,但你可以通过一次更新多个(4,8等)项目来大大降低你的时间。 我正在研究自己使用API​​,并发现了这篇文章。我猜我会计划使用ASYNC / AWAIT