Swift Requires Chilkat v11.7.0+
Swift
Box: Find a File by Name Across All Folder Pages
See more Box Examples
Run the reusable setup once per HttpCurl object, then select names and execute the operation. The setup is repeated here only to make this published example independently runnable. Find one exact, case-sensitive filename in a parent folder selected by name, checking the item type and every page before using its ID.
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
// This block is repeated so each published example can run on its own.
// In an application, run the setup example once and pass the SAME curl object
// to operation code below. Do not recreate it for every operation.
// See Reusable Box HttpCurl Setup.
let curl = CkoHttpCurl()!
curl.setVar(varName: "access_token", varValue: "BOX_ACCESS_TOKEN")
curl.setVar(varName: "parent_name", varValue: "/")
// Box's fixed root ID is an implementation detail; callers supply names.
curl.setVar(varName: "parent_id", varValue: "0")
let setupCommand = CkoStringBuilder()!
var setupReplacements: Int
// Register findParent; registration sends no request.
setupCommand.setString(value: "curl -X GET \"https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"")
setupReplacements = setupCommand.replace(value: "ACCESS_TOKEN", replacement: curl.getVar(varName: "access_token")).intValue
success = curl.addFunction(funcName: "findParent", curl: setupCommand.getAsString())
if success != true {
print("\(curl.lastErrorText!)")
return
}
// Register findFolder; registration sends no request.
setupCommand.setString(value: "curl -X GET \"https://api.box.com/2.0/folders/{{parent_id}}/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"")
setupReplacements = setupCommand.replace(value: "ACCESS_TOKEN", replacement: curl.getVar(varName: "access_token")).intValue
success = curl.addFunction(funcName: "findFolder", curl: setupCommand.getAsString())
if success != true {
print("\(curl.lastErrorText!)")
return
}
// Register findFile; registration sends no request.
setupCommand.setString(value: "curl -X GET \"https://api.box.com/2.0/folders/{{folder_id}}/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"")
setupReplacements = setupCommand.replace(value: "ACCESS_TOKEN", replacement: curl.getVar(varName: "access_token")).intValue
success = curl.addFunction(funcName: "findFile", curl: setupCommand.getAsString())
if success != true {
print("\(curl.lastErrorText!)")
return
}
// Register findDestination; registration sends no request.
setupCommand.setString(value: "curl -X GET \"https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"")
setupReplacements = setupCommand.replace(value: "ACCESS_TOKEN", replacement: curl.getVar(varName: "access_token")).intValue
success = curl.addFunction(funcName: "findDestination", curl: setupCommand.getAsString())
if success != true {
print("\(curl.lastErrorText!)")
return
}
// Register getFileDetails; registration sends no request.
setupCommand.setString(value: "curl -X GET \"https://api.box.com/2.0/files/{{file_id}}?fields=id,sha1,file_version\" -H \"Authorization: Bearer ACCESS_TOKEN\"")
setupReplacements = setupCommand.replace(value: "ACCESS_TOKEN", replacement: curl.getVar(varName: "access_token")).intValue
success = curl.addFunction(funcName: "getFileDetails", curl: setupCommand.getAsString())
if success != true {
print("\(curl.lastErrorText!)")
return
}
// Register getEnterprise; registration sends no request.
setupCommand.setString(value: "curl -X GET \"https://api.box.com/2.0/users/me?fields=enterprise\" -H \"Authorization: Bearer ACCESS_TOKEN\"")
setupReplacements = setupCommand.replace(value: "ACCESS_TOKEN", replacement: curl.getVar(varName: "access_token")).intValue
success = curl.addFunction(funcName: "getEnterprise", curl: setupCommand.getAsString())
if success != true {
print("\(curl.lastErrorText!)")
return
}
// Register output rules once. Chilkat 11.7.0 evaluates {{name}} variables
// when extracting each output, using the current value at that time.
success = curl.addOutput2(funcName: "findParent", arrayPath: "entries", wherePath: "name", whereValue: "{{parent_name}}", caseSensitive: true, subPath: "id", varName: "parent_id")
if success != true {
print("\(curl.lastErrorText!)")
return
}
success = curl.addOutput2(funcName: "findFolder", 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: "findFile", 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: "findDestination", arrayPath: "entries", wherePath: "name", whereValue: "{{destination_name}}", caseSensitive: true, subPath: "id", varName: "destination_id")
if success != true {
print("\(curl.lastErrorText!)")
return
}
success = curl.addOutput(funcName: "getFileDetails", jsonPath: "file_version.id", varName: "file_version_id")
if success != true {
print("\(curl.lastErrorText!)")
return
}
success = curl.addOutput(funcName: "getFileDetails", jsonPath: "sha1", varName: "file_sha1")
if success != true {
print("\(curl.lastErrorText!)")
return
}
success = curl.addOutput(funcName: "getEnterprise", jsonPath: "enterprise.id", varName: "enterprise_id")
if success != true {
print("\(curl.lastErrorText!)")
return
}
// END ONE-TIME SETUP
// The name lookups below assume each listing fits in one page (1000 items).
// Parent and destination names select root children; folder_name selects a child
// of parent_name. "/" selects the root parent. These are not arbitrary paths.
// BEGIN OPERATION -- keep the configured object when reusing it.
// Inputs are names. Retain cached IDs when the selected names have not changed.
var parentName: String? = "/"
var folderName: String? = "images"
if (curl.getVar(varName: "parent_name") != parentName) || ((parentName == "/") && (curl.varDefined(varName: "parent_id") == false)) {
curl.setVar(varName: "parent_name", varValue: parentName)
curl.clearVar(varName: "parent_id")
curl.clearVar(varName: "folder_id")
curl.clearVar(varName: "file_id")
curl.clearVar(varName: "file_version_id")
curl.clearVar(varName: "file_sha1")
if parentName == "/" {
curl.setVar(varName: "parent_id", varValue: "0")
}
}
if curl.getVar(varName: "folder_name") != folderName {
curl.setVar(varName: "folder_name", varValue: folderName)
curl.clearVar(varName: "folder_id")
curl.clearVar(varName: "file_id")
curl.clearVar(varName: "file_version_id")
curl.clearVar(varName: "file_sha1")
}
curl.setVar(varName: "marker", varValue: "")
let page = CkoJsonObject()!
var i: Int = 0
var count: Int = 0
var more: Bool = true
var nextMarker: String?
let authorizedCommand1 = CkoStringBuilder()!
authorizedCommand1.setString(value: "curl -X GET \"https://api.box.com/2.0/folders/{{folder_id}}/items?usemarker=true&limit=1000&fields=id,type,name\" -H \"Authorization: Bearer {{access_token}}\"")
var headerReplacement1: Int = authorizedCommand1.replace(value: "{{access_token}}", replacement: curl.getVar(varName: "access_token")).intValue
var pageCommand: String? = authorizedCommand1.getAsString()
var wantedName: String? = "penguins.jpg"
var fileId: String? = ""
var matches: Int = 0
while (more == true) {
curl.clearTargetOutput(varName: "*")
success = curl.doYourThing(targetCurl: pageCommand)
if (success != true) || (curl.statusCode.intValue != 200) {
print("Box request failed. HTTP status: \(curl.statusCode.intValue)")
print("Failure reason: \(curl.failReason.intValue)")
print("\(curl.responseBodyStr!)")
return
}
success = curl.getResponseJson(json: page)
if success != true {
print("\(curl.lastErrorText!)")
return
}
count = page.size(ofArray: "entries").intValue
i = 0
while i < count {
page.i = i
if (page.string(of: "entries[i].type") == "file") && (page.string(of: "entries[i].name") == wantedName) {
fileId = page.string(of: "entries[i].id")
matches = matches + 1
}
i = i + 1
}
more = false
if page.hasMember(jsonPath: "next_marker") == true {
if page.isNull(of: "next_marker") == false {
nextMarker = page.string(of: "next_marker")
if nextMarker != "" {
if nextMarker == curl.getVar(varName: "marker") {
print("Box returned the same pagination marker; stopping.")
return
}
curl.setVar(varName: "marker", varValue: nextMarker)
let authorizedCommand2 = CkoStringBuilder()!
authorizedCommand2.setString(value: "curl -X GET \"https://api.box.com/2.0/folders/{{folder_id}}/items?usemarker=true&limit=1000&fields=id,type,name&marker={{marker}}\" -H \"Authorization: Bearer {{access_token}}\"")
var headerReplacement2: Int = authorizedCommand2.replace(value: "{{access_token}}", replacement: curl.getVar(varName: "access_token")).intValue
pageCommand = authorizedCommand2.getAsString()
more = true
}
}
}
}
if matches != 1 {
print("Expected exactly one matching file. Matches: \(matches)")
return
}
curl.setVar(varName: "file_id", varValue: fileId)
print("File ID: \(fileId!)")
curl.clearTargetOutput(varName: "*")
let authorizedCommand3 = CkoStringBuilder()!
authorizedCommand3.setString(value: "curl -X GET \"https://api.box.com/2.0/files/{{file_id}}?fields=id,type,name,parent,size\" -H \"Authorization: Bearer {{access_token}}\"")
var headerReplacement3: Int = authorizedCommand3.replace(value: "{{access_token}}", replacement: curl.getVar(varName: "access_token")).intValue
success = curl.doYourThing(targetCurl: authorizedCommand3.getAsString())
if (success != true) || (curl.statusCode.intValue != 200) {
print("Box request failed. HTTP status: \(curl.statusCode.intValue)")
print("Failure reason: \(curl.failReason.intValue)")
print("\(curl.responseBodyStr!)")
return
}
print("\(curl.responseBodyStr!)")
}