PureBasic Requires Chilkat v11.7.0+
PureBasic
Box: Reuse HttpCurl and Invalidate Cached IDs
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. Perform three operations with one configured object: cold file lookup, warm download without lookups, then another download to a different local path after clearing only file state. Functions and output rules are registered only once; their variables are evaluated when used.
See the Box API guide for setup and the complete workflow.
Chilkat PureBasic Downloads
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkHttpCurl.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.jpg")
; Operation 1: get information by name. This resolves folder_id and file_id.
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}}?fields=id,name" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer {{access_token}}" + 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
; Operation 2: keep the same object and IDs. Only the final download is needed.
warmPlan.i = CkJsonObject::ckCreate()
If warmPlan.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::setCkEmitCompact(warmPlan, 0)
authorizedCommand2.i = CkStringBuilder::ckCreate()
If authorizedCommand2.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckSetString(authorizedCommand2,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/files/{{file_id}}/content" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer {{access_token}}" + Chr(34) + " -L -o " + Chr(34) + "{{local_path}}" + Chr(34))
headerReplacement2.i = CkStringBuilder::ckReplace(authorizedCommand2,"{{access_token}}",CkHttpCurl::ckGetVar(curl,"access_token"))
success = CkHttpCurl::ckExaminePlan(curl,CkStringBuilder::ckGetAsString(authorizedCommand2),warmPlan)
If success <> 1
Debug CkHttpCurl::ckLastErrorText(curl)
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(setupCommand)
CkStringBuilder::ckDispose(authorizedCommand1)
CkJsonObject::ckDispose(warmPlan)
CkStringBuilder::ckDispose(authorizedCommand2)
ProcedureReturn
EndIf
Debug CkJsonObject::ckEmit(warmPlan)
CkHttpCurl::ckClearTargetOutput(curl,"*")
authorizedCommand3.i = CkStringBuilder::ckCreate()
If authorizedCommand3.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckSetString(authorizedCommand3,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/files/{{file_id}}/content" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer {{access_token}}" + Chr(34) + " -L -o " + Chr(34) + "{{local_path}}" + Chr(34))
headerReplacement3.i = CkStringBuilder::ckReplace(authorizedCommand3,"{{access_token}}",CkHttpCurl::ckGetVar(curl,"access_token"))
success = CkHttpCurl::ckDoYourThing(curl,CkStringBuilder::ckGetAsString(authorizedCommand3))
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)
CkJsonObject::ckDispose(warmPlan)
CkStringBuilder::ckDispose(authorizedCommand2)
CkStringBuilder::ckDispose(authorizedCommand3)
ProcedureReturn
EndIf
; Operation 3: explicitly invalidate file state, retaining the known folder.
; Here we select the same name so this demonstration needs only one real file.
; Substitute another filename to switch files in this folder.
; The existing output rule reads the current file_name; do not register it again.
CkHttpCurl::ckClearVar(curl,"file_id")
CkHttpCurl::ckClearVar(curl,"file_version_id")
CkHttpCurl::ckClearVar(curl,"file_sha1")
CkHttpCurl::ckSetVar(curl,"file_name","penguins.jpg")
CkHttpCurl::ckSetVar(curl,"local_path","qa_output/penguins-copy.jpg")
authorizedCommand4.i = CkStringBuilder::ckCreate()
If authorizedCommand4.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckSetString(authorizedCommand4,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/files/{{file_id}}/content" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer {{access_token}}" + Chr(34) + " -L -o " + Chr(34) + "{{local_path}}" + Chr(34))
headerReplacement4.i = CkStringBuilder::ckReplace(authorizedCommand4,"{{access_token}}",CkHttpCurl::ckGetVar(curl,"access_token"))
success = CkHttpCurl::ckExaminePlan(curl,CkStringBuilder::ckGetAsString(authorizedCommand4),warmPlan)
If success <> 1
Debug CkHttpCurl::ckLastErrorText(curl)
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(setupCommand)
CkStringBuilder::ckDispose(authorizedCommand1)
CkJsonObject::ckDispose(warmPlan)
CkStringBuilder::ckDispose(authorizedCommand2)
CkStringBuilder::ckDispose(authorizedCommand3)
CkStringBuilder::ckDispose(authorizedCommand4)
ProcedureReturn
EndIf
Debug CkJsonObject::ckEmit(warmPlan)
CkHttpCurl::ckClearTargetOutput(curl,"*")
authorizedCommand5.i = CkStringBuilder::ckCreate()
If authorizedCommand5.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckSetString(authorizedCommand5,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/files/{{file_id}}/content" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer {{access_token}}" + Chr(34) + " -L -o " + Chr(34) + "{{local_path}}" + Chr(34))
headerReplacement5.i = CkStringBuilder::ckReplace(authorizedCommand5,"{{access_token}}",CkHttpCurl::ckGetVar(curl,"access_token"))
success = CkHttpCurl::ckDoYourThing(curl,CkStringBuilder::ckGetAsString(authorizedCommand5))
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)
CkJsonObject::ckDispose(warmPlan)
CkStringBuilder::ckDispose(authorizedCommand2)
CkStringBuilder::ckDispose(authorizedCommand3)
CkStringBuilder::ckDispose(authorizedCommand4)
CkStringBuilder::ckDispose(authorizedCommand5)
ProcedureReturn
EndIf
; Changing parent_name invalidates parent_id, folder_id, and file state.
; Changing folder_name invalidates folder_id and file state.
; Changing destination_name invalidates destination_id.
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(setupCommand)
CkStringBuilder::ckDispose(authorizedCommand1)
CkJsonObject::ckDispose(warmPlan)
CkStringBuilder::ckDispose(authorizedCommand2)
CkStringBuilder::ckDispose(authorizedCommand3)
CkStringBuilder::ckDispose(authorizedCommand4)
CkStringBuilder::ckDispose(authorizedCommand5)
ProcedureReturn
EndProcedure