Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implementation of /delete_batch/check and /delete_batch #126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions src/Dropbox/Dropbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kunnu\Dropbox;

use Kunnu\Dropbox\Models\DeletedList;
use Kunnu\Dropbox\Models\DeletedMetadata;
use Kunnu\Dropbox\Models\File;
use Kunnu\Dropbox\Models\Account;
Expand Down Expand Up @@ -1274,4 +1275,86 @@ public function getSpaceUsage()
//Return the decoded body
return $body;
}

/**
* Delete multiple files/folders at once.
* This route is asynchronous, which returns a job ID immediately and runs the delete batch asynchronously.
* Use checkDeleteBatch to check the job status
*
* @param array|\Kunnu\Dropbox\Models\FolderMetadata[]|\Kunnu\Dropbox\Models\FileMetadata[] $files
* @return DeletedList|string List of deleted items or job ID
* @throws DropboxClientException
* @throws \Dropbox_BadRequestException
*/
public function deleteBatch(array $files)
{
// prepare list of files
$entries = [];
foreach ($files as $file) {
if (is_string($file)) {
$path = $file;
} elseif (
$file instanceof \Kunnu\Dropbox\Models\FolderMetadata ||
$file instanceof \Kunnu\Dropbox\Models\FileMetadata
) {
$path = $file->getPathLower();
} else {
throw new \Dropbox_BadRequestException("Invalid argument type");
}

$entries[] = ['path' => $path];
}

$response = $this->postToAPI('/files/delete_batch', ['entries' => $entries]);
$body = $response->getDecodedBody();

//Response doesn't have .tag
if (!isset($body['.tag'])) {
throw new DropboxClientException("Invalid Response.");
}

if ($body['.tag'] == 'complete') {
$entries = isset($body['entries']) ? $body['entries'] : [];

return new DeletedList($entries);
}

if (!isset($body['async_job_id'])) {
throw new DropboxClientException("Could not retrieve Async Job ID.");
}

return $body['async_job_id'];
}

/**
* Returns the status of an asynchronous job for deleteBatch. If success, it returns list of result for each entry.
*
* @param string $asyncJobId
* @return DeletedList|string
* @throws DropboxClientException
*/
public function checkDeleteBatch($asyncJobId)
{
//Async Job ID cannot be null
if (empty($asyncJobId)) {
throw new DropboxClientException("Async Job ID cannot be empty.");
}

//Get Job Status
$response = $this->postToAPI('/files/delete_batch/check', ['async_job_id' => $asyncJobId]);
$body = $response->getDecodedBody();

//Status
$status = isset($body['.tag']) ? $body['.tag'] : '';

//If status is complete
if ($status === 'complete') {
$entries = isset($body['entries']) ? $body['entries'] : [];

return new DeletedList($entries);
}

//Return the status
return $status;
}
}
36 changes: 36 additions & 0 deletions src/Dropbox/Models/DeletedList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace Kunnu\Dropbox\Models;

class DeletedList extends ModelCollection
{
/**
* Create a new Metadata Collection
*
* @param array $data Collection Data
*/
public function __construct(array $data)
{
$processedItems = $this->processItems($data);
parent::__construct($processedItems);
}

/**
* Process items and cast them
* to DeletedMetadata Model
*
* @param array $items Unprocessed Items
*
* @return array Array of DeletedMetadata models
*/
protected function processItems(array $items)
{
$processedEntries = [];

foreach ($items as $item) {
$item['metadata']['status'] = $item['.tag'];
$processedEntries[] = new DeletedMetadata($item['metadata']);
}

return $processedEntries;
}
}