![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Swift) Invalidating Dependent Variables When Inputs ChangeSee more CURL ExamplesThis example demonstrates how After a multi-step execution plan runs successfully, variables such as However, if an input variable is cleared using For example, if Note: This example requires Chilkat v11.5.0 or greater. For more information, see https://www.chilkatsoft.com/curl_dependency_engine.asp
func chilkatTest() { var success: Bool = false let httpCurl = CkoHttpCurl()! // The target curl command we ultimately want to execute. // It requires drive_id. var targetCurl: String? = "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: String? = "getDrives" httpCurl.addFunction(fnName, curl: "curl -X GET https://graph.microsoft.com/v1.0/sites/{{site_id}}/drives") httpCurl.addOutput(fnName, jsonPath: "value[0].id", varName: "drive_id") // Define another helper function that produces site_id. // This requires site_name. fnName = "getSite" httpCurl.addFunction(fnName, curl: "curl -X GET https://graph.microsoft.com/v1.0/sites/root:/sites/{{site_name}}") httpCurl.addOutput(fnName, jsonPath: "id", varName: "site_id") // site_name is the starting known value. httpCurl.setVar("site_name", varValue: "test") // Configure OAuth2 authentication. let jsonOAuth2 = CkoJsonObject()! jsonOAuth2.enableSecrets = true jsonOAuth2.update("oauth2.client_id", value: "!!sharepoint|oauth2|client_id") jsonOAuth2.update("oauth2.client_secret", value: "!!sharepoint|oauth2|client_secret") jsonOAuth2.update("oauth2.scope", value: "https://graph.microsoft.com/.default") jsonOAuth2.update("oauth2.token_endpoint", value: "!!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. // ----------------------------------------------------------------------------- print("Execution plan before first call:") print("\(httpCurl.examinePlan(targetCurl)!)") // Expected: // // Plan for targetCurl(drive_id) // 1) site_id = getSite(site_name) // 2) drive_id = getDrives(site_id) // 3) targetCurl(drive_id) success = httpCurl.doYourThing(targetCurl) if success == false { print("\(httpCurl.lastErrorText!)") return } print("After first call:") print("site_id = \(httpCurl.getVar("site_id")!)") print("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. // ----------------------------------------------------------------------------- print("Execution plan after first call:") print("\(httpCurl.examinePlan(targetCurl)!)") // Expected: // // Plan for targetCurl(drive_id) // 1) targetCurl(drive_id) // ----------------------------------------------------------------------------- // 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. // ----------------------------------------------------------------------------- httpCurl.setVar("site_name", varValue: "anotherSite") print("After changing site_name:") print("site_id defined? \(httpCurl.varDefined("site_id"))") print("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. // ----------------------------------------------------------------------------- print("Execution plan after changing site_name:") print("\(httpCurl.examinePlan(targetCurl)!)") // Expected: // // Plan for targetCurl(drive_id) // 1) site_id = getSite(site_name) // 2) drive_id = getDrives(site_id) // 3) targetCurl(drive_id) success = httpCurl.doYourThing(targetCurl) if success == false { print("\(httpCurl.lastErrorText!)") return } print("After running again with the new site_name:") print("site_id = \(httpCurl.getVar("site_id")!)") print("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") print("After clearing site_id:") print("site_id defined? \(httpCurl.varDefined("site_id"))") print("drive_id defined? \(httpCurl.varDefined("drive_id"))") print("Execution plan after clearing site_id:") print("\(httpCurl.examinePlan(targetCurl)!)") // Expected: // // Plan for targetCurl(drive_id) // 1) site_id = getSite(site_name) // 2) drive_id = getDrives(site_id) // 3) targetCurl(drive_id } |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.