Zig
Zig
Lightspeed - Create a Product
See more Lightspeed Examples
Create a new product based on the given parameters.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 -u API_KEY:API_SECRET \
// -H "Content-Type: application/json" \
// -X POST \
// -d '{
// "product": {
// "visibility": "visible",
// "data01": "",
// "data02": "",
// "data03": "",
// "title": "Lookin Sharp T-Shirt",
// "fulltitle": "Lookin Sharp T-Shirt",
// "description": "Description of the Lookin Sharp T-Shirt",
// "content": "Long Description of the Lookin Sharp T-Shirt",
// "deliverydate": 6488,
// "supplier": 78794,
// "brand": 1171202
// }
// }' \
// "https://api.webshopapp.com/en/products.json"
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
http.setLogin("API_KEY");
http.setPassword("API_SECRET");
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "product": {
// "visibility": "visible",
// "data01": "",
// "data02": "",
// "data03": "",
// "title": "Lookin Sharp T-Shirt",
// "fulltitle": "Lookin Sharp T-Shirt",
// "description": "Description of the Lookin Sharp T-Shirt",
// "content": "Long Description of the Lookin Sharp T-Shirt",
// "deliverydate": 6488,
// "supplier": 78794,
// "brand": 1171202
// }
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("product.visibility", "visible") catch {};
json.updateString("product.data01", "") catch {};
json.updateString("product.data02", "") catch {};
json.updateString("product.data03", "") catch {};
json.updateString("product.title", "Lookin Sharp T-Shirt") catch {};
json.updateString("product.fulltitle", "Lookin Sharp T-Shirt") catch {};
json.updateString("product.description", "Description of the Lookin Sharp T-Shirt") catch {};
json.updateString("product.content", "Long Description of the Lookin Sharp T-Shirt") catch {};
json.updateInt("product.deliverydate", 6488) catch {};
json.updateInt("product.supplier", 78794) catch {};
json.updateInt("product.brand", 1171202) catch {};
http.setRequestHeader("Content-Type", "application/json");
// Use the correct cluster for your shop. Here are the choices:
// eu1 https://api.webshopapp.com/en/
// us1 https://api.shoplightspeed.com/en/
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpJson("POST", "https://api.webshopapp.com/en/products.json", json, "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)
// {
// "product": {
// "id": 20974460,
// "createdAt": "2019-05-28T20:14:26+00:00",
// "updatedAt": "2019-05-28T20:14:26+00:00",
// "isVisible": true,
// "visibility": "visible",
// "hasMatrix": false,
// "data01": "",
// "data02": "",
// "data03": "",
// "url": "lookin-sharp-t-shirt",
// "title": "Lookin' Sharp T-Shirt",
// "fulltitle": "Lookin' Sharp T-Shirt",
// "description": "Description of the Lookin' Sharp T-Shirt",
// "content": "<p>Long Description of the Lookin' Sharp T-Shirt</p>",
// "set": false,
// "brand": {
// "resource": {
// "id": 1171202,
// "url": "brands/1171202",
// "link": "https://api.shoplightspeed.com/us/brands/1171202.json"
// }
// },
// "categories": {
// "resource": {
// "id": false,
// "url": "categories/products?product=20974460",
// "link": "https://api.shoplightspeed.com/us/categories/products.json?product=20974460"
// }
// },
// "deliverydate": {
// "resource": {
// "id": 6488,
// "url": "deliverydates/6488",
// "link": "https://api.shoplightspeed.com/us/deliverydates/6488.json"
// }
// },
// "image": false,
// "images": false,
// "relations": {
// "resource": {
// "id": false,
// "url": "products/20974460/relations",
// "link": "https://api.shoplightspeed.com/us/products/20974460/relations.json"
// }
// },
// "metafields": {
// "resource": {
// "id": false,
// "url": "products/20974460/metafields",
// "link": "https://api.shoplightspeed.com/us/products/20974460/metafields.json"
// }
// },
// "reviews": {
// "resource": {
// "id": false,
// "url": "reviews?product=20974460",
// "link": "https://api.shoplightspeed.com/us/reviews.json?product=20974460"
// }
// },
// "type": false,
// "attributes": {
// "resource": {
// "id": false,
// "url": "products/20974460/attributes",
// "link": "https://api.shoplightspeed.com/us/products/20974460/attributes.json"
// }
// },
// "supplier": {
// "resource": {
// "id": 78794,
// "url": "suppliers/78794",
// "link": "https://api.shoplightspeed.com/us/suppliers/78794.json"
// }
// },
// "tags": {
// "resource": {
// "id": false,
// "url": "tags/products?product=20974460",
// "link": "https://api.shoplightspeed.com/us/tags/products.json?product=20974460"
// }
// },
// "variants": {
// "resource": {
// "id": false,
// "url": "variants?product=20974460",
// "link": "https://api.shoplightspeed.com/us/variants.json?product=20974460"
// }
// },
// "movements": {
// "resource": {
// "id": false,
// "url": "variants/movements?product=20974460",
// "link": "https://api.shoplightspeed.com/us/variants/movements.json?product=20974460"
// }
// }
// }
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
_ = j_resp.intOf("product.id");
_ = try j_resp.stringOf(alloc, "product.createdAt");
_ = try j_resp.stringOf(alloc, "product.updatedAt");
_ = j_resp.boolOf("product.isVisible");
_ = try j_resp.stringOf(alloc, "product.visibility");
_ = j_resp.boolOf("product.hasMatrix");
_ = try j_resp.stringOf(alloc, "product.data01");
_ = try j_resp.stringOf(alloc, "product.data02");
_ = try j_resp.stringOf(alloc, "product.data03");
_ = try j_resp.stringOf(alloc, "product.url");
_ = try j_resp.stringOf(alloc, "product.title");
_ = try j_resp.stringOf(alloc, "product.fulltitle");
_ = try j_resp.stringOf(alloc, "product.description");
_ = try j_resp.stringOf(alloc, "product.content");
_ = j_resp.boolOf("product.set");
_ = j_resp.intOf("product.brand.resource.id");
_ = try j_resp.stringOf(alloc, "product.brand.resource.url");
_ = try j_resp.stringOf(alloc, "product.brand.resource.link");
_ = j_resp.boolOf("product.categories.resource.id");
_ = try j_resp.stringOf(alloc, "product.categories.resource.url");
_ = try j_resp.stringOf(alloc, "product.categories.resource.link");
_ = j_resp.intOf("product.deliverydate.resource.id");
_ = try j_resp.stringOf(alloc, "product.deliverydate.resource.url");
_ = try j_resp.stringOf(alloc, "product.deliverydate.resource.link");
_ = j_resp.boolOf("product.image");
_ = j_resp.boolOf("product.images");
_ = j_resp.boolOf("product.relations.resource.id");
_ = try j_resp.stringOf(alloc, "product.relations.resource.url");
_ = try j_resp.stringOf(alloc, "product.relations.resource.link");
_ = j_resp.boolOf("product.metafields.resource.id");
_ = try j_resp.stringOf(alloc, "product.metafields.resource.url");
_ = try j_resp.stringOf(alloc, "product.metafields.resource.link");
_ = j_resp.boolOf("product.reviews.resource.id");
_ = try j_resp.stringOf(alloc, "product.reviews.resource.url");
_ = try j_resp.stringOf(alloc, "product.reviews.resource.link");
_ = j_resp.boolOf("product.type");
_ = j_resp.boolOf("product.attributes.resource.id");
_ = try j_resp.stringOf(alloc, "product.attributes.resource.url");
_ = try j_resp.stringOf(alloc, "product.attributes.resource.link");
_ = j_resp.intOf("product.supplier.resource.id");
_ = try j_resp.stringOf(alloc, "product.supplier.resource.url");
_ = try j_resp.stringOf(alloc, "product.supplier.resource.link");
_ = j_resp.boolOf("product.tags.resource.id");
_ = try j_resp.stringOf(alloc, "product.tags.resource.url");
_ = try j_resp.stringOf(alloc, "product.tags.resource.link");
_ = j_resp.boolOf("product.variants.resource.id");
_ = try j_resp.stringOf(alloc, "product.variants.resource.url");
_ = try j_resp.stringOf(alloc, "product.variants.resource.link");
_ = j_resp.boolOf("product.movements.resource.id");
_ = try j_resp.stringOf(alloc, "product.movements.resource.url");
_ = try j_resp.stringOf(alloc, "product.movements.resource.link");
}