PureBasic Box: Copy Folder

Back to Index

Used to create a copy of a folder in another folder. The original version of the folder will not be altered. The a response status code of 202 is expected for success.

Documentation: https://developer.box.com/reference#copy-a-folder

CURL Command

curl https://api.box.com/2.0/folders/FOLDER_ID/copy \
-H "Authorization: Bearer BOX_ACCESS_TOKEN" \
-d '{"parent": {"id" : DESTINATION_FOLDER_ID}}' \
-X POST

PureBasic Example

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"

Procedure ChilkatExample()

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    ;  URL: https://api.box.com/2.0/folders/FOLDER_ID/copy
    bTls.i = 1
    port.i = 443
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"api.box.com",port,bTls,bAutoReconnect)
    If success <> 1
        Debug "ConnectFailReason: " + Str(CkRest::ckConnectFailReason(rest))
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateNumber(json,"parent.id","DESTINATION_FOLDER_ID")

    CkRest::ckAddHeader(rest,"Authorization","Bearer BOX_ACCESS_TOKEN")

    sbRequestBody.i = CkStringBuilder::ckCreate()
    If sbRequestBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckEmitSb(json,sbRequestBody)
    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkRest::ckFullRequestSb(rest,"POST","/2.0/folders/FOLDER_ID/copy",sbRequestBody,sbResponseBody)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbRequestBody)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

    jsonResponse.i = CkJsonObject::ckCreate()
    If jsonResponse.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(jsonResponse,sbResponseBody)

    type.s
    id.s
    sequence_id.s
    etag.s
    name.s
    created_at.s
    modified_at.s
    description.s
    size.i
    path_collectionTotal_count.i
    created_byType.s
    created_byId.s
    created_byName.s
    created_byLogin.s
    modified_byType.s
    modified_byId.s
    modified_byName.s
    modified_byLogin.s
    owned_byType.s
    owned_byId.s
    owned_byName.s
    owned_byLogin.s
    shared_linkUrl.s
    shared_linkDownload_url.i
    shared_linkVanity_url.i
    shared_linkIs_password_enabled.i
    shared_linkUnshared_at.i
    shared_linkDownload_count.i
    shared_linkPreview_count.i
    shared_linkAccess.s
    shared_linkPermissionsCan_download.i
    shared_linkPermissionsCan_preview.i
    folder_upload_emailAccess.s
    folder_upload_emailEmail.s
    parentType.s
    parentId.s
    parentSequence_id.i
    parentEtag.i
    parentName.s
    item_status.s
    item_collectionTotal_count.i
    item_collectionOffset.i
    item_collectionLimit.i
    i.i
    count_i.i
    sequence_id_bool.i
    etag_bool.i
    sha1.s

    type = CkJsonObject::ckStringOf(jsonResponse,"type")
    id = CkJsonObject::ckStringOf(jsonResponse,"id")
    sequence_id = CkJsonObject::ckStringOf(jsonResponse,"sequence_id")
    etag = CkJsonObject::ckStringOf(jsonResponse,"etag")
    name = CkJsonObject::ckStringOf(jsonResponse,"name")
    created_at = CkJsonObject::ckStringOf(jsonResponse,"created_at")
    modified_at = CkJsonObject::ckStringOf(jsonResponse,"modified_at")
    description = CkJsonObject::ckStringOf(jsonResponse,"description")
    size = CkJsonObject::ckIntOf(jsonResponse,"size")
    path_collectionTotal_count = CkJsonObject::ckIntOf(jsonResponse,"path_collection.total_count")
    created_byType = CkJsonObject::ckStringOf(jsonResponse,"created_by.type")
    created_byId = CkJsonObject::ckStringOf(jsonResponse,"created_by.id")
    created_byName = CkJsonObject::ckStringOf(jsonResponse,"created_by.name")
    created_byLogin = CkJsonObject::ckStringOf(jsonResponse,"created_by.login")
    modified_byType = CkJsonObject::ckStringOf(jsonResponse,"modified_by.type")
    modified_byId = CkJsonObject::ckStringOf(jsonResponse,"modified_by.id")
    modified_byName = CkJsonObject::ckStringOf(jsonResponse,"modified_by.name")
    modified_byLogin = CkJsonObject::ckStringOf(jsonResponse,"modified_by.login")
    owned_byType = CkJsonObject::ckStringOf(jsonResponse,"owned_by.type")
    owned_byId = CkJsonObject::ckStringOf(jsonResponse,"owned_by.id")
    owned_byName = CkJsonObject::ckStringOf(jsonResponse,"owned_by.name")
    owned_byLogin = CkJsonObject::ckStringOf(jsonResponse,"owned_by.login")
    shared_linkUrl = CkJsonObject::ckStringOf(jsonResponse,"shared_link.url")
    shared_linkDownload_url = CkJsonObject::ckIsNullOf(jsonResponse,"shared_link.download_url")
    shared_linkVanity_url = CkJsonObject::ckIsNullOf(jsonResponse,"shared_link.vanity_url")
    shared_linkIs_password_enabled = CkJsonObject::ckBoolOf(jsonResponse,"shared_link.is_password_enabled")
    shared_linkUnshared_at = CkJsonObject::ckIsNullOf(jsonResponse,"shared_link.unshared_at")
    shared_linkDownload_count = CkJsonObject::ckIntOf(jsonResponse,"shared_link.download_count")
    shared_linkPreview_count = CkJsonObject::ckIntOf(jsonResponse,"shared_link.preview_count")
    shared_linkAccess = CkJsonObject::ckStringOf(jsonResponse,"shared_link.access")
    shared_linkPermissionsCan_download = CkJsonObject::ckBoolOf(jsonResponse,"shared_link.permissions.can_download")
    shared_linkPermissionsCan_preview = CkJsonObject::ckBoolOf(jsonResponse,"shared_link.permissions.can_preview")
    folder_upload_emailAccess = CkJsonObject::ckStringOf(jsonResponse,"folder_upload_email.access")
    folder_upload_emailEmail = CkJsonObject::ckStringOf(jsonResponse,"folder_upload_email.email")
    parentType = CkJsonObject::ckStringOf(jsonResponse,"parent.type")
    parentId = CkJsonObject::ckStringOf(jsonResponse,"parent.id")
    parentSequence_id = CkJsonObject::ckIsNullOf(jsonResponse,"parent.sequence_id")
    parentEtag = CkJsonObject::ckIsNullOf(jsonResponse,"parent.etag")
    parentName = CkJsonObject::ckStringOf(jsonResponse,"parent.name")
    item_status = CkJsonObject::ckStringOf(jsonResponse,"item_status")
    item_collectionTotal_count = CkJsonObject::ckIntOf(jsonResponse,"item_collection.total_count")
    item_collectionOffset = CkJsonObject::ckIntOf(jsonResponse,"item_collection.offset")
    item_collectionLimit = CkJsonObject::ckIntOf(jsonResponse,"item_collection.limit")
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"path_collection.entries")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        type = CkJsonObject::ckStringOf(jsonResponse,"path_collection.entries[i].type")
        id = CkJsonObject::ckStringOf(jsonResponse,"path_collection.entries[i].id")
        sequence_id_bool = CkJsonObject::ckIsNullOf(jsonResponse,"path_collection.entries[i].sequence_id")
        etag_bool = CkJsonObject::ckIsNullOf(jsonResponse,"path_collection.entries[i].etag")
        name = CkJsonObject::ckStringOf(jsonResponse,"path_collection.entries[i].name")
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"item_collection.entries")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        type = CkJsonObject::ckStringOf(jsonResponse,"item_collection.entries[i].type")
        id = CkJsonObject::ckStringOf(jsonResponse,"item_collection.entries[i].id")
        sequence_id = CkJsonObject::ckStringOf(jsonResponse,"item_collection.entries[i].sequence_id")
        etag = CkJsonObject::ckStringOf(jsonResponse,"item_collection.entries[i].etag")
        sha1 = CkJsonObject::ckStringOf(jsonResponse,"item_collection.entries[i].sha1")
        name = CkJsonObject::ckStringOf(jsonResponse,"item_collection.entries[i].name")
        i = i + 1
    Wend


    CkRest::ckDispose(rest)
    CkJsonObject::ckDispose(json)
    CkStringBuilder::ckDispose(sbRequestBody)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jsonResponse)


    ProcedureReturn
