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

Invalidating Dependent Variables When Inputs Change

See more CURL Examples

This example demonstrates how HttpCurl automatically updates the execution plan when a known variable is cleared or changed.

After a multi-step execution plan runs successfully, variables such as site_id and drive_id may become known. Later calls to DoYourThing can reuse those values, resulting in a shorter execution plan.

However, if an input variable is cleared using ClearVar, or changed using SetVar, any variables that depend on that value are automatically invalidated. This ensures that stale values are not reused.

For example, if site_name changes, then the previously resolved site_id is no longer valid. Because drive_id depends on site_id, it is also invalidated. The next execution plan is rebuilt to resolve the dependency chain again using the new input value.

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

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

    // The target curl command we ultimately want to execute.
    // It requires drive_id.
    const target_curl = "curl -X GET https://graph.microsoft.com/v1.0/drives/{{drive_id}}/root/children";

    // Define a helper function that produces drive_id.
    // This 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 {};
    http_curl.addOutput(fn_name, "value[0].id", "drive_id") catch {};

    // Define another helper function that produces site_id.
    // This requires site_name.
    fn_name = "getSite";
    http_curl.addFunction(fn_name, "curl -X GET https://graph.microsoft.com/v1.0/sites/root:/sites/{{site_name}}") catch {};
    http_curl.addOutput(fn_name, "id", "site_id") catch {};

    // site_name is the starting known value.
    http_curl.setVar("site_name", "test");

    // Configure OAuth2 authentication.
    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 {};

    // -----------------------------------------------------------------------------
    // First execution plan:
    // site_id and drive_id are not known yet, so the full dependency chain is needed.
    // -----------------------------------------------------------------------------
    const plan_json = try chilkat.JsonObject.init();
    defer plan_json.deinit();
    plan_json.setEmitCompact(false);

    std.debug.print("Execution plan before first call:\n", .{});
    try http_curl.examinePlan(target_curl, plan_json);
    std.debug.print("{s}\n", .{try plan_json.emit(alloc)});

    // Expected:
    //
    // {
    //   "plan": [{
    //     "function": "getSite",
    //     "inputs": ["site_name"],
    //     "outputs": ["site_id"]
    //   },{
    //     "function": "getDrives",
    //     "inputs": ["site_id"],
    //     "outputs": ["drive_id"]
    //   },{
    //     "function": "targetCurl",
    //     "inputs": ["drive_id"],
    //     "outputs": []
    //   }]
    // }

    http_curl.doYourThing(target_curl) catch {
        std.debug.print("{s}\n", .{try http_curl.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("After first call:\n", .{});
    std.debug.print("site_id  = {s}\n", .{try http_curl.getVar(alloc, "site_id")});
    std.debug.print("drive_id = {s}\n", .{try http_curl.getVar(alloc, "drive_id")});

    // -----------------------------------------------------------------------------
    // Second execution plan:
    // site_id and drive_id are now known, so only the target curl command is needed.
    // -----------------------------------------------------------------------------
    std.debug.print("Execution plan after first call:\n", .{});
    try http_curl.examinePlan(target_curl, plan_json);
    std.debug.print("{s}\n", .{try plan_json.emit(alloc)});

    // Expected:
    //
    // {
    //   "plan": [{
    //     "function": "targetCurl",
    //     "inputs": ["drive_id"],
    //     "outputs": []
    //   }]
    // }

    // -----------------------------------------------------------------------------
    // Change the original input.
    // Because site_id was produced from site_name, changing site_name invalidates site_id.
    // Because drive_id depends on site_id, drive_id is also invalidated.
    // -----------------------------------------------------------------------------

    // Note: Make sure this site exists on in your SharePoint...
    http_curl.setVar("site_name", "anotherSite");

    std.debug.print("After changing site_name:\n", .{});
    std.debug.print("site_id defined?  {}\n", .{http_curl.varDefined("site_id")});
    std.debug.print("drive_id defined? {}\n", .{http_curl.varDefined("drive_id")});

    // -----------------------------------------------------------------------------
    // The execution plan is automatically rebuilt.
    // Since site_id and drive_id were invalidated, the full dependency chain is needed again.
    // -----------------------------------------------------------------------------
    std.debug.print("Execution plan after changing site_name:\n", .{});
    try http_curl.examinePlan(target_curl, plan_json);
    std.debug.print("{s}\n", .{try plan_json.emit(alloc)});

    // Expected:
    //
    // {
    //   "plan": [{
    //     "function": "getSite",
    //     "inputs": ["site_name"],
    //     "outputs": ["site_id"]
    //   },{
    //     "function": "getDrives",
    //     "inputs": ["site_id"],
    //     "outputs": ["drive_id"]
    //   },{
    //     "function": "targetCurl",
    //     "inputs": ["drive_id"],
    //     "outputs": []
    //   }]
    // }

    http_curl.doYourThing(target_curl) catch {
        std.debug.print("{s}\n", .{try http_curl.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("After running again with the new site_name:\n", .{});
    std.debug.print("site_id  = {s}\n", .{try http_curl.getVar(alloc, "site_id")});
    std.debug.print("drive_id = {s}\n", .{try http_curl.getVar(alloc, "drive_id")});

    // -----------------------------------------------------------------------------
    // Clearing a variable also invalidates values that depend on it.
    // Here, clearing site_id also invalidates drive_id.
    // -----------------------------------------------------------------------------
    http_curl.clearVar("site_id");

    std.debug.print("After clearing site_id:\n", .{});
    std.debug.print("site_id defined?  {}\n", .{http_curl.varDefined("site_id")});
    std.debug.print("drive_id defined? {}\n", .{http_curl.varDefined("drive_id")});

    std.debug.print("Execution plan after clearing site_id:\n", .{});
    try http_curl.examinePlan(target_curl, plan_json);
    std.debug.print("{s}\n", .{try plan_json.emit(alloc)});

    // Expected:
    //
    // {
    //   "plan": [{
    //     "function": "getSite",
    //     "inputs": ["site_name"],
    //     "outputs": ["site_id"]
    //   },{
    //     "function": "getDrives",
    //     "inputs": ["site_id"],
    //     "outputs": ["drive_id"]
    //   },{
    //     "function": "targetCurl",
    //     "inputs": ["drive_id"],
    //     "outputs": []
    //   }]
    // }
}