Zig
Zig
Lightspeed - Get All Products
See more Lightspeed Examples
Retrieve a list of all product objects from this shop.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 https://api.shoplightspeed.com/en/products.json \
// -u {key}:{secret}
// 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 the correct cluster for your shop. Here are the choices:
// eu1 https://api.webshopapp.com/en/
// us1 https://api.shoplightspeed.com/en/
const sb_response_body = try chilkat.StringBuilder.init();
defer sb_response_body.deinit();
http.quickGetSb("https://api.webshopapp.com/en/products.json", sb_response_body) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
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 = http.getLastStatus();
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 http.getLastHeader(alloc)});
std.debug.print("Failed.\n", .{});
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "product": {
// "id": 20967267,
// "createdAt": "2019-05-28T15:25:46+00:00",
// "updatedAt": "2019-05-28T17:16:16+00:00",
// "isVisible": true,
// "visibility": "visible",
// "hasMatrix": false,
// "data01": "",
// "data02": "",
// "data03": "",
// "url": "lookin-sharp-tee",
// "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": {
// "id": 2785,
// "createdAt": "2019-05-28T15:32:12+00:00",
// "updatedAt": "2019-05-28T15:42:59+00:00",
// "name": "Shirts",
// "options": [
// {
// "id": 4626,
// "sortOrder": 1,
// "name": "Size",
// "values": [
// {
// "id": 14045,
// "sortOrder": 1,
// "name": "S"
// },
// {
// "id": 14046,
// "sortOrder": 2,
// "name": "M"
// },
// {
// "id": 14047,
// "sortOrder": 3,
// "name": "L"
// }
// ]
// }
// ]
// },
// "brand": {
// "resource": {
// "id": 1171202,
// "url": "brands/1171202",
// "link": "https://api.shoplightspeed.com/us/brands/1171202.json"
// }
// },
// "categories": {
// "resource": {
// "id": false,
// "url": "categories/products?product=20967267",
// "link": "https://api.shoplightspeed.com/us/categories/products.json?product=20967267"
// }
// },
// "deliverydate": {
// "resource": {
// "id": 6488,
// "url": "deliverydates/6488",
// "link": "https://api.shoplightspeed.com/us/deliverydates/6488.json"
// }
// },
// "image": {
// "createdAt": "2019-05-28T15:25:46+00:00",
// "updatedAt": "2019-05-28T15:25:46+00:00",
// "extension": "jpg",
// "size": 86649,
// "title": "17x35x-jcfk-88na-udwd-5hck-z63u-img-16-e0aa4f57-f4",
// "thumb": "https://cdn.shoplightspeed.com/shops/000001/files/14119398/50x50x2/17x35x-jcfk-88na-udwd-5hck-z63u-img-16-e0aa4f57-f4.jpg",
// "src": "https://cdn.shoplightspeed.com/shops/000001/files/14119398/17x35x-jcfk-88na-udwd-5hck-z63u-img-16-e0aa4f57-f4.jpg"
// },
// "images": {
// "resource": {
// "id": false,
// "url": "products/20967267/images",
// "link": "https://api.shoplightspeed.com/us/products/20967267/images.json"
// }
// },
// "relations": {
// "resource": {
// "id": false,
// "url": "products/20967267/relations",
// "link": "https://api.shoplightspeed.com/us/products/20967267/relations.json"
// }
// },
// "metafields": {
// "resource": {
// "id": false,
// "url": "products/20967267/metafields",
// "link": "https://api.shoplightspeed.com/us/products/20967267/metafields.json"
// }
// },
// "reviews": {
// "resource": {
// "id": false,
// "url": "reviews?product=20967267",
// "link": "https://api.shoplightspeed.com/us/reviews.json?product=20967267"
// }
// },
// "type": false,
// "attributes": {
// "resource": {
// "id": false,
// "url": "products/20967267/attributes",
// "link": "https://api.shoplightspeed.com/us/products/20967267/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=20967267",
// "link": "https://api.shoplightspeed.com/us/tags/products.json?product=20967267"
// }
// },
// "variants": {
// "resource": {
// "id": false,
// "url": "variants?product=20967267",
// "link": "https://api.shoplightspeed.com/us/variants.json?product=20967267"
// }
// },
// "movements": {
// "resource": {
// "id": false,
// "url": "variants/movements?product=20967267",
// "link": "https://api.shoplightspeed.com/us/variants/movements.json?product=20967267"
// }
// }
// }
// }
// 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: i32 = 0;
var sort_order: i32 = 0;
var name: [:0]const u8 = "";
var j: i32 = 0;
var count_j: i32 = 0;
_ = 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.intOf("product.set.id");
_ = try j_resp.stringOf(alloc, "product.set.createdAt");
_ = try j_resp.stringOf(alloc, "product.set.updatedAt");
_ = try j_resp.stringOf(alloc, "product.set.name");
_ = 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");
_ = try j_resp.stringOf(alloc, "product.image.createdAt");
_ = try j_resp.stringOf(alloc, "product.image.updatedAt");
_ = try j_resp.stringOf(alloc, "product.image.extension");
_ = j_resp.intOf("product.image.size");
_ = try j_resp.stringOf(alloc, "product.image.title");
_ = try j_resp.stringOf(alloc, "product.image.thumb");
_ = try j_resp.stringOf(alloc, "product.image.src");
_ = j_resp.boolOf("product.images.resource.id");
_ = try j_resp.stringOf(alloc, "product.images.resource.url");
_ = try j_resp.stringOf(alloc, "product.images.resource.link");
_ = 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");
var i: i32 = 0;
const count_i = j_resp.sizeOfArray("product.set.options");
while (i < count_i) {
j_resp.setI(i);
id = j_resp.intOf("product.set.options[i].id");
sort_order = j_resp.intOf("product.set.options[i].sortOrder");
name = try j_resp.stringOf(alloc, "product.set.options[i].name");
j = 0;
count_j = j_resp.sizeOfArray("product.set.options[i].values");
while (j < count_j) {
j_resp.setJ(j);
id = j_resp.intOf("product.set.options[i].values[j].id");
sort_order = j_resp.intOf("product.set.options[i].values[j].sortOrder");
name = try j_resp.stringOf(alloc, "product.set.options[i].values[j].name");
j = j + 1;
}
i = i + 1;
}
}