Zig
Zig
Page Through All Contacts
See more Google APIs Examples
Demonstrates how to page through the entire list of Google Contacts.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 requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// --------------------------------------------------------------------------------------------------------
// Note: The code for setting up the Chilkat REST object and making the initial connection can be done once.
// Once connected, the REST object may be re-used for many REST API calls.
// (It's a good idea to put the connection setup code in a separate function/subroutine.)
// --------------------------------------------------------------------------------------------------------
// It is assumed we previously obtained an OAuth2 access token.
// This example loads the JSON access token file
// saved by this example: Get Google Contacts OAuth2 Access Token
const json_token = try chilkat.JsonObject.init();
defer json_token.deinit();
json_token.loadFile("qa_data/tokens/googleContacts.json") catch {
std.debug.print("Failed to load googleContacts.json\n", .{});
return;
};
const g_auth = try chilkat.AuthGoogle.init();
defer g_auth.deinit();
g_auth.setAccessToken(try json_token.stringOf(alloc, "access_token"));
const rest = try chilkat.Rest.init();
defer rest.deinit();
// Connect using TLS.
const b_auto_reconnect = true;
try rest.connect("www.google.com", 443, true, b_auto_reconnect);
// Provide the authentication credentials (i.e. the access token)
rest.setAuthGoogle(g_auth) catch {};
// ----------------------------------------------
// OK, the REST connection setup is completed..
// ----------------------------------------------
var start_index: i32 = 1;
const max_results = 25;
// The totalResults will get updated with the correct value in the 1st loop iteration..
var total_results: i32 = 100;
// To retrieve the contacts in pages of 25 each, we need to send the following for each page.
// GET /m8/feeds/contacts/default/full?max-results=25&start-index=<startIndex>
// GData-Version: 3.0
const sb_max_results = try chilkat.StringBuilder.init();
defer sb_max_results.deinit();
sb_max_results.appendInt(max_results) catch {};
const sb_start_index = try chilkat.StringBuilder.init();
defer sb_start_index.deinit();
var loop_iteration: i32 = 0;
while (start_index <= total_results) {
sb_start_index.clear();
sb_start_index.appendInt(start_index) catch {};
rest.clearAllHeaders() catch {};
rest.clearAllQueryParams() catch {};
rest.addHeader("GData-Version", "3.0") catch {};
rest.addQueryParam("start-index", try sb_start_index.getAsString(alloc)) catch {};
rest.addQueryParam("max-results", try sb_max_results.getAsString(alloc)) catch {};
const sb_response_body = try chilkat.StringBuilder.init();
defer sb_response_body.deinit();
rest.fullRequestNoBodySb("GET", "/m8/feeds/contacts/default/full", sb_response_body) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// A successful response will have a status code equal to 200.
if (rest.getResponseStatusCode() != 200) {
std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
std.debug.print("response body: {s}\n", .{try sb_response_body.getAsString(alloc)});
return;
}
// If the 200 response was received, then the contacts XML is contained
// in the response body.
const xml = try chilkat.Xml.init();
defer xml.deinit();
xml.loadSb(sb_response_body, false) catch {};
// Now let's parse the XML...
// Get the the total number of results, the start index, and the items per page.
// We'll likely NOT get the full list, but will instead get the 1st page.
total_results = xml.getChildIntValue("openSearch:totalResults");
const start_index2 = xml.getChildIntValue("openSearch:startIndex");
const items_per_page = xml.getChildIntValue("openSearch:itemsPerPage");
std.debug.print("totalResults = {d}\n", .{total_results});
std.debug.print("startIndex = {d}\n", .{start_index2});
std.debug.print("itemsPerPage = {d}\n", .{items_per_page});
// Iterate over each contact.
const num_entries = xml.numChildrenHavingTag("entry");
var i: i32 = 0;
while (i < num_entries) {
xml.setI(i);
std.debug.print("{d} ----\n", .{loop_iteration * max_results + i + 1});
std.debug.print("title: {s}\n", .{try xml.getChildContent(alloc, "entry[i]|title")});
const id_url = try xml.getChildContent(alloc, "entry[i]|id");
std.debug.print("id: {s}\n", .{id_url});
if (xml.chilkatPath(alloc, "entry[i]|gd:name|gd:fullName|*")) |full_name| {
std.debug.print("fullName: {s}\n", .{full_name});
} else |_| {}
if (xml.chilkatPath(alloc, "entry[i]|gd:email|(address)")) |email_address| {
std.debug.print("email address: {s}\n", .{email_address});
} else |_| {}
// Find the photo link and check to see if this contact has a photo.
if (xml.getChildWithAttr("link", "rel", "http://schemas.google.com/contacts/2008/rel#photo")) |x_link| {
defer x_link.deinit();
// Get the photo etag.
const b_has_photo = x_link.hasAttribute("gd:etag");
if (b_has_photo) {
std.debug.print("This contact has a photo.\n", .{});
}
} else |_| {}
i = i + 1;
}
start_index = start_index + max_results;
loop_iteration = loop_iteration + 1;
}
}