Sample code for 30+ languages & platforms
Zig

RSAP Union API - Get Members Status

See more _Miscellaneous_ Examples

Demonstrates how to use an OAuth2 access token for the RSAP Union API. Calls the endpoint to get the statuses of all union members.

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

    // Load the access token previously obtained by this example:  RSAP Union OAuth2
    const j_token = try chilkat.JsonObject.init();
    defer j_token.deinit();
    j_token.loadFile("qa_data/tokens/rsapToken.json") catch {
        std.debug.print("Failed to load access token JSON.\n", .{});
        return;
    };

    // Adds the "Authorization: Bearer ACCESS_TOKEN" header.
    http.setAuthToken(try j_token.stringOf(alloc, "access_token"));

    // For authentication, assuming both the client cert and access token are needed???
    const cert = try chilkat.Cert.init();
    defer cert.deinit();
    cert.loadFromFile("qa_data/certs_and_keys/union_client_certificate.crt") catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    const priv_key = try chilkat.PrivateKey.init();
    defer priv_key.deinit();
    priv_key.loadAnyFormatFile("qa_data/certs_and_keys/union_client_certificate.nopass.key", "") catch {
        std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
        return;
    };

    // Associate the private key with the cert.
    // This will fail if the private key is not actually the correct one that corresponds to the public key stored within the cert.
    cert.setPrivateKey(priv_key) catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    // Tell HTTP to use the cert for client TLS certificate authentication.
    http.setSslClientCert(cert) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    http.quickGetSb("https://api-test.rsap.ca/members/status", 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;
    }
}