Sample code for 30+ languages & platforms
Zig

Etsy: Get the Inventory for a Listing

See more Etsy Examples

Gets the inventory for a listing.

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 -X GET \
    //   https://openapi.etsy.com/v2/listings/listing_id/inventory?api_key=MY_ETSY_KEYSTRING

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    http.quickGetSb("https://openapi.etsy.com/v2/listings/listing_id/inventory?api_key=MY_ETSY_KEYSTRING", 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)

    // {
    //   "count": 1,
    //   "results": {
    //     "products": [
    //       {
    //         "product_id": 3361120103,
    //         "property_values": [
    //         ],
    //         "offerings": [
    //           {
    //             "offering_id": 3579642570,
    //             "price": {
    //               "amount": 16000,
    //               "divisor": 100,
    //               "currency_code": "USD",
    //               "currency_formatted_short": "$160.00",
    //               "currency_formatted_long": "$160.00 USD",
    //               "currency_formatted_raw": "160.00"
    //             },
    //             "quantity": 1
    //           }
    //         ]
    //       }
    //     ]
    //   },
    //   "params": {
    //     "listing_id": "720138253",
    //     "write_missing_inventory": false
    //   },
    //   "type": "ListingInventory",
    //   "pagination": {}
    // }

    // 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 product_id: i32 = 0;
    var j: i32 = 0;
    var count_j: i32 = 0;
    var offering_id: i32 = 0;
    var price_amount: i32 = 0;
    var price_divisor: i32 = 0;
    var price_currency_code: [:0]const u8 = "";
    var price_currency_formatted_short: [:0]const u8 = "";
    var price_currency_formatted_long: [:0]const u8 = "";
    var price_currency_formatted_raw: [:0]const u8 = "";
    var quantity: i32 = 0;

    _ = j_resp.intOf("count");
    _ = try j_resp.stringOf(alloc, "params.listing_id");
    _ = j_resp.boolOf("params.write_missing_inventory");
    _ = try j_resp.stringOf(alloc, "type");
    var i: i32 = 0;
    const count_i = j_resp.sizeOfArray("results.products");
    while (i < count_i) {
        j_resp.setI(i);
        product_id = j_resp.intOf("results.products[i].product_id");
        j = 0;
        count_j = j_resp.sizeOfArray("results.products[i].property_values");
        while (j < count_j) {
            j_resp.setJ(j);
            j = j + 1;
        }

        j = 0;
        count_j = j_resp.sizeOfArray("results.products[i].offerings");
        while (j < count_j) {
            j_resp.setJ(j);
            offering_id = j_resp.intOf("results.products[i].offerings[j].offering_id");
            price_amount = j_resp.intOf("results.products[i].offerings[j].price.amount");
            price_divisor = j_resp.intOf("results.products[i].offerings[j].price.divisor");
            price_currency_code = try j_resp.stringOf(alloc, "results.products[i].offerings[j].price.currency_code");
            price_currency_formatted_short = try j_resp.stringOf(alloc, "results.products[i].offerings[j].price.currency_formatted_short");
            price_currency_formatted_long = try j_resp.stringOf(alloc, "results.products[i].offerings[j].price.currency_formatted_long");
            price_currency_formatted_raw = try j_resp.stringOf(alloc, "results.products[i].offerings[j].price.currency_formatted_raw");
            quantity = j_resp.intOf("results.products[i].offerings[j].quantity");
            j = j + 1;
        }

        i = i + 1;
    }
}