Zig
Zig
List all Pages of Files in Google Drive
See more Google Drive Examples
Demonstrates how iterate over pages to list files in Google Drive.See Google Drive Files list for more optional HTTP parameters.
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// It requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// This example uses a previously obtained access token having permission for the
// Google Drive scope.
const g_auth = try chilkat.AuthGoogle.init();
defer g_auth.deinit();
g_auth.setAccessToken("GOOGLE-DRIVE-ACCESS-TOKEN");
const rest = try chilkat.Rest.init();
defer rest.deinit();
// Connect using TLS.
const b_auto_reconnect = true;
try rest.connect("www.googleapis.com", 443, true, b_auto_reconnect);
// Provide the authentication credentials (i.e. the access token)
rest.setAuthGoogle(g_auth) catch {};
// Get 5 results per page for testing. (The default page size is 100, with a max of 1000.
rest.addQueryParam("pageSize", "5") catch {};
const json = try chilkat.JsonObject.init();
defer json.deinit();
var i: i32 = 0;
var num_files: i32 = 0;
// Send the request for the 1st page.
var json_response: [:0]const u8 = try rest.fullRequestNoBody(alloc, "GET", "/drive/v3/files");
var page_number: i32 = 1;
var page_token: [:0]const u8 = "";
var b_continue_loop: bool = rest.getLastMethodSuccess() and (rest.getResponseStatusCode() == 200);
while (b_continue_loop) {
std.debug.print("---- Page {d} ----\n", .{page_number});
// Iterate over each file in the response and show the name, id, and mimeType.
json.load(json_response) catch {};
// See the sample JSON response at the bottom of this example.
num_files = json.sizeOfArray("files");
i = 0;
while (i < num_files) {
json.setI(i);
std.debug.print("name: {s}\n", .{try json.stringOf(alloc, "files[i].name")});
std.debug.print("id: {s}\n", .{try json.stringOf(alloc, "files[i].id")});
std.debug.print("mimeType: {s}\n", .{try json.stringOf(alloc, "files[i].mimeType")});
std.debug.print("-\n", .{});
i = i + 1;
}
// Get the next page of files.
// If the "nextPageToken" is present in the JSON response, then use it in the "pageToken" parameter
// for the next request. If no "nextPageToken" was present, then this was the last page of files.
page_token = try json.stringOf(alloc, "nextPageToken");
b_continue_loop = false;
const b_has_more_pages = json.getLastMethodSuccess();
if (b_has_more_pages) {
rest.clearAllQueryParams() catch {};
rest.addQueryParam("pageSize", "5") catch {};
rest.addQueryParam("pageToken", page_token) catch {};
json_response = try rest.fullRequestNoBody(alloc, "GET", "/drive/v3/files");
b_continue_loop = rest.getLastMethodSuccess() and (rest.getResponseStatusCode() == 200);
page_number = page_number + 1;
}
}
if (!rest.getLastMethodSuccess()) {
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 JSON: {s}\n", .{json_response});
return;
}
// A successful JSON response looks like this:
// {
// "kind": "drive#fileList",
// "files": [
// {
// "kind": "drive#file",
// "id": "0B53Q6OSTWYolenpjTEU4ekJlQUU",
// "name": "test",
// "mimeType": "application/vnd.google-apps.folder"
// },
// {
// "kind": "drive#file",
// "id": "0B53Q6OSTWYolRm4ycjZtdXhRaEE",
// "name": "starfish4.jpg",
// "mimeType": "image/jpeg"
// },
// {
// "kind": "drive#file",
// "id": "0B53Q6OSTWYolMWt2VzN0Qlo1UjA",
// "name": "hamlet2.xml",
// "mimeType": "text/xml"
// },
// ...
// {
// "kind": "drive#file",
// "id": "0B53Q6OSTWYolc3RhcnRlcl9maWxlX2Rhc2hlclYw",
// "name": "Getting started",
// "mimeType": "application/pdf"
// }
// ]
// }
}