Sample code for 30+ languages & platforms
Zig

Upsert Catalog Object

See more Square Examples

Creates or updates the target CatalogObject.

Chilkat Zig Downloads

Zig
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://connect.squareup.com/v2/catalog/object \
    //   -X POST \
    //   -H 'Square-Version: 2020-07-22' \
    //   -H 'Authorization: Bearer ACCESS_TOKEN' \
    //   -H 'Content-Type: application/json' \
    //   -d '{
    //     "idempotency_key": "af3d1afc-7212-4300-b463-0bfc5314a5ae",
    //     "object": {
    //       "type": "ITEM",
    //       "id": "#Cocoa",
    //       "item_data": {
    //         "name": "Cocoa",
    //         "description": "Hot chocolate",
    //         "abbreviation": "Ch"
    //       }
    //     }
    //   }'

    // 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.

    // {
    //   "idempotency_key": "af3d1afc-7212-4300-b463-0bfc5314a5ae",
    //   "object": {
    //     "type": "ITEM",
    //     "id": "#Cocoa",
    //     "item_data": {
    //       "name": "Cocoa",
    //       "description": "Hot chocolate",
    //       "abbreviation": "Ch"
    //     }
    //   }
    // }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("idempotency_key", "af3d1afc-7212-4300-b463-0bfc5314a5ae") catch {};
    json.updateString("object.type", "ITEM") catch {};
    json.updateString("object.id", "#Cocoa") catch {};
    json.updateString("object.item_data.name", "Cocoa") catch {};
    json.updateString("object.item_data.description", "Hot chocolate") catch {};
    json.updateString("object.item_data.abbreviation", "Ch") catch {};

    // Adds the "Authorization: Bearer ACCESS_TOKEN" header.
    http.setAuthToken("ACCESS_TOKEN");
    http.setRequestHeader("Square-Version", "2020-07-22");

    // This example uses the sandbox: connect.squareupsandbox.com
    // Production should use connect.squareup.com
    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://connect.squareupsandbox.com/v2/catalog/object", 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)

    // {
    //   "catalog_object": {
    //     "type": "ITEM",
    //     "id": "7SB3ZQYJ5GDMVFL7JK46JCHT",
    //     "updated_at": "2016-11-16T22:32:42.996Z",
    //     "version": 1479335562996,
    //     "is_deleted": false,
    //     "item_data": {
    //       "name": "Cocoa",
    //       "description": "Hot chocolate",
    //       "abbreviation": "Ch"
    //     }
    //   },
    //   "id_mappings": [
    //     {
    //       "client_object_id": "#Cocoa",
    //       "object_id": "7SB3ZQYJ5GDMVFL7JK46JCHT"
    //     }
    //   ]
    // }

    // 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 client_object_id: [:0]const u8 = "";
    var object_id: [:0]const u8 = "";

    _ = try j_resp.stringOf(alloc, "catalog_object.type");
    _ = try j_resp.stringOf(alloc, "catalog_object.id");
    _ = try j_resp.stringOf(alloc, "catalog_object.updated_at");
    _ = j_resp.intOf("catalog_object.version");
    _ = j_resp.boolOf("catalog_object.is_deleted");
    _ = try j_resp.stringOf(alloc, "catalog_object.item_data.name");
    _ = try j_resp.stringOf(alloc, "catalog_object.item_data.description");
    _ = try j_resp.stringOf(alloc, "catalog_object.item_data.abbreviation");
    var i: i32 = 0;
    const count_i = j_resp.sizeOfArray("id_mappings");
    while (i < count_i) {
        j_resp.setI(i);
        client_object_id = try j_resp.stringOf(alloc, "id_mappings[i].client_object_id");
        object_id = try j_resp.stringOf(alloc, "id_mappings[i].object_id");
        i = i + 1;
    }
}