Rust
Rust
Faire - Update Inventory Levels
See more Faire Examples
Update the inventory levels for multiple product options in one request.Chilkat Rust Downloads
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = chilkat::Http::new();
// Implements the following CURL command:
// curl -X PATCH
// -H "Content-Type: application/json"
// -H "X-FAIRE-ACCESS-TOKEN: <access_token>"
// -d '{
// "inventories": [
// {
// "sku": "vanilla-candle",
// "current_quantity": 24,
// "discontinued": false,
// "backordered_until": null
// },
// {
// "sku": "cinnamon-candle",
// "current_quantity": 0,
// "discontinued": false,
// "backordered_until": "20190314T000915.000Z"
// },
// {
// "sku": "fall-candle",
// "current_quantity": 0,
// "discontinued": true,
// "backordered_until": null
// },
// {
// "sku": "fall-candle",
// "current_quantity": null,
// "discontinued": false,
// "backordered_until": null
// }
// ]
// }' https://www.faire.com/api/v1/products/options/inventory-levels
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "inventories": [
// {
// "sku": "vanilla-candle",
// "current_quantity": 24,
// "discontinued": false,
// "backordered_until": null
// },
// {
// "sku": "cinnamon-candle",
// "current_quantity": 0,
// "discontinued": false,
// "backordered_until": "20190314T000915.000Z"
// },
// {
// "sku": "fall-candle",
// "current_quantity": 0,
// "discontinued": true,
// "backordered_until": null
// },
// {
// "sku": "fall-candle",
// "current_quantity": null,
// "discontinued": false,
// "backordered_until": null
// }
// ]
// }
let json = chilkat::JsonObject::new();
let _ = json.update_string("inventories[0].sku", "vanilla-candle");
let _ = json.update_int("inventories[0].current_quantity", 24);
let _ = json.update_bool("inventories[0].discontinued", false);
let _ = json.update_null("inventories[0].backordered_until");
let _ = json.update_string("inventories[1].sku", "cinnamon-candle");
let _ = json.update_int("inventories[1].current_quantity", 0);
let _ = json.update_bool("inventories[1].discontinued", false);
let _ = json.update_string("inventories[1].backordered_until", "20190314T000915.000Z");
let _ = json.update_string("inventories[2].sku", "fall-candle");
let _ = json.update_int("inventories[2].current_quantity", 0);
let _ = json.update_bool("inventories[2].discontinued", true);
let _ = json.update_null("inventories[2].backordered_until");
let _ = json.update_string("inventories[3].sku", "fall-candle");
let _ = json.update_null("inventories[3].current_quantity");
let _ = json.update_bool("inventories[3].discontinued", false);
let _ = json.update_null("inventories[3].backordered_until");
http.set_request_header("Content-Type", "application/json");
http.set_request_header("X-FAIRE-ACCESS-TOKEN", "<access_token>");
let sb_request_body = chilkat::StringBuilder::new();
let _ = json.emit_sb(&sb_request_body);
let resp = chilkat::HttpResponse::new();
if http.http_sb("PATCH", "https://www.faire.com/api/v1/products/options/inventory-levels", &sb_request_body, "utf-8", "application/json", &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
let sb_response_body = chilkat::StringBuilder::new();
let _ = resp.get_body_sb(&sb_response_body);
let j_resp = chilkat::JsonObject::new();
let _ = j_resp.load_sb(&sb_response_body);
j_resp.set_emit_compact(false);
println!("Response Body:");
println!("{}", j_resp.emit().unwrap_or_default());
let resp_status_code = resp.status_code();
println!("Response Status Code = {}", resp_status_code);
if resp_status_code >= 400 {
println!("Response Header:");
println!("{}", resp.header());
println!("Failed.");
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "options": [
// {
// "id": "po_012",
// "product_id": "p_ghi",
// "active": false,
// "deleted": false,
// "name": "Fall Scent",
// "sku": "fall-candle",
// "available_quantity": 0,
// "created_at": "20190313T000915.000Z",
// "updated_at": "20190315T000915.000Z",
// "variations": [
// {
// "name": "Scent",
// "value": "Fall"
// }
// ],
// "retail_price_cents": 599,
// "wholesale_price_cents": 300
// },
// {
// "id": "po_789",
// "product_id": "p_def",
// "active": false,
// "deleted": false,
// "name": "Cinnamon Scent",
// "sku": "cinnamon-candle",
// "available_quantity": 0,
// "created_at": "20190312T000915.000Z",
// "updated_at": "20190315T000915.000Z",
// "backordered_until": "20190314T000915.000Z",
// "variations": [
// {
// "name": "Scent",
// "value": "Cinnamon"
// }
// ],
// "retail_price_cents": 599,
// "wholesale_price_cents": 300
// },
// {
// "id": "po_456",
// "product_id": "p_abc",
// "active": true,
// "deleted": false,
// "name": "Vanilla Scent",
// "sku": "vanilla-candle",
// "available_quantity": 24,
// "created_at": "20190314T000915.000Z",
// "updated_at": "20190315T000915.000Z",
// "variations": [
// {
// "name": "Scent",
// "value": "Vanilla"
// }
// ],
// "retail_price_cents": 599,
// "wholesale_price_cents": 300
// }
// ]
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
let mut j: i32 = 0;
let mut count_j: i32 = 0;
let mut i = 0;
let count_i = j_resp.size_of_array("options");
while i < count_i {
j_resp.set_i(i);
let _ = j_resp.string_of("options[i].id").unwrap_or_default();
let _ = j_resp.string_of("options[i].product_id").unwrap_or_default();
let _ = j_resp.bool_of("options[i].active");
let _ = j_resp.bool_of("options[i].deleted");
let _ = j_resp.string_of("options[i].name").unwrap_or_default();
let _ = j_resp.string_of("options[i].sku").unwrap_or_default();
let _ = j_resp.int_of("options[i].available_quantity");
let _ = j_resp.string_of("options[i].created_at").unwrap_or_default();
let _ = j_resp.string_of("options[i].updated_at").unwrap_or_default();
let _ = j_resp.int_of("options[i].retail_price_cents");
let _ = j_resp.int_of("options[i].wholesale_price_cents");
let _ = j_resp.string_of("options[i].backordered_until").unwrap_or_default();
j = 0;
count_j = j_resp.size_of_array("options[i].variations");
while j < count_j {
j_resp.set_j(j);
let _ = j_resp.string_of("options[i].variations[j].name").unwrap_or_default();
let _ = j_resp.string_of("options[i].variations[j].value").unwrap_or_default();
j = j + 1;
}
i = i + 1;
}