Zig
Zig
Populi Search People
See more Populi Examples
Demonstrates the Populi searchPeople 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", "searchPeople") catch {};
rest.addQueryParam("search_term", "Robert") catch {};
rest.addQueryParam("limit", "10") 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="UTF-8"?>
// <response>
// <person>
// <id>11111</id>
// <first_name>Robert</first_name>
// <last_name>McStudent</last_name>
// <middle_name>Kensington</middle_name>
// <preferred_name>Bobby</preferred_name>
// <is_user>1</is_user>
// <primary_email>r.mcstudent@myschool.edu</primary_email>
// </person>
// <person>
// <id>2222</id>
// <first_name>Robert</first_name>
// <last_name>McBoardmember</last_name>
// <middle_name/>
// <preferred_name/>
// <is_user>0</is_user>
// <primary_email>robert@gmail.com</primary_email>
// </person>
// </response>
var i: i32 = 0;
var id: i32 = 0;
var first_name: [:0]const u8 = "";
var last_name: [:0]const u8 = "";
var middle_name: [:0]const u8 = "";
var preferred_name: [:0]const u8 = "";
var is_user: i32 = 0;
var primary_email: [:0]const u8 = "";
i = 0;
const count_i = xml.numChildrenHavingTag("person");
while (i < count_i) {
xml.setI(i);
id = xml.getChildIntValue("person[i]|id");
first_name = try xml.getChildContent(alloc, "person[i]|first_name");
last_name = try xml.getChildContent(alloc, "person[i]|last_name");
middle_name = try xml.getChildContent(alloc, "person[i]|middle_name");
preferred_name = try xml.getChildContent(alloc, "person[i]|preferred_name");
is_user = xml.getChildIntValue("person[i]|is_user");
primary_email = try xml.getChildContent(alloc, "person[i]|primary_email");
i = i + 1;
}
}