EndProcedure

Sample JSON Response Body

{
  "type": "folder",
  "id": "11446498",
  "sequence_id": "1",
  "etag": "1",
  "name": "Pictures",
  "created_at": "2012-12-12T10:53:43-08:00",
  "modified_at": "2012-12-12T11:15:04-08:00",
  "description": "Some pictures I took",
  "size": 629644,
  "path_collection": {
    "total_count": 1,
    "entries": [
      {
        "type": "folder",
        "id": "0",
        "sequence_id": null,
        "etag": null,
        "name": "All Files"
      }
    ]
  },
  "created_by": {
    "type": "user",
    "id": "17738362",
    "name": "sean rose",
    "login": "sean@box.com"
  },
  "modified_by": {
    "type": "user",
    "id": "17738362",
    "name": "sean rose",
    "login": "sean@box.com"
  },
  "owned_by": {
    "type": "user",
    "id": "17738362",
    "name": "sean rose",
    "login": "sean@box.com"
  },
  "shared_link": {
    "url": "https://www.box.com/s/vspke7y05sb214wjokpk",
    "download_url": null,
    "vanity_url": null,
    "is_password_enabled": false,
    "unshared_at": null,
    "download_count": 0,
    "preview_count": 0,
    "access": "open",
    "permissions": {
      "can_download": true,
      "can_preview": true
    }
  },
  "folder_upload_email": {
    "access": "open",
    "email": "upload.Picture.k13sdz1@u.box.com"
  },
  "parent": {
    "type": "folder",
    "id": "0",
    "sequence_id": null,
    "etag": null,
    "name": "All Files"
  },
  "item_status": "active",
  "item_collection": {
    "total_count": 1,
    "entries": [
      {
        "type": "file",
        "id": "5000948880",
        "sequence_id": "3",
        "etag": "3",
        "sha1": "134b65991ed521fcfe4724b7d814ab8ded5185dc",
        "name": "tigers.jpeg"
      }
    ],
    "offset": 0,
    "limit": 100
  }
}