Zig
Zig
Populi Get Roles
See more Populi Examples
Demonstrates the Populi getRoles task.Chilkat Zig Downloads
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.
// First load the previously obtained API token.
// See Get Populi Access Token for sample code showing how to get the API token.
const xml = try chilkat.Xml.init();
defer xml.deinit();
try xml.loadXmlFile("qa_data/tokens/populi_token.xml");
const access_key = xml.getChildContent(alloc, "access_key") catch {
std.debug.print("Did not find the access_key\n", .{});
return;
};
const rest = try chilkat.Rest.init();
defer rest.deinit();
// Connect using TLS.
// A single REST object, once connected, can be used for many Populi REST API calls.
// The auto-reconnect indicates that if the already-established HTTPS connection is closed,
// then it will be automatically re-established as needed.
const b_auto_reconnect = true;
rest.connect("yourcollege.populi.co", 443, true, b_auto_reconnect) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
rest.setAuthorization(access_key);
rest.addQueryParam("task", "getRoles") catch {};
const response_body = rest.fullRequestFormUrlEncoded(alloc, "POST", "/api/index.php") catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// We should expect a 200 response if successful.
if (rest.getResponseStatusCode() != 200) {
std.debug.print("Request Header: \n", .{});
std.debug.print("{s}\n", .{try rest.getLastRequestHeader(alloc)});
std.debug.print("----\n", .{});
std.debug.print("Response StatusCode = {d}\n", .{rest.getResponseStatusCode()});
std.debug.print("Response StatusLine: {s}\n", .{try rest.getResponseStatusText(alloc)});
std.debug.print("Response Header:\n", .{});
std.debug.print("{s}\n", .{try rest.getResponseHeader(alloc)});
std.debug.print("Response Body:\n", .{});
std.debug.print("{s}\n", .{response_body});
return;
}
xml.loadXml(response_body) catch {};
std.debug.print("{s}\n", .{try xml.getXml(alloc)});
// Sample response:
// Use this online tool to generate parsing code from sample XML:
// Generate Parsing Code from XML
// <?xml version="1.0" encoding="ISO-8859-1"?>
// <response>
// <role>
// <id>7</id>
// <name>Academic Admin</name>
// <inactive>0</inactive>
// </role>
// <role>
// <id>19</id>
// <name>Financial Aid</name>
// <inactive>0</inactive>
// </role>
// <role>
// <id>15</id>
// <name>Registrar</name>
// <inactive>0</inactive>
// </role>
// <role>
// <id>4</id>
// <name>Staff</name>
// <inactive>0</inactive>
// </role>
// <role>
// <id>1</id>
// <name>Everyone</name>
// <inactive>0</inactive>
// </role>
// </response>
var i: i32 = 0;
var id: i32 = 0;
var name: [:0]const u8 = "";
var inactive: i32 = 0;
i = 0;
const count_i = xml.numChildrenHavingTag("role");
while (i < count_i) {
xml.setI(i);
id = xml.getChildIntValue("role[i]|id");
name = try xml.getChildContent(alloc, "role[i]|name");
inactive = xml.getChildIntValue("role[i]|inactive");
i = i + 1;
}
}