Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loHttpCurl
LOCAL lcTargetCurl
LOCAL lcFnName
LOCAL loJsonOAuth2
LOCAL loPlanJson

lnSuccess = 0

loHttpCurl = CreateObject('Chilkat.HttpCurl')

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

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

* site_name is the starting known value.
loHttpCurl.SetVar("site_name","test")

* Configure OAuth2 authentication.
loJsonOAuth2 = CreateObject('Chilkat.JsonObject')
loJsonOAuth2.EnableSecrets = 1
loJsonOAuth2.UpdateString("oauth2.client_id","!!sharepoint|oauth2|client_id")
loJsonOAuth2.UpdateString("oauth2.client_secret","!!sharepoint|oauth2|client_secret")
loJsonOAuth2.UpdateString("oauth2.scope","https://graph.microsoft.com/.default")
loJsonOAuth2.UpdateString("oauth2.token_endpoint","!!sharepoint|oauth2|token_endpoint")
loHttpCurl.SetAuth(loJsonOAuth2)

* -----------------------------------------------------------------------------
* First execution plan:
* site_id and drive_id are not known yet, so the full dependency chain is needed.
* -----------------------------------------------------------------------------
loPlanJson = CreateObject('Chilkat.JsonObject')
loPlanJson.EmitCompact = 0

? "Execution plan before first call:"
lnSuccess = loHttpCurl.ExaminePlan(lcTargetCurl,loPlanJson)
? loPlanJson.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": []
*   }]
* }

lnSuccess = loHttpCurl.DoYourThing(lcTargetCurl)
IF (lnSuccess = 0) THEN
    ? loHttpCurl.LastErrorText
    RELEASE loHttpCurl
    RELEASE loJsonOAuth2
    RELEASE loPlanJson
    CANCEL
ENDIF

? "After first call:"
? "site_id  = " + loHttpCurl.GetVar("site_id")
? "drive_id = " + loHttpCurl.GetVar("drive_id")

* -----------------------------------------------------------------------------
* Second execution plan:
* site_id and drive_id are now known, so only the target curl command is needed.
* -----------------------------------------------------------------------------
? "Execution plan after first call:"
lnSuccess = loHttpCurl.ExaminePlan(lcTargetCurl,loPlanJson)
? loPlanJson.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...
loHttpCurl.SetVar("site_name","anotherSite")

? "After changing site_name:"
? "site_id defined?  " + STR(loHttpCurl.VarDefined("site_id"))
? "drive_id defined? " + STR(loHttpCurl.VarDefined("drive_id"))

* -----------------------------------------------------------------------------
* The execution plan is automatically rebuilt.
* Since site_id and drive_id were invalidated, the full dependency chain is needed again.
* -----------------------------------------------------------------------------
? "Execution plan after changing site_name:"
lnSuccess = loHttpCurl.ExaminePlan(lcTargetCurl,loPlanJson)
? loPlanJson.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": []
*   }]
* }

lnSuccess = loHttpCurl.DoYourThing(lcTargetCurl)
IF (lnSuccess = 0) THEN
    ? loHttpCurl.LastErrorText
    RELEASE loHttpCurl
    RELEASE loJsonOAuth2
    RELEASE loPlanJson
    CANCEL
ENDIF

? "After running again with the new site_name:"
? "site_id  = " + loHttpCurl.GetVar("site_id")
? "drive_id = " + loHttpCurl.GetVar("drive_id")

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

? "After clearing site_id:"
? "site_id defined?  " + STR(loHttpCurl.VarDefined("site_id"))
? "drive_id defined? " + STR(loHttpCurl.VarDefined("drive_id"))

? "Execution plan after clearing site_id:"
lnSuccess = loHttpCurl.ExaminePlan(lcTargetCurl,loPlanJson)
? loPlanJson.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": []
*   }]
* }

RELEASE loHttpCurl
RELEASE loJsonOAuth2
RELEASE loPlanJson