|
|
(JavaScript) Invalidating Dependent Variables When Inputs Change
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. Note: This example requires Chilkat v11.5.0 or greater. For more information, see https://www.chilkatsoft.com/curl_dependency_engine.asp
var success = false;
var httpCurl = new CkHttpCurl();
// The target curl command we ultimately want to execute.
// It requires drive_id.
var targetCurl = "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 fnName = "getDrives";
httpCurl.AddFunction(fnName,"curl -X GET https://graph.microsoft.com/v1.0/sites/{{site_id}}/drives");
httpCurl.AddOutput(fnName,"value[0].id","drive_id");
// Define another helper function that produces site_id.
// This requires site_name.
fnName = "getSite";
httpCurl.AddFunction(fnName,"curl -X GET https://graph.microsoft.com/v1.0/sites/root:/sites/{{site_name}}");
httpCurl.AddOutput(fnName,"id","site_id");
// site_name is the starting known value.
httpCurl.SetVar("site_name","test");
// Configure OAuth2 authentication.
var jsonOAuth2 = new CkJsonObject();
jsonOAuth2.EnableSecrets = true;
jsonOAuth2.UpdateString("oauth2.client_id","!!sharepoint|oauth2|client_id");
jsonOAuth2.UpdateString("oauth2.client_secret","!!sharepoint|oauth2|client_secret");
jsonOAuth2.UpdateString("oauth2.scope","https://graph.microsoft.com/.default");
jsonOAuth2.UpdateString("oauth2.token_endpoint","!!sharepoint|oauth2|token_endpoint");
httpCurl.SetAuth(jsonOAuth2);
// -----------------------------------------------------------------------------
// First execution plan:
// site_id and drive_id are not known yet, so the full dependency chain is needed.
// -----------------------------------------------------------------------------
var planJson = new CkJsonObject();
planJson.EmitCompact = false;
console.log("Execution plan before first call:");
success = httpCurl.ExaminePlan(targetCurl,planJson);
console.log(planJson.Emit());
// Expected:
//
// {
// "plan": [{
// "function": "getSite",
// "inputs": ["site_name"],
// "outputs": ["site_id"]
// },{
// "function": "getDrives",
// "inputs": ["site_id"],
// "outputs": ["drive_id"]
// },{
// "function": "targetCurl",
// "inputs": ["drive_id"],
// "outputs": []
// }]
// }
success = httpCurl.DoYourThing(targetCurl);
if (success == false) {
console.log(httpCurl.LastErrorText);
return;
}
console.log("After first call:");
console.log("site_id = " + httpCurl.GetVar("site_id"));
console.log("drive_id = " + httpCurl.GetVar("drive_id"));
// -----------------------------------------------------------------------------
// Second execution plan:
// site_id and drive_id are now known, so only the target curl command is needed.
// -----------------------------------------------------------------------------
console.log("Execution plan after first call:");
success = httpCurl.ExaminePlan(targetCurl,planJson);
console.log(planJson.Emit());
// 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...
httpCurl.SetVar("site_name","anotherSite");
console.log("After changing site_name:");
console.log("site_id defined? " + httpCurl.VarDefined("site_id"));
console.log("drive_id defined? " + httpCurl.VarDefined("drive_id"));
// -----------------------------------------------------------------------------
// The execution plan is automatically rebuilt.
// Since site_id and drive_id were invalidated, the full dependency chain is needed again.
// -----------------------------------------------------------------------------
console.log("Execution plan after changing site_name:");
success = httpCurl.ExaminePlan(targetCurl,planJson);
console.log(planJson.Emit());
// Expected:
//
// {
// "plan": [{
// "function": "getSite",
// "inputs": ["site_name"],
// "outputs": ["site_id"]
// },{
// "function": "getDrives",
// "inputs": ["site_id"],
// "outputs": ["drive_id"]
// },{
// "function": "targetCurl",
// "inputs": ["drive_id"],
// "outputs": []
// }]
// }
success = httpCurl.DoYourThing(targetCurl);
if (success == false) {
console.log(httpCurl.LastErrorText);
return;
}
console.log("After running again with the new site_name:");
console.log("site_id = " + httpCurl.GetVar("site_id"));
console.log("drive_id = " + httpCurl.GetVar("drive_id"));
// -----------------------------------------------------------------------------
// Clearing a variable also invalidates values that depend on it.
// Here, clearing site_id also invalidates drive_id.
// -----------------------------------------------------------------------------
httpCurl.ClearVar("site_id");
console.log("After clearing site_id:");
console.log("site_id defined? " + httpCurl.VarDefined("site_id"));
console.log("drive_id defined? " + httpCurl.VarDefined("drive_id"));
console.log("Execution plan after clearing site_id:");
success = httpCurl.ExaminePlan(targetCurl,planJson);
console.log(planJson.Emit());
// Expected:
//
// {
// "plan": [{
// "function": "getSite",
// "inputs": ["site_name"],
// "outputs": ["site_id"]
// },{
// "function": "getDrives",
// "inputs": ["site_id"],
// "outputs": ["drive_id"]
// },{
// "function": "targetCurl",
// "inputs": ["drive_id"],
// "outputs": []
// }]
// }
|