Sample code for 30+ languages & platforms
Zig

GetHarvest - Test Personal Access Token

See more GetHarvest Examples

Demonstrates how to test your GetHarvest Personal Access Token

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();

    http.setRequestHeader("User-Agent", "Harvest API Example");
    http.setRequestHeader("Harvest-Account-ID", "ACCOUNT_ID");
    http.setRequestHeader("Authorization", "Bearer ACCESS_TOKEN");

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

    // Sample JSON response:

    // {
    //   "id": 2826142,
    //   "first_name": "Joe",
    //   "last_name": "Johnson",
    //   "email": "joe@example.com",
    //   "telephone": "",
    //   "timezone": "Central Time (US & Canada)",
    //   "weekly_capacity": 126000,
    //   "has_access_to_all_future_projects": false,
    //   "is_contractor": false,
    //   "is_admin": true,
    //   "is_project_manager": false,
    //   "can_see_rates": true,
    //   "can_create_projects": true,
    //   "can_create_invoices": true,
    //   "is_active": true,
    //   "created_at": "2019-06-06T15:57:58Z",
    //   "updated_at": "2019-06-06T15:58:32Z",
    //   "default_hourly_rate": 175.0,
    //   "cost_rate": 60.0,
    //   "roles": [
    //     "role1",
    //     "role2"
    //   ],
    //   "avatar_url": "https://d3s3969qhosaug.cloudfront.net/default-avatars/4d46.png?1559836678"
    // }

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

    _ = j_resp.intOf("id");
    _ = try j_resp.stringOf(alloc, "first_name");
    _ = try j_resp.stringOf(alloc, "last_name");
    _ = try j_resp.stringOf(alloc, "email");
    _ = try j_resp.stringOf(alloc, "telephone");
    _ = try j_resp.stringOf(alloc, "timezone");
    _ = j_resp.intOf("weekly_capacity");
    _ = j_resp.boolOf("has_access_to_all_future_projects");
    _ = j_resp.boolOf("is_contractor");
    _ = j_resp.boolOf("is_admin");
    _ = j_resp.boolOf("is_project_manager");
    _ = j_resp.boolOf("can_see_rates");
    _ = j_resp.boolOf("can_create_projects");
    _ = j_resp.boolOf("can_create_invoices");
    _ = j_resp.boolOf("is_active");
    _ = try j_resp.stringOf(alloc, "created_at");
    _ = try j_resp.stringOf(alloc, "updated_at");
    _ = try j_resp.stringOf(alloc, "default_hourly_rate");
    _ = try j_resp.stringOf(alloc, "cost_rate");
    _ = try j_resp.stringOf(alloc, "avatar_url");
    i = 0;
    const count_i = j_resp.sizeOfArray("roles");
    while (i < count_i) {
        j_resp.setI(i);
        str_val = try j_resp.stringOf(alloc, "roles[i]");
        i = i + 1;
    }
}