Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.5.0+

Inspect the Complete Set of Defined Variables

See more CURL Examples

This example shows how to inspect the complete set of defined variables after executing the curl dependency plan. The code that enumerates and displays all variables is located at the end of the example.

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

    // This example shows how to inspect the complete set of defined variables
    // after executing the curl dependency plan.
    // The code that enumerates and displays all variables is located at the end of the example.

    const http_curl = try chilkat.HttpCurl.init();
    defer http_curl.deinit();

    // The final curl command we want to execute.
    // It depends on {{drive_id}}, which is not yet known.
    const target_curl = "curl -X GET https://graph.microsoft.com/v1.0/drives/{{drive_id}}/root/children";

    // Define a helper function that can produce drive_id.
    // This function lists drives for a given site, which requires {{site_id}}.
    var fn_name: [:0]const u8 = "getDrives";
    http_curl.addFunction(fn_name, "curl -X GET https://graph.microsoft.com/v1.0/sites/{{site_id}}/drives") catch {};

    // Define the output of getDrives.
    // The JSON response contains an array named "value".
    // We take the first element (value[0]) and extract its "id" as drive_id.
    var json_path: [:0]const u8 = "value[0].id";
    http_curl.addOutput(fn_name, json_path, "drive_id") catch {};

    // drive_id depends on site_id, which is also not yet known.
    // Define another helper function to obtain site_id.
    fn_name = "getSite";
    http_curl.addFunction(fn_name, "GET https://graph.microsoft.com/v1.0/sites/root:/sites/{{site_name}}") catch {};

    // Map the "id" field from the getSite response to the variable site_id.
    json_path = "id";
    http_curl.addOutput(fn_name, json_path, "site_id") catch {};

    // Provide the initial known input.
    // site_name is the starting value that allows the dependency chain to be resolved.
    http_curl.setVar("site_name", "test");

    // ----------------------------------------------------------------------------------------------------------------------
    // Configure OAuth2 authentication using the client credentials flow.
    // Secrets (client_id, client_secret, token_endpoint) are retrieved from the
    // local secrets manager because EnableSecrets is enabled.
    const json_o_auth2 = try chilkat.JsonObject.init();
    defer json_o_auth2.deinit();
    json_o_auth2.setEnableSecrets(true);
    json_o_auth2.updateString("oauth2.client_id", "!!sharepoint|oauth2|client_id") catch {};
    json_o_auth2.updateString("oauth2.client_secret", "!!sharepoint|oauth2|client_secret") catch {};
    json_o_auth2.updateString("oauth2.scope", "https://graph.microsoft.com/.default") catch {};
    json_o_auth2.updateString("oauth2.token_endpoint", "!!sharepoint|oauth2|token_endpoint") catch {};
    http_curl.setAuth(json_o_auth2) catch {};
    // ----------------------------------------------------------------------------------------------------------------------

    // Execute the target curl command.
    // Internally, the system will:
    //
    // 1) Detect that {{drive_id}} is required but not defined.
    // 2) Find that "getDrives" can produce drive_id.
    // 3) Detect that getDrives requires {{site_id}}, which is also not defined.
    // 4) Find that "getSite" can produce site_id.
    // 5) Verify that getSite requires {{site_name}}, which is already known.
    // 6) Build a multi-step execution plan:
    //      - First:  run getSite (produces site_id)
    //      - Second: run getDrives (produces drive_id)
    //      - Third:  run the target curl (uses drive_id)
    http_curl.doYourThing(target_curl) catch {
        std.debug.print("{s}\n", .{try http_curl.getLastErrorText(alloc)});
        return;
    };

    // Retrieve and display the HTTP response status code.
    const status_code = http_curl.getStatusCode();
    std.debug.print("response status code: {d}\n", .{status_code});

    // Load and display the JSON response from the final curl command.
    const response_json = try chilkat.JsonObject.init();
    defer response_json.deinit();
    response_json.setEmitCompact(false);
    http_curl.getResponseJson(response_json) catch {};
    std.debug.print("{s}\n", .{try response_json.emit(alloc)});

    // ----------------------------------
    // Examine all defined variables.
    // ----------------------------------
    const defined_vars_json = try chilkat.JsonObject.init();
    defer defined_vars_json.deinit();
    defined_vars_json.setEmitCompact(false);

    http_curl.getAllVars(defined_vars_json);
    std.debug.print("{s}\n", .{try defined_vars_json.emit(alloc)});

    // Sample output:

    // {
    //   "vars": [
    //     {
    //       "name": "site_name",
    //       "value": "test"
    //     },
    //     {
    //       "name": "site_id",
    //       "value": "example.sharepoint.com,9b923c5e-5117-44ad-8b03-adbb8e19be85,b2451e19-290f-5f29-9f5d-674c2950a9f7"
    //     },
    //     {
    //       "name": "drive_id",
    //       "value": "b!XjySmxdRrrtLA827jhm-nRkeRbIPKSlPn11nTClQqfeVOj5VUvQMQae34rwOHdiP"
    //     }
    //   ]
    // }

    // Sample code to parse the above JSON.
    var i: i32 = 0;
    const num_vars = defined_vars_json.sizeOfArray("vars");
    while (i < num_vars) {
        defined_vars_json.setI(i);
        std.debug.print("name: {s}\n", .{try defined_vars_json.stringOf(alloc, "vars[i].name")});
        std.debug.print("value: {s}\n", .{try defined_vars_json.stringOf(alloc, "vars[i].value")});
        i = i + 1;
    }
}