Zig
Zig
Shopify Create a new unpublished product
See more Shopify Examples
Create a new unpublished productChilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
const rest = try chilkat.Rest.init();
defer rest.deinit();
rest.setAuthBasic("SHOPIFY_PRIVATE_API_KEY", "SHOPIFY_PRIVATE_API_KEY") catch {};
rest.connect("chilkat.myshopify.com", 443, true, true) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// The following code creates the JSON request body.
// The JSON created by this code is shown below.
const json_req = try chilkat.JsonObject.init();
defer json_req.deinit();
json_req.updateString("product.title", "Burton Custom Freestyle 151") catch {};
json_req.updateString("product.body_html", "<strong>Good snowboard!</strong>") catch {};
json_req.updateString("product.vendor", "Burton") catch {};
json_req.updateString("product.product_type", "Snowboard") catch {};
json_req.updateBool("product.published", false) catch {};
// The JSON request body created by the above code:
// {
// "product": {
// "title": "Burton Custom Freestyle 151",
// "body_html": "<strong>Good snowboard!<\/strong>",
// "vendor": "Burton",
// "product_type": "Snowboard",
// "published": false
// }
// }
const sb_req = try chilkat.StringBuilder.init();
defer sb_req.deinit();
json_req.emitSb(sb_req) catch {};
rest.addHeader("Content-Type", "application/json") catch {};
const sb_json = try chilkat.StringBuilder.init();
defer sb_json.deinit();
rest.fullRequestSb("POST", "/admin/products.json ", sb_req, sb_json) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
if (rest.getResponseStatusCode() != 201) {
std.debug.print("Received error response code: {d}\n", .{rest.getResponseStatusCode()});
std.debug.print("Response body:\n", .{});
std.debug.print("{s}\n", .{try sb_json.getAsString(alloc)});
return;
}
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.loadSb(sb_json) catch {};
// The following code parses the JSON response.
// A sample JSON response is shown below the sample code.
var product_id: i32 = 0;
var i: i32 = 0;
var count_i: i32 = 0;
var id: i32 = 0;
var title: [:0]const u8 = "";
var price: [:0]const u8 = "";
var sku: [:0]const u8 = "";
var position: i32 = 0;
var grams: i32 = 0;
var inventory_policy: [:0]const u8 = "";
var compare_at_price: bool = false;
var fulfillment_service: [:0]const u8 = "";
var inventory_management: bool = false;
var option1: [:0]const u8 = "";
var option2: bool = false;
var option3: bool = false;
var created_at: [:0]const u8 = "";
var updated_at: [:0]const u8 = "";
var taxable: bool = false;
var barcode: bool = false;
var image_id: bool = false;
var inventory_quantity: i32 = 0;
var weight: i32 = 0;
var weight_unit: [:0]const u8 = "";
var old_inventory_quantity: i32 = 0;
var requires_shipping: bool = false;
var name: [:0]const u8 = "";
var j: i32 = 0;
var count_j: i32 = 0;
var str_val: [:0]const u8 = "";
product_id = json.intOf("product.id");
_ = try json.stringOf(alloc, "product.title");
_ = try json.stringOf(alloc, "product.body_html");
_ = try json.stringOf(alloc, "product.vendor");
_ = try json.stringOf(alloc, "product.product_type");
_ = try json.stringOf(alloc, "product.created_at");
_ = try json.stringOf(alloc, "product.handle");
_ = try json.stringOf(alloc, "product.updated_at");
_ = json.isNullOf("product.published_at");
_ = json.isNullOf("product.template_suffix");
_ = try json.stringOf(alloc, "product.published_scope");
_ = try json.stringOf(alloc, "product.tags");
_ = json.isNullOf("product.image");
i = 0;
count_i = json.sizeOfArray("product.variants");
while (i < count_i) {
json.setI(i);
id = json.intOf("product.variants[i].id");
product_id = json.intOf("product.variants[i].product_id");
title = try json.stringOf(alloc, "product.variants[i].title");
price = try json.stringOf(alloc, "product.variants[i].price");
sku = try json.stringOf(alloc, "product.variants[i].sku");
position = json.intOf("product.variants[i].position");
grams = json.intOf("product.variants[i].grams");
inventory_policy = try json.stringOf(alloc, "product.variants[i].inventory_policy");
compare_at_price = json.isNullOf("product.variants[i].compare_at_price");
fulfillment_service = try json.stringOf(alloc, "product.variants[i].fulfillment_service");
inventory_management = json.isNullOf("product.variants[i].inventory_management");
option1 = try json.stringOf(alloc, "product.variants[i].option1");
option2 = json.isNullOf("product.variants[i].option2");
option3 = json.isNullOf("product.variants[i].option3");
created_at = try json.stringOf(alloc, "product.variants[i].created_at");
updated_at = try json.stringOf(alloc, "product.variants[i].updated_at");
taxable = json.boolOf("product.variants[i].taxable");
barcode = json.isNullOf("product.variants[i].barcode");
image_id = json.isNullOf("product.variants[i].image_id");
inventory_quantity = json.intOf("product.variants[i].inventory_quantity");
weight = json.intOf("product.variants[i].weight");
weight_unit = try json.stringOf(alloc, "product.variants[i].weight_unit");
old_inventory_quantity = json.intOf("product.variants[i].old_inventory_quantity");
requires_shipping = json.boolOf("product.variants[i].requires_shipping");
i = i + 1;
}
i = 0;
count_i = json.sizeOfArray("product.options");
while (i < count_i) {
json.setI(i);
id = json.intOf("product.options[i].id");
product_id = json.intOf("product.options[i].product_id");
name = try json.stringOf(alloc, "product.options[i].name");
position = json.intOf("product.options[i].position");
j = 0;
count_j = json.sizeOfArray("product.options[i].values");
while (j < count_j) {
json.setJ(j);
str_val = try json.stringOf(alloc, "product.options[i].values[j]");
j = j + 1;
}
i = i + 1;
}
i = 0;
count_i = json.sizeOfArray("product.images");
while (i < count_i) {
json.setI(i);
i = i + 1;
}
// A sample JSON response body that is parsed by the above code:
// {
// "product": {
// "id": 1071559753,
// "title": "Burton Custom Freestyle 151",
// "body_html": "<strong>Good snowboard!<\/strong>",
// "vendor": "Burton",
// "product_type": "Snowboard",
// "created_at": "2017-09-22T14:48:52-04:00",
// "handle": "burton-custom-freestyle-151",
// "updated_at": "2017-09-22T14:48:52-04:00",
// "published_at": null,
// "template_suffix": null,
// "published_scope": "global",
// "tags": "",
// "variants": [
// {
// "id": 1070325224,
// "product_id": 1071559753,
// "title": "Default Title",
// "price": "0.00",
// "sku": "",
// "position": 1,
// "grams": 0,
// "inventory_policy": "deny",
// "compare_at_price": null,
// "fulfillment_service": "manual",
// "inventory_management": null,
// "option1": "Default Title",
// "option2": null,
// "option3": null,
// "created_at": "2017-09-22T14:48:52-04:00",
// "updated_at": "2017-09-22T14:48:52-04:00",
// "taxable": true,
// "barcode": null,
// "image_id": null,
// "inventory_quantity": 1,
// "weight": 0.0,
// "weight_unit": "lb",
// "old_inventory_quantity": 1,
// "requires_shipping": true
// }
// ],
// "options": [
// {
// "id": 1022828912,
// "product_id": 1071559753,
// "name": "Title",
// "position": 1,
// "values": [
// "Default Title"
// ]
// }
// ],
// "images": [
// ],
// "image": null
// }
// }
std.debug.print("Example Completed.\n", .{});
}