PureBasic Requires Chilkat v11.7.0+
PureBasic
Box: Apply, Read, and Update File Metadata
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. Requires an enterprise metadata template with key projectDocument and string fields customerId and status. An administrator must create the template first. This example creates a new instance; if it already exists, begin with GET/PUT instead. The enterprise scope ID is resolved from the current user profile; enterprise membership and the pre-existing template are required.
See the Box API guide for setup and the complete workflow.
Chilkat PureBasic Downloads
IncludeFile "CkHttpCurl.pb"
IncludeFile "CkStringBuilder.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 = "documents"
fileName.s = "invoice.pdf"
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::ckClearTargetOutput(curl,"*")
authorizedCommand1.i = CkStringBuilder::ckCreate()
If authorizedCommand1.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckSetString(authorizedCommand1,"curl -X POST " + Chr(34) + "https://api.box.com/2.0/files/{{file_id}}/metadata/enterprise_{{enterprise_id}}/projectDocument" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer {{access_token}}" + Chr(34) + " -H " + Chr(34) + "Content-Type: application/json" + Chr(34) + " -d '{" + Chr(34) + "customerId" + Chr(34) + ":" + Chr(34) + "CUST-1001" + Chr(34) + "," + Chr(34) + "status" + Chr(34) + ":" + Chr(34) + "received" + 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) <> 201)
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
CkHttpCurl::ckClearTargetOutput(curl,"*")
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}}/metadata/enterprise_{{enterprise_id}}/projectDocument" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer {{access_token}}" + Chr(34))
headerReplacement2.i = CkStringBuilder::ckReplace(authorizedCommand2,"{{access_token}}",CkHttpCurl::ckGetVar(curl,"access_token"))
success = CkHttpCurl::ckDoYourThing(curl,CkStringBuilder::ckGetAsString(authorizedCommand2))
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)
CkStringBuilder::ckDispose(authorizedCommand2)
ProcedureReturn
EndIf
Debug CkHttpCurl::ckResponseBodyStr(curl)
CkHttpCurl::ckClearTargetOutput(curl,"*")
authorizedCommand3.i = CkStringBuilder::ckCreate()
If authorizedCommand3.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckSetString(authorizedCommand3,"curl -X PUT " + Chr(34) + "https://api.box.com/2.0/files/{{file_id}}/metadata/enterprise_{{enterprise_id}}/projectDocument" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer {{access_token}}" + Chr(34) + " -H " + Chr(34) + "Content-Type: application/json-patch+json" + Chr(34) + " -d '[{" + Chr(34) + "op" + Chr(34) + ":" + Chr(34) + "replace" + Chr(34) + "," + Chr(34) + "path" + Chr(34) + ":" + Chr(34) + "/status" + Chr(34) + "," + Chr(34) + "value" + Chr(34) + ":" + Chr(34) + "approved" + 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)
CkStringBuilder::ckDispose(authorizedCommand2)
CkStringBuilder::ckDispose(authorizedCommand3)
ProcedureReturn
EndIf
Debug "Updated metadata: " + CkHttpCurl::ckResponseBodyStr(curl)
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(setupCommand)
CkStringBuilder::ckDispose(authorizedCommand1)
CkStringBuilder::ckDispose(authorizedCommand2)
CkStringBuilder::ckDispose(authorizedCommand3)
ProcedureReturn
EndProcedure