Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_HttpCurl
string ls_TargetCurl
string ls_FnName
oleobject loo_JsonOAuth2
oleobject loo_PlanJson

li_Success = 0

loo_HttpCurl = create oleobject
li_rc = loo_HttpCurl.ConnectToNewObject("Chilkat.HttpCurl")
if li_rc < 0 then
    destroy loo_HttpCurl
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// The target curl command we ultimately want to execute.
// It requires drive_id.
ls_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.
ls_FnName = "getDrives"
loo_HttpCurl.AddFunction(ls_FnName,"curl -X GET https://graph.microsoft.com/v1.0/sites/{{site_id}}/drives")
loo_HttpCurl.AddOutput(ls_FnName,"value[0].id","drive_id")

// Define another helper function that produces site_id.
// This requires site_name.
ls_FnName = "getSite"
loo_HttpCurl.AddFunction(ls_FnName,"curl -X GET https://graph.microsoft.com/v1.0/sites/root:/sites/{{site_name}}")
loo_HttpCurl.AddOutput(ls_FnName,"id","site_id")

// site_name is the starting known value.
loo_HttpCurl.SetVar("site_name","test")

// Configure OAuth2 authentication.
loo_JsonOAuth2 = create oleobject
li_rc = loo_JsonOAuth2.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonOAuth2.EnableSecrets = 1
loo_JsonOAuth2.UpdateString("oauth2.client_id","!!sharepoint|oauth2|client_id")
loo_JsonOAuth2.UpdateString("oauth2.client_secret","!!sharepoint|oauth2|client_secret")
loo_JsonOAuth2.UpdateString("oauth2.scope","https://graph.microsoft.com/.default")
loo_JsonOAuth2.UpdateString("oauth2.token_endpoint","!!sharepoint|oauth2|token_endpoint")
loo_HttpCurl.SetAuth(loo_JsonOAuth2)

// -----------------------------------------------------------------------------
// First execution plan:
// site_id and drive_id are not known yet, so the full dependency chain is needed.
// -----------------------------------------------------------------------------
loo_PlanJson = create oleobject
li_rc = loo_PlanJson.ConnectToNewObject("Chilkat.JsonObject")

loo_PlanJson.EmitCompact = 0

Write-Debug "Execution plan before first call:"
li_Success = loo_HttpCurl.ExaminePlan(ls_TargetCurl,loo_PlanJson)
Write-Debug loo_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": []
//   }]
// }

li_Success = loo_HttpCurl.DoYourThing(ls_TargetCurl)
if li_Success = 0 then
    Write-Debug loo_HttpCurl.LastErrorText
    destroy loo_HttpCurl
    destroy loo_JsonOAuth2
    destroy loo_PlanJson
    return
end if

Write-Debug "After first call:"
Write-Debug "site_id  = " + loo_HttpCurl.GetVar("site_id")
Write-Debug "drive_id = " + loo_HttpCurl.GetVar("drive_id")

// -----------------------------------------------------------------------------
// Second execution plan:
// site_id and drive_id are now known, so only the target curl command is needed.
// -----------------------------------------------------------------------------
Write-Debug "Execution plan after first call:"
li_Success = loo_HttpCurl.ExaminePlan(ls_TargetCurl,loo_PlanJson)
Write-Debug loo_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...
loo_HttpCurl.SetVar("site_name","anotherSite")

Write-Debug "After changing site_name:"
Write-Debug "site_id defined?  " + string(loo_HttpCurl.VarDefined("site_id"))
Write-Debug "drive_id defined? " + string(loo_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.
// -----------------------------------------------------------------------------
Write-Debug "Execution plan after changing site_name:"
li_Success = loo_HttpCurl.ExaminePlan(ls_TargetCurl,loo_PlanJson)
Write-Debug loo_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": []
//   }]
// }

li_Success = loo_HttpCurl.DoYourThing(ls_TargetCurl)
if li_Success = 0 then
    Write-Debug loo_HttpCurl.LastErrorText
    destroy loo_HttpCurl
    destroy loo_JsonOAuth2
    destroy loo_PlanJson
    return
end if

Write-Debug "After running again with the new site_name:"
Write-Debug "site_id  = " + loo_HttpCurl.GetVar("site_id")
Write-Debug "drive_id = " + loo_HttpCurl.GetVar("drive_id")

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

Write-Debug "After clearing site_id:"
Write-Debug "site_id defined?  " + string(loo_HttpCurl.VarDefined("site_id"))
Write-Debug "drive_id defined? " + string(loo_HttpCurl.VarDefined("drive_id"))

Write-Debug "Execution plan after clearing site_id:"
li_Success = loo_HttpCurl.ExaminePlan(ls_TargetCurl,loo_PlanJson)
Write-Debug loo_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": []
//   }]
// }


destroy loo_HttpCurl
destroy loo_JsonOAuth2
destroy loo_PlanJson