Sample code for 30+ languages & platforms
Zig

Search for Files in Google Drive

See more Google Drive Examples

This example follows the same methodology for listing all files in Google Drive in pages, but applies a search filter. It shows how to apply a query parameter for filtering the file results. See the Google Drive Files list for more optional HTTP parameters.

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

    // 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 {};

    // Our search filter is to list all files containing ".jpg" (i.e. all JPG image files)
    rest.addQueryParam("q", "name contains '.jpg'") 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 {};

        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 {};
            rest.addQueryParam("q", "name contains '.jpg'") 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;
    }
}