Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttpCurl
    String sTargetCurl
    String sFnName
    Variant vJsonOAuth2
    Handle hoJsonOAuth2
    Variant vPlanJson
    Handle hoPlanJson
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatHttpCurl)) To hoHttpCurl
    If (Not(IsComObjectCreated(hoHttpCurl))) Begin
        Send CreateComObject of hoHttpCurl
    End

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

    // Define a helper function that produces drive_id.
    // This requires site_id.
    Move "getDrives" To sFnName
    Get ComAddFunction Of hoHttpCurl sFnName "curl -X GET https://graph.microsoft.com/v1.0/sites/{{site_id}}/drives" To iSuccess
    Get ComAddOutput Of hoHttpCurl sFnName "value[0].id" "drive_id" To iSuccess

    // Define another helper function that produces site_id.
    // This requires site_name.
    Move "getSite" To sFnName
    Get ComAddFunction Of hoHttpCurl sFnName "curl -X GET https://graph.microsoft.com/v1.0/sites/root:/sites/{{site_name}}" To iSuccess
    Get ComAddOutput Of hoHttpCurl sFnName "id" "site_id" To iSuccess

    // site_name is the starting known value.
    Send ComSetVar To hoHttpCurl "site_name" "test"

    // Configure OAuth2 authentication.
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOAuth2
    If (Not(IsComObjectCreated(hoJsonOAuth2))) Begin
        Send CreateComObject of hoJsonOAuth2
    End
    Set ComEnableSecrets Of hoJsonOAuth2 To True
    Get ComUpdateString Of hoJsonOAuth2 "oauth2.client_id" "!!sharepoint|oauth2|client_id" To iSuccess
    Get ComUpdateString Of hoJsonOAuth2 "oauth2.client_secret" "!!sharepoint|oauth2|client_secret" To iSuccess
    Get ComUpdateString Of hoJsonOAuth2 "oauth2.scope" "https://graph.microsoft.com/.default" To iSuccess
    Get ComUpdateString Of hoJsonOAuth2 "oauth2.token_endpoint" "!!sharepoint|oauth2|token_endpoint" To iSuccess
    Get pvComObject of hoJsonOAuth2 to vJsonOAuth2
    Get ComSetAuth Of hoHttpCurl vJsonOAuth2 To iSuccess

    // -----------------------------------------------------------------------------
    // First execution plan:
    // site_id and drive_id are not known yet, so the full dependency chain is needed.
    // -----------------------------------------------------------------------------
    Get Create (RefClass(cComChilkatJsonObject)) To hoPlanJson
    If (Not(IsComObjectCreated(hoPlanJson))) Begin
        Send CreateComObject of hoPlanJson
    End
    Set ComEmitCompact Of hoPlanJson To False

    Showln "Execution plan before first call:"
    Get pvComObject of hoPlanJson to vPlanJson
    Get ComExaminePlan Of hoHttpCurl sTargetCurl vPlanJson To iSuccess
    Get ComEmit Of hoPlanJson To sTemp1
    Showln sTemp1

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

    Get ComDoYourThing Of hoHttpCurl sTargetCurl To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttpCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "After first call:"
    Get ComGetVar Of hoHttpCurl "site_id" To sTemp1
    Showln "site_id  = " sTemp1
    Get ComGetVar Of hoHttpCurl "drive_id" To sTemp1
    Showln "drive_id = " sTemp1

    // -----------------------------------------------------------------------------
    // Second execution plan:
    // site_id and drive_id are now known, so only the target curl command is needed.
    // -----------------------------------------------------------------------------
    Showln "Execution plan after first call:"
    Get pvComObject of hoPlanJson to vPlanJson
    Get ComExaminePlan Of hoHttpCurl sTargetCurl vPlanJson To iSuccess
    Get ComEmit Of hoPlanJson To sTemp1
    Showln sTemp1

    // 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...
    Send ComSetVar To hoHttpCurl "site_name" "anotherSite"

    Showln "After changing site_name:"
    Get ComVarDefined Of hoHttpCurl "site_id" To bTemp1
    Showln "site_id defined?  " bTemp1
    Get ComVarDefined Of hoHttpCurl "drive_id" To bTemp1
    Showln "drive_id defined? " bTemp1

    // -----------------------------------------------------------------------------
    // The execution plan is automatically rebuilt.
    // Since site_id and drive_id were invalidated, the full dependency chain is needed again.
    // -----------------------------------------------------------------------------
    Showln "Execution plan after changing site_name:"
    Get pvComObject of hoPlanJson to vPlanJson
    Get ComExaminePlan Of hoHttpCurl sTargetCurl vPlanJson To iSuccess
    Get ComEmit Of hoPlanJson To sTemp1
    Showln sTemp1

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

    Get ComDoYourThing Of hoHttpCurl sTargetCurl To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttpCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "After running again with the new site_name:"
    Get ComGetVar Of hoHttpCurl "site_id" To sTemp1
    Showln "site_id  = " sTemp1
    Get ComGetVar Of hoHttpCurl "drive_id" To sTemp1
    Showln "drive_id = " sTemp1

    // -----------------------------------------------------------------------------
    // Clearing a variable also invalidates values that depend on it.
    // Here, clearing site_id also invalidates drive_id.
    // -----------------------------------------------------------------------------
    Send ComClearVar To hoHttpCurl "site_id"

    Showln "After clearing site_id:"
    Get ComVarDefined Of hoHttpCurl "site_id" To bTemp1
    Showln "site_id defined?  " bTemp1
    Get ComVarDefined Of hoHttpCurl "drive_id" To bTemp1
    Showln "drive_id defined? " bTemp1

    Showln "Execution plan after clearing site_id:"
    Get pvComObject of hoPlanJson to vPlanJson
    Get ComExaminePlan Of hoHttpCurl sTargetCurl vPlanJson To iSuccess
    Get ComEmit Of hoPlanJson To sTemp1
    Showln sTemp1

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


End_Procedure