Zig
Zig
Faire - Update Inventory Levels
See more Faire Examples
Update the inventory levels for multiple product options in one request.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const http = try chilkat.Http.init();
defer http.deinit();
// 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
// }
// ]
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("inventories[0].sku", "vanilla-candle") catch {};
json.updateInt("inventories[0].current_quantity", 24) catch {};
json.updateBool("inventories[0].discontinued", false) catch {};
json.updateNull("inventories[0].backordered_until") catch {};
json.updateString("inventories[1].sku", "cinnamon-candle") catch {};
json.updateInt("inventories[1].current_quantity", 0) catch {};
json.updateBool("inventories[1].discontinued", false) catch {};
json.updateString("inventories[1].backordered_until", "20190314T000915.000Z") catch {};
json.updateString("inventories[2].sku", "fall-candle") catch {};
json.updateInt("inventories[2].current_quantity", 0) catch {};
json.updateBool("inventories[2].discontinued", true) catch {};
json.updateNull("inventories[2].backordered_until") catch {};
json.updateString("inventories[3].sku", "fall-candle") catch {};
json.updateNull("inventories[3].current_quantity") catch {};
json.updateBool("inventories[3].discontinued", false) catch {};
json.updateNull("inventories[3].backordered_until") catch {};
http.setRequestHeader("Content-Type", "application/json");
http.setRequestHeader("X-FAIRE-ACCESS-TOKEN", "<access_token>");
const sb_request_body = try chilkat.StringBuilder.init();
defer sb_request_body.deinit();
json.emitSb(sb_request_body) catch {};
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpSb("PATCH", "https://www.faire.com/api/v1/products/options/inventory-levels", sb_request_body, "utf-8", "application/json", resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
const sb_response_body = try chilkat.StringBuilder.init();
defer sb_response_body.deinit();
resp.getBodySb(sb_response_body) catch {};
const j_resp = try chilkat.JsonObject.init();
defer j_resp.deinit();
j_resp.loadSb(sb_response_body) catch {};
j_resp.setEmitCompact(false);
std.debug.print("Response Body:\n", .{});
std.debug.print("{s}\n", .{try j_resp.emit(alloc)});
const resp_status_code = resp.getStatusCode();
std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
if (resp_status_code >= 400) {
std.debug.print("Response Header:\n", .{});
std.debug.print("{s}\n", .{try resp.getHeader(alloc)});
std.debug.print("Failed.\n", .{});
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
var id: [:0]const u8 = "";
var product_id: [:0]const u8 = "";
var active: bool = false;
var deleted: bool = false;
var name: [:0]const u8 = "";
var sku: [:0]const u8 = "";
var available_quantity: i32 = 0;
var created_at: [:0]const u8 = "";
var updated_at: [:0]const u8 = "";
var retail_price_cents: i32 = 0;
var wholesale_price_cents: i32 = 0;
var backordered_until: [:0]const u8 = "";
var j: i32 = 0;
var count_j: i32 = 0;
var value: [:0]const u8 = "";
var i: i32 = 0;
const count_i = j_resp.sizeOfArray("options");
while (i < count_i) {
j_resp.setI(i);
id = try j_resp.stringOf(alloc, "options[i].id");
product_id = try j_resp.stringOf(alloc, "options[i].product_id");
active = j_resp.boolOf("options[i].active");
deleted = j_resp.boolOf("options[i].deleted");
name = try j_resp.stringOf(alloc, "options[i].name");
sku = try j_resp.stringOf(alloc, "options[i].sku");
available_quantity = j_resp.intOf("options[i].available_quantity");
created_at = try j_resp.stringOf(alloc, "options[i].created_at");
updated_at = try j_resp.stringOf(alloc, "options[i].updated_at");
retail_price_cents = j_resp.intOf("options[i].retail_price_cents");
wholesale_price_cents = j_resp.intOf("options[i].wholesale_price_cents");
backordered_until = try j_resp.stringOf(alloc, "options[i].backordered_until");
j = 0;
count_j = j_resp.sizeOfArray("options[i].variations");
while (j < count_j) {
j_resp.setJ(j);
name = try j_resp.stringOf(alloc, "options[i].variations[j].name");
value = try j_resp.stringOf(alloc, "options[i].variations[j].value");
j = j + 1;
}
i = i + 1;
}
}