Swift Requires Chilkat v11.7.0+
Swift
Box: Set Up Reusable HttpCurl Requests and Outputs
See more Box Examples
Register searchFolder and searchFile once, with deferred folder_name and file_name output rules. This review example resolves a folder directly under root and a file directly inside it. The download operation requires exactly one search result at each step and validates exact names and parent IDs. Registration and ExaminePlan send no requests. Other Box examples still have their existing standalone setups.
See the Box API guide for setup and the complete workflow.
Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// Requires Chilkat 11.7.0 or later and a previously unlocked Chilkat bundle.
// See Global Unlock Sample.
// Guide: Box API Guide.
success = false
// BEGIN ONE-TIME SETUP
// Requires Chilkat 11.7.0. Registration sends no requests.
// Run this once and keep the SAME HttpCurl object for later downloads.
// This focused setup searches for a root folder and a file directly inside it.
let curl = CkoHttpCurl()!
curl.setVar(varName: "access_token", varValue: "BOX_ACCESS_TOKEN")
let setupCommand = CkoStringBuilder()!
var setupReplacements: Int
// searchFolder: Box searches names; quotes request a phrase match.
setupCommand.setString(value: "curl -X GET \"https://api.box.com/2.0/search?query={{folder_query}}&type=folder&content_types=name&fields=id,type,name,parent&limit=2\" -H \"Authorization: Bearer ACCESS_TOKEN\"")
setupReplacements = setupCommand.replace(value: "ACCESS_TOKEN", replacement: curl.getVar(varName: "access_token")).intValue
success = curl.addFunction(funcName: "searchFolder", curl: setupCommand.getAsString())
if success != true {
print("\(curl.lastErrorText!)")
return
}
// Deferred selection: folder_name need not be set during setup.
success = curl.addOutput2(funcName: "searchFolder", arrayPath: "entries", wherePath: "name", whereValue: "{{folder_name}}", caseSensitive: true, subPath: "id", varName: "folder_id")
if success != true {
print("\(curl.lastErrorText!)")
return
}
success = curl.addOutput2(funcName: "searchFolder", arrayPath: "entries", wherePath: "name", whereValue: "{{folder_name}}", caseSensitive: true, subPath: "name", varName: "folder_result_name")
if success != true {
print("\(curl.lastErrorText!)")
return
}
success = curl.addOutput2(funcName: "searchFolder", arrayPath: "entries", wherePath: "name", whereValue: "{{folder_name}}", caseSensitive: true, subPath: "parent.id", varName: "folder_parent_id")
if success != true {
print("\(curl.lastErrorText!)")
return
}
success = curl.addOutput(funcName: "searchFolder", jsonPath: "total_count", varName: "folder_search_count")
if success != true {
print("\(curl.lastErrorText!)")
return
}
// searchFile: Box searches names; quotes request a phrase match.
setupCommand.setString(value: "curl -X GET \"https://api.box.com/2.0/search?query={{file_query}}&type=file&content_types=name&fields=id,type,name,parent&limit=2&ancestor_folder_ids={{folder_id}}\" -H \"Authorization: Bearer ACCESS_TOKEN\"")
setupReplacements = setupCommand.replace(value: "ACCESS_TOKEN", replacement: curl.getVar(varName: "access_token")).intValue
success = curl.addFunction(funcName: "searchFile", curl: setupCommand.getAsString())
if success != true {
print("\(curl.lastErrorText!)")
return
}
// Deferred selection: file_name need not be set during setup.
success = curl.addOutput2(funcName: "searchFile", arrayPath: "entries", wherePath: "name", whereValue: "{{file_name}}", caseSensitive: true, subPath: "id", varName: "file_id")
if success != true {
print("\(curl.lastErrorText!)")
return
}
success = curl.addOutput2(funcName: "searchFile", arrayPath: "entries", wherePath: "name", whereValue: "{{file_name}}", caseSensitive: true, subPath: "name", varName: "file_result_name")
if success != true {
print("\(curl.lastErrorText!)")
return
}
success = curl.addOutput2(funcName: "searchFile", arrayPath: "entries", wherePath: "name", whereValue: "{{file_name}}", caseSensitive: true, subPath: "parent.id", varName: "file_parent_id")
if success != true {
print("\(curl.lastErrorText!)")
return
}
success = curl.addOutput(funcName: "searchFile", jsonPath: "total_count", varName: "file_search_count")
if success != true {
print("\(curl.lastErrorText!)")
return
}
// END ONE-TIME SETUP
// Search is indexed: newly uploaded/renamed items may not appear immediately.
// Search matches phrases, not exact strings, and ancestor filtering includes
// subfolders. The operation validates counts, exact names, and immediate parents.
// limit=2 is enough to demonstrate an ambiguous search; total_count covers all
// results, not just this page. This introductory example requires exactly one
// search result at each step and does not select among multiple candidates.
// BEGIN OPERATION -- use the previously configured object.
// Human-readable inputs. No resource IDs are supplied by the caller.
var folderName: String? = "images"
var fileName: String? = "penguins.jpg"
// Encode the quoted search queries separately from the literal output filters.
// In this build, URL variable substitution does not encode '&' in a name.
let queryText = CkoStringBuilder()!
queryText.setString(value: "\"")
queryText.append(value: folderName)
queryText.append(value: "\"")
curl.setVar(varName: "folder_query", varValue: queryText.getEncoded(encoding: "url", charset: "utf-8"))
queryText.setString(value: "\"")
queryText.append(value: fileName)
queryText.append(value: "\"")
curl.setVar(varName: "file_query", varValue: queryText.getEncoded(encoding: "url", charset: "utf-8"))
if curl.getVar(varName: "folder_name") != folderName {
curl.clearVar(varName: "folder_id")
curl.clearVar(varName: "folder_result_name")
curl.clearVar(varName: "folder_parent_id")
curl.clearVar(varName: "folder_search_count")
curl.clearVar(varName: "file_id")
curl.clearVar(varName: "file_result_name")
curl.clearVar(varName: "file_parent_id")
curl.clearVar(varName: "file_search_count")
curl.setVar(varName: "folder_name", varValue: folderName)
}
if curl.getVar(varName: "file_name") != fileName {
curl.clearVar(varName: "file_id")
curl.clearVar(varName: "file_result_name")
curl.clearVar(varName: "file_parent_id")
curl.clearVar(varName: "file_search_count")
curl.setVar(varName: "file_name", varValue: fileName)
}
// With unchanged names, cached IDs and validation fields can be reused.
// Clear the corresponding cached fields to refresh after a remote rename/move.
// Examine the plan without running any HTTP requests.
let setupPlan = CkoJsonObject()!
setupPlan.emitCompact = false
let authorizedCommand1 = CkoStringBuilder()!
authorizedCommand1.setString(value: "curl -X GET \"https://api.box.com/2.0/files/{{file_id}}/content\" -H \"Authorization: Bearer {{access_token}}\" -L")
var headerReplacement1: Int = authorizedCommand1.replace(value: "{{access_token}}", replacement: curl.getVar(varName: "access_token")).intValue
success = curl.examinePlan(curl: authorizedCommand1.getAsString(), json: setupPlan)
if success != true {
print("\(curl.lastErrorText!)")
return
}
print("\(setupPlan.emit()!)")
print("Keep this HttpCurl object for the search-based download operation.")
}