Sample code for 30+ languages & platforms
Zig

Faire - Get All Products

See more Faire Examples

Retrieves a list of products, ordered ascending by updated_at. By default, it only returns non-deleted products.

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 -H "X-FAIRE-ACCESS-TOKEN: <access_token>" -d "limit=50" -d "page=1" https://www.faire.com/api/v1/products

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

    const query_params = try chilkat.JsonObject.init();
    defer query_params.deinit();
    query_params.updateInt("limit", 50) catch {};
    query_params.updateInt("page", 1) catch {};

    http.setRequestHeader("X-FAIRE-ACCESS-TOKEN", "<access_token>");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpParams("GET", "https://www.faire.com/api/v1/products", query_params, 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)

    // {
    //   "page": 1,
    //   "limit": 50,
    //   "products": [
    //     {
    //       "id": "p_123",
    //       "brand_id": "b_abc",
    //       "short_description": "Our candles smell fantastic. Want to know how good? Read our description!",
    //       "description": "Glad you decided to read our description! We have significantly more characters to describe to you just how good our candles smell.",
    //       "wholesale_price_cents": 500,
    //       "retail_price_cents": 1000,
    //       "sale_state": "FOR_SALE",
    //       "active": true,
    //       "deleted": false,
    //       "name": "Faire's fantastic candle",
    //       "unit_multiplier": 8,
    //       "taxonomy_type": {
    //         "id": "tt_23nl3bzl00",
    //         "name": "Votive Candle"
    //       },
    //       "options": [
    //       ],
    //       "created_at": "20190314T000915.000Z",
    //       "updated_at": "20190315T000915.000Z"
    //     }
    //   ]
    // }

    // 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: [:0]const u8 = "";
    var brand_id: [:0]const u8 = "";
    var short_description: [:0]const u8 = "";
    var description: [:0]const u8 = "";
    var wholesale_price_cents: i32 = 0;
    var retail_price_cents: i32 = 0;
    var sale_state: [:0]const u8 = "";
    var active: bool = false;
    var deleted: bool = false;
    var name: [:0]const u8 = "";
    var unit_multiplier: i32 = 0;
    var taxonomy_type_id: [:0]const u8 = "";
    var taxonomy_type_name: [:0]const u8 = "";
    var created_at: [:0]const u8 = "";
    var updated_at: [:0]const u8 = "";
    var j: i32 = 0;
    var count_j: i32 = 0;

    _ = j_resp.intOf("page");
    _ = j_resp.intOf("limit");
    var i: i32 = 0;
    const count_i = j_resp.sizeOfArray("products");
    while (i < count_i) {
        j_resp.setI(i);
        id = try j_resp.stringOf(alloc, "products[i].id");
        brand_id = try j_resp.stringOf(alloc, "products[i].brand_id");
        short_description = try j_resp.stringOf(alloc, "products[i].short_description");
        description = try j_resp.stringOf(alloc, "products[i].description");
        wholesale_price_cents = j_resp.intOf("products[i].wholesale_price_cents");
        retail_price_cents = j_resp.intOf("products[i].retail_price_cents");
        sale_state = try j_resp.stringOf(alloc, "products[i].sale_state");
        active = j_resp.boolOf("products[i].active");
        deleted = j_resp.boolOf("products[i].deleted");
        name = try j_resp.stringOf(alloc, "products[i].name");
        unit_multiplier = j_resp.intOf("products[i].unit_multiplier");
        taxonomy_type_id = try j_resp.stringOf(alloc, "products[i].taxonomy_type.id");
        taxonomy_type_name = try j_resp.stringOf(alloc, "products[i].taxonomy_type.name");
        created_at = try j_resp.stringOf(alloc, "products[i].created_at");
        updated_at = try j_resp.stringOf(alloc, "products[i].updated_at");
        j = 0;
        count_j = j_resp.sizeOfArray("products[i].options");
        while (j < count_j) {
            j_resp.setJ(j);
            j = j + 1;
        }

        i = i + 1;
    }
}