Zig
Zig
Shopware 6 - Find Category by Name
See more Shopware 6 Examples
Find a category with a given name and get the id.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();
// Sends the following POST to find the category named "Test123"
// POST /api/v3/search/category
// {
// "filter": [
// { "type": "equals", "field": "name", "value": "Test123" }
// ],
// "includes": {
// "category": ["id", "name"]
// }
// }
// Create the above JSON.
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("filter[0].type", "equals") catch {};
json.updateString("filter[0].field", "name") catch {};
json.updateString("filter[0].value", "Test123") catch {};
json.updateString("includes.category[0]", "id") catch {};
json.updateString("includes.category[1]", "name") catch {};
// Load the access token previously obtained in Shopware 6 OAuth2 Client Credentials
const json_token = try chilkat.JsonObject.init();
defer json_token.deinit();
json_token.loadFile("qa_data/tokens/shopware6.json") catch {};
// This causes the "Authorization: Bearer <access_token>" header to be added.
http.setAuthToken(try json_token.stringOf(alloc, "access_token"));
// Note: If you get a 401 response status code, then fetch a new access token and retry.
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpJson("POST", "https://my-shopware-6-shop.de/api/v3/search/category", 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)});
// If we get a 401 response, it may be that our access token expired and we need to fetch a new one.
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;
}
// The response looks like this:
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
// {
// "data": [
// {
// "id": "89c4131789fd422c8f00cf37bbb83330",
// "type": "category",
// "attributes": {
// "name": "Test123",
// "apiAlias": null
// },
// "links": {
// "self": "https:\/\/***.de\/api\/v3\/category\/89c4131789fd422c8f00cf37bbb83330"
// },
// "relationships": [
// ],
// "meta": null
// }
// ],
// "included": [
// ],
// "links": {
// "self": "https:\/\/***.de\/api\/v3\/search\/category"
// },
// "meta": {
// "totalCountMode": 0,
// "total": 1
// },
// "aggregations": [
// ]
// }
const num_search_results = j_resp.sizeOfArray("data");
if (num_search_results != 1) {
std.debug.print("numSearchResults = {d}\n", .{num_search_results});
std.debug.print("Did not find the category, or unexpectedly found more than one with the same name.\n", .{});
return;
}
const category_id = try j_resp.stringOf(alloc, "data[0].id");
std.debug.print("Category ID = {s}\n", .{category_id});
}