PureBasic Requires Chilkat v11.7.0+
PureBasic
Box: Resolve a File Version by Name and Verify the Download
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. One target request requires file_id and file_version_id. The engine uses AddOutput2 for name lookups and AddOutput for version/SHA-1 metadata, then downloads that version to local_path and verifies its digest.
See the Box API guide for setup and the complete workflow.
Chilkat PureBasic Downloads
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttpCurl.pb"
IncludeFile "CkCrypt2.pb"
Procedure ChilkatExample()
success.i = 0
; Requires Chilkat 11.7.0 or later and a previously unlocked Chilkat bundle.
; See Global Unlock Sample.
; Guide: Box API Guide.
success = 0
; 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.
curl.i = CkHttpCurl::ckCreate()
If curl.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttpCurl::ckSetVar(curl,"access_token","BOX_ACCESS_TOKEN")
CkHttpCurl::ckSetVar(curl,"parent_name","/")
; Box's fixed root ID is an implementation detail; callers supply names.
CkHttpCurl::ckSetVar(curl,"parent_id","0")
setupCommand.i = CkStringBuilder::ckCreate()
If setupCommand.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
setupReplacements.i
; Register findParent; registration sends no request.
CkStringBuilder::ckSetString(setupCommand,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer ACCESS_TOKEN" + Chr(34))
setupReplacements = CkStringBuilder::ckReplace(setupCommand,"ACCESS_TOKEN",CkHttpCurl::ckGetVar(curl,"access_token"))
success = CkHttpCurl::ckAddFunction(curl,"findParent",CkStringBuilder::ckGetAsString(setupCommand))
If success <> 1
Debug CkHttpCurl::ckLastErrorText(curl)
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(setupCommand)
ProcedureReturn
EndIf
; Register findFolder; registration sends no request.
CkStringBuilder::ckSetString(setupCommand,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/folders/{{parent_id}}/items?limit=1000&fields=id,type,name" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer ACCESS_TOKEN" + Chr(34))
setupReplacements = CkStringBuilder::ckReplace(setupCommand,"ACCESS_TOKEN",CkHttpCurl::ckGetVar(curl,"access_token"))
success = CkHttpCurl::ckAddFunction(curl,"findFolder",CkStringBuilder::ckGetAsString(setupCommand))
If success <> 1
Debug CkHttpCurl::ckLastErrorText(curl)
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(setupCommand)
ProcedureReturn
EndIf
; Register findFile; registration sends no request.
CkStringBuilder::ckSetString(setupCommand,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/folders/{{folder_id}}/items?limit=1000&fields=id,type,name" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer ACCESS_TOKEN" + Chr(34))
setupReplacements = CkStringBuilder::ckReplace(setupCommand,"ACCESS_TOKEN",CkHttpCurl::ckGetVar(curl,"access_token"))
success = CkHttpCurl::ckAddFunction(curl,"findFile",CkStringBuilder::ckGetAsString(setupCommand))
If success <> 1
Debug CkHttpCurl::ckLastErrorText(curl)
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(setupCommand)
ProcedureReturn
EndIf
; Register findDestination; registration sends no request.
CkStringBuilder::ckSetString(setupCommand,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer ACCESS_TOKEN" + Chr(34))
setupReplacements = CkStringBuilder::ckReplace(setupCommand,"ACCESS_TOKEN",CkHttpCurl::ckGetVar(curl,"access_token"))
success = CkHttpCurl::ckAddFunction(curl,"findDestination",CkStringBuilder::ckGetAsString(setupCommand))
If success <> 1
Debug CkHttpCurl::ckLastErrorText(curl)
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(setupCommand)
ProcedureReturn
EndIf
; Register getFileDetails; registration sends no request.
CkStringBuilder::ckSetString(setupCommand,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/files/{{file_id}}?fields=id,sha1,file_version" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer ACCESS_TOKEN" + Chr(34))
setupReplacements = CkStringBuilder::ckReplace(setupCommand,"ACCESS_TOKEN",CkHttpCurl::ckGetVar(curl,"access_token"))
success = CkHttpCurl::ckAddFunction(curl,"getFileDetails",CkStringBuilder::ckGetAsString(setupCommand))
If success <> 1
Debug CkHttpCurl::ckLastErrorText(curl)
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(setupCommand)
ProcedureReturn
EndIf
; Register getEnterprise; registration sends no request.
CkStringBuilder::ckSetString(setupCommand,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/users/me?fields=enterprise" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer ACCESS_TOKEN" + Chr(34))
setupReplacements = CkStringBuilder::ckReplace(setupCommand,"ACCESS_TOKEN",CkHttpCurl::ckGetVar(curl,"access_token"))
success = CkHttpCurl::ckAddFunction(curl,"getEnterprise",CkStringBuilder::ckGetAsString(setupCommand))
If success <> 1
Debug CkHttpCurl::ckLastErrorText(curl)
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(setupCommand)
ProcedureReturn
EndIf
; Register output rules once. Chilkat 11.7.0 evaluates {{name}} variables
; when extracting each output, using the current value at that time.
success = CkHttpCurl::ckAddOutput2(curl,"findParent","entries","name","{{parent_name}}",1,"id","parent_id")
If success <> 1
Debug CkHttpCurl::ckLastErrorText(curl)
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(setupCommand)
ProcedureReturn
EndIf
success = CkHttpCurl::ckAddOutput2(curl,"findFolder","entries","name","{{folder_name}}",1,"id","folder_id")
If success <> 1
Debug CkHttpCurl::ckLastErrorText(curl)
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(setupCommand)
ProcedureReturn
EndIf
success = CkHttpCurl::ckAddOutput2(curl,"findFile","entries","name","{{file_name}}",1,"id","file_id")
If success <> 1
Debug CkHttpCurl::ckLastErrorText(curl)
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(setupCommand)
ProcedureReturn
EndIf
success = CkHttpCurl::ckAddOutput2(curl,"findDestination","entries","name","{{destination_name}}",1,"id","destination_id")
If success <> 1
Debug CkHttpCurl::ckLastErrorText(curl)
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(setupCommand)
ProcedureReturn
EndIf
success = CkHttpCurl::ckAddOutput(curl,"getFileDetails","file_version.id","file_version_id")
If success <> 1
Debug CkHttpCurl::ckLastErrorText(curl)
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(setupCommand)
ProcedureReturn
EndIf
success = CkHttpCurl::ckAddOutput(curl,"getFileDetails","sha1","file_sha1")
If success <> 1
Debug CkHttpCurl::ckLastErrorText(curl)
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(setupCommand)
ProcedureReturn
EndIf
success = CkHttpCurl::ckAddOutput(curl,"getEnterprise","enterprise.id","enterprise_id")
If success <> 1
Debug CkHttpCurl::ckLastErrorText(curl)
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(setupCommand)
ProcedureReturn
EndIf
; 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.
parentName.s = "/"
folderName.s = "images"
fileName.s = "penguins.jpg"
If (CkHttpCurl::ckGetVar(curl,"parent_name") <> parentName) OR ((parentName = "/") AND (CkHttpCurl::ckVarDefined(curl,"parent_id") = 0))
CkHttpCurl::ckSetVar(curl,"parent_name",parentName)
CkHttpCurl::ckClearVar(curl,"parent_id")
CkHttpCurl::ckClearVar(curl,"folder_id")
CkHttpCurl::ckClearVar(curl,"file_id")
CkHttpCurl::ckClearVar(curl,"file_version_id")
CkHttpCurl::ckClearVar(curl,"file_sha1")
If parentName = "/"
CkHttpCurl::ckSetVar(curl,"parent_id","0")
EndIf
EndIf
If CkHttpCurl::ckGetVar(curl,"folder_name") <> folderName
CkHttpCurl::ckSetVar(curl,"folder_name",folderName)
CkHttpCurl::ckClearVar(curl,"folder_id")
CkHttpCurl::ckClearVar(curl,"file_id")
CkHttpCurl::ckClearVar(curl,"file_version_id")
CkHttpCurl::ckClearVar(curl,"file_sha1")
EndIf
If CkHttpCurl::ckGetVar(curl,"file_name") <> fileName
CkHttpCurl::ckSetVar(curl,"file_name",fileName)
CkHttpCurl::ckClearVar(curl,"file_id")
CkHttpCurl::ckClearVar(curl,"file_version_id")
CkHttpCurl::ckClearVar(curl,"file_sha1")
EndIf
CkHttpCurl::ckSetVar(curl,"local_path","qa_output/penguins-verified.jpg")
; file_version_id is also unknown, so getFileDetails becomes a prerequisite.
; Its AddOutput mappings cache both the selected version and its SHA-1.
CkHttpCurl::ckClearTargetOutput(curl,"*")
authorizedCommand1.i = CkStringBuilder::ckCreate()
If authorizedCommand1.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckSetString(authorizedCommand1,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/files/{{file_id}}/content?version={{file_version_id}}" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer {{access_token}}" + Chr(34) + " -L -o " + Chr(34) + "{{local_path}}" + Chr(34))
headerReplacement1.i = CkStringBuilder::ckReplace(authorizedCommand1,"{{access_token}}",CkHttpCurl::ckGetVar(curl,"access_token"))
success = CkHttpCurl::ckDoYourThing(curl,CkStringBuilder::ckGetAsString(authorizedCommand1))
If (success <> 1) OR (CkHttpCurl::ckStatusCode(curl) <> 200)
Debug "Box request failed. HTTP status: " + Str(CkHttpCurl::ckStatusCode(curl))
Debug "Failure reason: " + Str(CkHttpCurl::ckFailReason(curl))
Debug CkHttpCurl::ckResponseBodyStr(curl)
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(setupCommand)
CkStringBuilder::ckDispose(authorizedCommand1)
ProcedureReturn
EndIf
digest.i = CkCrypt2::ckCreate()
If digest.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkCrypt2::setCkHashAlgorithm(digest, "sha1")
CkCrypt2::setCkEncodingMode(digest, "hex_lower")
actualSha1.s = CkCrypt2::ckHashFileENC(digest,CkHttpCurl::ckGetVar(curl,"local_path"))
If CkCrypt2::ckLastMethodSuccess(digest) <> 1
Debug CkCrypt2::ckLastErrorText(digest)
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(setupCommand)
CkStringBuilder::ckDispose(authorizedCommand1)
CkCrypt2::ckDispose(digest)
ProcedureReturn
EndIf
If actualSha1 <> CkHttpCurl::ckGetVar(curl,"file_sha1")
Debug "Downloaded content did not match the selected version's SHA-1."
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(setupCommand)
CkStringBuilder::ckDispose(authorizedCommand1)
CkCrypt2::ckDispose(digest)
ProcedureReturn
EndIf
Debug "Version-specific download verified."
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(setupCommand)
CkStringBuilder::ckDispose(authorizedCommand1)
CkCrypt2::ckDispose(digest)
ProcedureReturn
EndProcedure