Sample code for 30+ languages & platforms
Zig

GetHarvest - List Clients

See more GetHarvest Examples

Returns a list of your clients.

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://api.harvestapp.com/v2/clients" \
    //   -H "Authorization: Bearer ACCESS_TOKEN" \
    //   -H "Harvest-Account-Id: ACCOUNT_ID" \
    //   -H "User-Agent: MyApp (yourname@example.com)"

    http.setRequestHeader("User-Agent", "MyApp (yourname@example.com)");
    http.setRequestHeader("Authorization", "Bearer ACCESS_TOKEN");
    http.setRequestHeader("Harvest-Account-Id", "ACCOUNT_ID");

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    http.quickGetSb("https://api.harvestapp.com/v2/clients", 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:

    // {
    //   "clients": [
    //     {
    //       "id": 5735776,
    //       "name": "123 Industries",
    //       "is_active": true,
    //       "address": "123 Main St.\r\nAnytown, LA 71223",
    //       "created_at": "2017-06-26T21:02:12Z",
    //       "updated_at": "2017-06-26T21:34:11Z",
    //       "currency": "EUR"
    //     },
    //     {
    //       "id": 5735774,
    //       "name": "ABC Corp",
    //       "is_active": true,
    //       "address": "456 Main St.\r\nAnytown, CT 06467",
    //       "created_at": "2017-06-26T21:01:52Z",
    //       "updated_at": "2017-06-26T21:27:07Z",
    //       "currency": "USD"
    //     }
    //   ],
    //   "per_page": 100,
    //   "total_pages": 1,
    //   "total_entries": 2,
    //   "next_page": null,
    //   "previous_page": null,
    //   "page": 1,
    //   "links": {
    //     "first": "https://api.harvestapp.com/v2/clients?page=1&per_page=100",
    //     "next": null,
    //     "previous": null,
    //     "last": "https://api.harvestapp.com/v2/clients?page=1&per_page=100"
    //   }
    // }

    // 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 i: i32 = 0;
    var id: i32 = 0;
    var name: [:0]const u8 = "";
    var is_active: bool = false;
    var address: [:0]const u8 = "";
    var created_at: [:0]const u8 = "";
    var updated_at: [:0]const u8 = "";
    var currency: [:0]const u8 = "";

    _ = j_resp.intOf("per_page");
    _ = j_resp.intOf("total_pages");
    _ = j_resp.intOf("total_entries");
    _ = try j_resp.stringOf(alloc, "next_page");
    _ = try j_resp.stringOf(alloc, "previous_page");
    _ = j_resp.intOf("page");
    _ = try j_resp.stringOf(alloc, "links.first");
    _ = try j_resp.stringOf(alloc, "links.next");
    _ = try j_resp.stringOf(alloc, "links.previous");
    _ = try j_resp.stringOf(alloc, "links.last");
    i = 0;
    const count_i = j_resp.sizeOfArray("clients");
    while (i < count_i) {
        j_resp.setI(i);
        id = j_resp.intOf("clients[i].id");
        name = try j_resp.stringOf(alloc, "clients[i].name");
        is_active = j_resp.boolOf("clients[i].is_active");
        address = try j_resp.stringOf(alloc, "clients[i].address");
        created_at = try j_resp.stringOf(alloc, "clients[i].created_at");
        updated_at = try j_resp.stringOf(alloc, "clients[i].updated_at");
        currency = try j_resp.stringOf(alloc, "clients[i].currency");
        i = i + 1;
    }
}