Completed
Pull Request — master (#160)
by Lito
01:41
created

Dropbox::simpleUpload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 9
cp 0
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
namespace Kunnu\Dropbox;
4
5
use Kunnu\Dropbox\Models\DeletedMetadata;
6
use Kunnu\Dropbox\Models\File;
7
use Kunnu\Dropbox\Models\Account;
8
use Kunnu\Dropbox\Models\Thumbnail;
9
use Kunnu\Dropbox\Models\AccountList;
10
use Kunnu\Dropbox\Models\ModelFactory;
11
use Kunnu\Dropbox\Models\FileMetadata;
12
use Kunnu\Dropbox\Models\CopyReference;
13
use Kunnu\Dropbox\Models\FolderMetadata;
14
use Kunnu\Dropbox\Models\ModelCollection;
15
use Kunnu\Dropbox\Authentication\OAuth2Client;
16
use Kunnu\Dropbox\Store\PersistentDataStoreFactory;
17
use Kunnu\Dropbox\Authentication\DropboxAuthHelper;
18
use Kunnu\Dropbox\Exceptions\DropboxClientException;
19
use Kunnu\Dropbox\Security\RandomStringGeneratorFactory;
20
use Kunnu\Dropbox\Http\Clients\DropboxHttpClientFactory;
21
22
/**
23
 * Dropbox
24
 */
25
class Dropbox
26
{
27
    /**
28
     * Uploading a file with the 'uploadFile' method, with the file's
29
     * size less than this value (~8 MB), the simple `upload` method will be
30
     * used, if the file size exceed this value (~8 MB), the `startUploadSession`,
31
     * `appendUploadSession` & `finishUploadSession` methods will be used
32
     * to upload the file in chunks.
33
     *
34
     * @const int
35
     */
36
    const AUTO_CHUNKED_UPLOAD_THRESHOLD = 8000000;
37
38
    /**
39
     * The Chunk Size the file will be
40
     * split into and uploaded (~4 MB)
41
     *
42
     * @const int
43
     */
44
    const DEFAULT_CHUNK_SIZE = 4000000;
45
46
    /**
47
     * Response header containing file metadata
48
     *
49
     * @const string
50
     */
51
    const METADATA_HEADER = 'dropbox-api-result';
52
53
    /**
54
     * The Dropbox App
55
     *
56
     * @var \Kunnu\Dropbox\DropboxApp
57
     */
58
    protected $app;
59
60
    /**
61
     * OAuth2 Access Token
62
     *
63
     * @var string
64
     */
65
    protected $accessToken;
66
67
    /**
68
     * Dropbox Client
69
     *
70
     * @var \Kunnu\Dropbox\DropboxClient
71
     */
72
    protected $client;
73
74
    /**
75
     * OAuth2 Client
76
     *
77
     * @var \Kunnu\Dropbox\Authentication\OAuth2Client
78
     */
79
    protected $oAuth2Client;
80
81
    /**
82
     * Random String Generator
83
     *
84
     * @var \Kunnu\Dropbox\Security\RandomStringGeneratorInterface
85
     */
86
    protected $randomStringGenerator;
87
88
    /**
89
     * Persistent Data Store
90
     *
91
     * @var \Kunnu\Dropbox\Store\PersistentDataStoreInterface
92
     */
93
    protected $persistentDataStore;
94
95
    /**
96
     * Create a new Dropbox instance
97
     *
98
     * @param \Kunnu\Dropbox\DropboxApp
99
     * @param array $config Configuration Array
100
     */
101
    public function __construct(DropboxApp $app, array $config = [])
102
    {
103
        //Configuration
104
        $config = array_merge([
105
            'http_client_handler' => null,
106
            'random_string_generator' => null,
107
            'persistent_data_store' => null
108
        ], $config);
109
110
        //Set the app
111
        $this->app = $app;
112
113
        //Set the access token
114
        $this->setAccessToken($app->getAccessToken());
115
116
        //Make the HTTP Client
117
        $httpClient = DropboxHttpClientFactory::make($config['http_client_handler']);
118
119
        //Make and Set the DropboxClient
120
        $this->client = new DropboxClient($httpClient);
121
122
        //Make and Set the Random String Generator
123
        $this->randomStringGenerator = RandomStringGeneratorFactory::makeRandomStringGenerator($config['random_string_generator']);
124
125
        //Make and Set the Persistent Data Store
126
        $this->persistentDataStore = PersistentDataStoreFactory::makePersistentDataStore($config['persistent_data_store']);
127
    }
128
129
    /**
130
     * Get Dropbox Auth Helper
131
     *
132
     * @return \Kunnu\Dropbox\Authentication\DropboxAuthHelper
133
     */
134
    public function getAuthHelper()
135
    {
136
        return new DropboxAuthHelper(
137
            $this->getOAuth2Client(),
138
            $this->getRandomStringGenerator(),
139
            $this->getPersistentDataStore()
140
        );
141
    }
142
143
    /**
144
     * Get OAuth2Client
145
     *
146
     * @return \Kunnu\Dropbox\Authentication\OAuth2Client
147
     */
148
    public function getOAuth2Client()
149
    {
150
        if (!$this->oAuth2Client instanceof OAuth2Client) {
151
            return new OAuth2Client(
152
                $this->getApp(),
153
                $this->getClient(),
154
                $this->getRandomStringGenerator()
155
            );
156
        }
157
158
        return $this->oAuth2Client;
159
    }
160
161
    /**
162
     * Get the Dropbox App.
163
     *
164
     * @return \Kunnu\Dropbox\DropboxApp Dropbox App
165
     */
166
    public function getApp()
167
    {
168
        return $this->app;
169
    }
170
171
    /**
172
     * Get the Client
173
     *
174
     * @return \Kunnu\Dropbox\DropboxClient
175
     */
176
    public function getClient()
177
    {
178
        return $this->client;
179
    }
180
181
    /**
182
     * Get the Random String Generator
183
     *
184
     * @return \Kunnu\Dropbox\Security\RandomStringGeneratorInterface
185
     */
186
    public function getRandomStringGenerator()
187
    {
188
        return $this->randomStringGenerator;
189
    }
190
191
    /**
192
     * Get Persistent Data Store
193
     *
194
     * @return \Kunnu\Dropbox\Store\PersistentDataStoreInterface
195
     */
196
    public function getPersistentDataStore()
197
    {
198
        return $this->persistentDataStore;
199
    }
200
201
    /**
202
     * Get the Metadata for a file or folder
203
     *
204
     * @param  string $path   Path of the file or folder
205
     * @param  array  $params Additional Params
206
     *
207
     * @return \Kunnu\Dropbox\Models\FileMetadata | \Kunnu\Dropbox\Models\FolderMetadata
208
     * @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
209
     *
210
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_metadata
211
     *
212
     */
213
    public function getMetadata($path, array $params = [])
214
    {
215
        //Root folder is unsupported
216
        if ($path === '/') {
217
            throw new DropboxClientException("Metadata for the root folder is unsupported.");
218
        }
219
220
        //Set the path
221
        $params['path'] = $path;
222
223
        //Get File Metadata
224
        $response = $this->postToAPI('/files/get_metadata', $params);
225
226
        //Make and Return the Model
227
        return $this->makeModelFromResponse($response);
228
    }
229
230
    /**
231
     * Make a HTTP POST Request to the API endpoint type
232
     *
233
     * @param  string $endpoint    API Endpoint to send Request to
234
     * @param  array  $params      Request Query Params
235
     * @param  string $accessToken Access Token to send with the Request
236
     *
237
     * @return \Kunnu\Dropbox\DropboxResponse
238
     */
239
    public function postToAPI($endpoint, array $params = [], $accessToken = null)
240
    {
241
        return $this->sendRequest("POST", $endpoint, 'api', $params, $accessToken);
242
    }
243
244
    /**
245
     * Make Request to the API
246
     *
247
     * @param  string      $method       HTTP Request Method
248
     * @param  string      $endpoint     API Endpoint to send Request to
249
     * @param  string      $endpointType Endpoint type ['api'|'content']
250
     * @param  array       $params       Request Query Params
251
     * @param  string      $accessToken  Access Token to send with the Request
252
     * @param  DropboxFile $responseFile Save response to the file
253
     *
254
     * @return \Kunnu\Dropbox\DropboxResponse
255
     *
256
     * @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
257
     */
258
    public function sendRequest($method, $endpoint, $endpointType = 'api', array $params = [], $accessToken = null, DropboxFile $responseFile = null)
259
    {
260
        //Access Token
261
        $accessToken = $this->getAccessToken() ? $this->getAccessToken() : $accessToken;
262
263
        //Make a DropboxRequest object
264
        $request = new DropboxRequest($method, $endpoint, $accessToken, $endpointType, $params);
265
266
        //Make a DropboxResponse object if a response should be saved to the file
267
        $response = $responseFile ? new DropboxResponseToFile($request, $responseFile) : null;
268
269
        //Send Request through the DropboxClient
270
        //Fetch and return the Response
271
        return $this->getClient()->sendRequest($request, $response);
272
    }
273
274
    /**
275
     * Get the Access Token.
276
     *
277
     * @return string Access Token
278
     */
279
    public function getAccessToken()
280
    {
281
        return $this->accessToken;
282
    }
283
284
    /**
285
     * Set the Access Token.
286
     *
287
     * @param string $accessToken Access Token
288
     *
289
     * @return \Kunnu\Dropbox\Dropbox Dropbox Client
290
     */
291
    public function setAccessToken($accessToken)
292
    {
293
        $this->accessToken = $accessToken;
294
295
        return $this;
296
    }
297
298
    /**
299
     * Make Model from DropboxResponse
300
     *
301
     * @param  DropboxResponse $response
302
     *
303
     * @return \Kunnu\Dropbox\Models\ModelInterface
304
     *
305
     * @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
306
     */
307
    public function makeModelFromResponse(DropboxResponse $response)
308
    {
309
        //Get the Decoded Body
310
        $body = $response->getDecodedBody();
311
312
        if (is_null($body)) {
313
            $body = [];
314
        }
315
316
        //Make and Return the Model
317
        return ModelFactory::make($body);
318
    }
319
320
    /**
321
     * Get the contents of a Folder
322
     *
323
     * @param  string $path   Path to the folder. Defaults to root.
324
     * @param  array  $params Additional Params
325
     *
326
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder
327
     *
328
     * @return \Kunnu\Dropbox\Models\MetadataCollection
329
     */
330 View Code Duplication
    public function listFolder($path = null, array $params = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
331
    {
332
        //Specify the root folder as an
333
        //empty string rather than as "/"
334
        if ($path === '/') {
335
            $path = "";
336
        }
337
338
        //Set the path
339
        $params['path'] = $path;
340
341
        //Get File Metadata
342
        $response = $this->postToAPI('/files/list_folder', $params);
343
344
        //Make and Return the Model
345
        return $this->makeModelFromResponse($response);
346
    }
347
348
    /**
349
     * Paginate through all files and retrieve updates to the folder,
350
     * using the cursor retrieved from listFolder or listFolderContinue
351
     *
352
     * @param  string $cursor The cursor returned by your
353
     *                        last call to listFolder or listFolderContinue
354
     *
355
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder-continue
356
     *
357
     * @return \Kunnu\Dropbox\Models\MetadataCollection
358
     */
359
    public function listFolderContinue($cursor)
360
    {
361
        $response = $this->postToAPI('/files/list_folder/continue', ['cursor' => $cursor]);
362
363
        //Make and Return the Model
364
        return $this->makeModelFromResponse($response);
365
    }
366
367
    /**
368
     * Get a cursor for the folder's state.
369
     *
370
     * @param  string $path   Path to the folder. Defaults to root.
371
     * @param  array  $params Additional Params
372
     *
373
     * @return string The Cursor for the folder's state
374
     *
375
     * @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
376
     *
377
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder-get_latest_cursor
378
     *
379
     */
380
    public function listFolderLatestCursor($path, array $params = [])
381
    {
382
        //Specify the root folder as an
383
        //empty string rather than as "/"
384
        if ($path === '/') {
385
            $path = "";
386
        }
387
388
        //Set the path
389
        $params['path'] = $path;
390
391
        //Fetch the cursor
392
        $response = $this->postToAPI('/files/list_folder/get_latest_cursor', $params);
393
394
        //Retrieve the cursor
395
        $body = $response->getDecodedBody();
396
        $cursor = isset($body['cursor']) ? $body['cursor'] : false;
397
398
        //No cursor returned
399
        if (!$cursor) {
400
            throw new DropboxClientException("Could not retrieve cursor. Something went wrong.");
401
        }
402
403
        //Return the cursor
404
        return $cursor;
405
    }
406
407
    /**
408
     * Get Revisions of a File
409
     *
410
     * @param  string $path   Path to the file
411
     * @param  array  $params Additional Params
412
     *
413
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_revisions
414
     *
415
     * @return \Kunnu\Dropbox\Models\ModelCollection
416
     */
417
    public function listRevisions($path, array $params = [])
418
    {
419
        //Set the Path
420
        $params['path'] = $path;
421
422
        //Fetch the Revisions
423
        $response = $this->postToAPI('/files/list_revisions', $params);
424
425
        //The file metadata of the entries, returned by this
426
        //endpoint doesn't include a '.tag' attribute, which
427
        //is used by the ModelFactory to resolve the correct
428
        //model. But since we know that revisions returned
429
        //are file metadata objects, we can explicitly cast
430
        //them as \Kunnu\Dropbox\Models\FileMetadata manually.
431
        $body = $response->getDecodedBody();
432
        $entries = isset($body['entries']) ? $body['entries'] : [];
433
        $processedEntries = [];
434
435
        foreach ($entries as $entry) {
436
            $processedEntries[] = new FileMetadata($entry);
437
        }
438
439
        return new ModelCollection($processedEntries);
440
    }
441
442
    /**
443
     * Search a folder for files/folders
444
     *
445
     * @param  string $path   Path to search
446
     * @param  string $query  Search Query
447
     * @param  array  $params Additional Params
448
     *
449
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-search
450
     *
451
     * @return \Kunnu\Dropbox\Models\SearchResults
452
     */
453 View Code Duplication
    public function search($path, $query, array $params = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
454
    {
455
        //Specify the root folder as an
456
        //empty string rather than as "/"
457
        if ($path === '/') {
458
            $path = "";
459
        }
460
461
        //Set the path and query
462
        $params['path'] = $path;
463
        $params['query'] = $query;
464
465
        //Fetch Search Results
466
        $response = $this->postToAPI('/files/search', $params);
467
468
        //Make and Return the Model
469
        return $this->makeModelFromResponse($response);
470
    }
471
472
    /**
473
     * Create a folder at the given path
474
     *
475
     * @param  string  $path       Path to create
476
     * @param  boolean $autorename Auto Rename File
477
     *
478
     * @return \Kunnu\Dropbox\Models\FolderMetadata
479
     *
480
     * @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
481
     *
482
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-create_folder
483
     *
484
     */
485 View Code Duplication
    public function createFolder($path, $autorename = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
486
    {
487
        //Path cannot be null
488
        if (is_null($path)) {
489
            throw new DropboxClientException("Path cannot be null.");
490
        }
491
492
        //Create Folder
493
        $response = $this->postToAPI('/files/create_folder', ['path' => $path, 'autorename' => $autorename]);
494
495
        //Fetch the Metadata
496
        $body = $response->getDecodedBody();
497
498
        //Make and Return the Model
499
        return new FolderMetadata($body);
500
    }
501
502
    /**
503
     * Delete a file or folder at the given path
504
     *
505
     * @param  string $path Path to file/folder to delete
506
     *
507
     * @return \Kunnu\Dropbox\Models\DeletedMetadata
508
     *
509
     * @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
510
     *
511
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-delete
512
     *
513
     */
514 View Code Duplication
    public function delete($path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
515
    {
516
        //Path cannot be null
517
        if (is_null($path)) {
518
            throw new DropboxClientException("Path cannot be null.");
519
        }
520
521
        //Delete
522
        $response = $this->postToAPI('/files/delete_v2', ['path' => $path]);
523
        $body = $response->getDecodedBody();
524
525
        //Response doesn't have Metadata
526
        if (!isset($body['metadata']) || !is_array($body['metadata'])) {
527
            throw new DropboxClientException("Invalid Response.");
528
        }
529
530
        return new DeletedMetadata($body['metadata']);
531
    }
532
533
    /**
534
     * Move a file or folder to a different location
535
     *
536
     * @param  string $fromPath Path to be moved
537
     * @param  string $toPath   Path to be moved to
538
     *
539
     * @return \Kunnu\Dropbox\Models\DeletedMetadata|\Kunnu\Dropbox\Models\FileMetadata
540
     *
541
     * @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
542
     *
543
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-move
544
     *
545
     */
546 View Code Duplication
    public function move($fromPath, $toPath)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
547
    {
548
        //From and To paths cannot be null
549
        if (is_null($fromPath) || is_null($toPath)) {
550
            throw new DropboxClientException("From and To paths cannot be null.");
551
        }
552
553
        //Response
554
        $response = $this->postToAPI('/files/move', ['from_path' => $fromPath, 'to_path' => $toPath]);
555
556
        //Make and Return the Model
557
        return $this->makeModelFromResponse($response);
558
    }
559
560
    /**
561
     * Copy a file or folder to a different location
562
     *
563
     * @param  string $fromPath Path to be copied
564
     * @param  string $toPath   Path to be copied to
565
     *
566
     * @return \Kunnu\Dropbox\Models\DeletedMetadata|\Kunnu\Dropbox\Models\FileMetadata
567
     *
568
     * @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
569
     *
570
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-copy
571
     *
572
     */
573 View Code Duplication
    public function copy($fromPath, $toPath)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
574
    {
575
        //From and To paths cannot be null
576
        if (is_null($fromPath) || is_null($toPath)) {
577
            throw new DropboxClientException("From and To paths cannot be null.");
578
        }
579
580
        //Response
581
        $response = $this->postToAPI('/files/copy', ['from_path' => $fromPath, 'to_path' => $toPath]);
582
583
        //Make and Return the Model
584
        return $this->makeModelFromResponse($response);
585
    }
586
587
    /**
588
     * Restore a file to the specific version
589
     *
590
     * @param  string $path Path to the file to restore
591
     * @param  string $rev  Revision to store for the file
592
     *
593
     * @return \Kunnu\Dropbox\Models\DeletedMetadata|\Kunnu\Dropbox\Models\FileMetadata|\Kunnu\Dropbox\Models\FolderMetadata
594
     *
595
     * @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
596
     *
597
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-restore
598
     *
599
     */
600 View Code Duplication
    public function restore($path, $rev)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
601
    {
602
        //Path and Revision cannot be null
603
        if (is_null($path) || is_null($rev)) {
604
            throw new DropboxClientException("Path and Revision cannot be null.");
605
        }
606
607
        //Response
608
        $response = $this->postToAPI('/files/restore', ['path' => $path, 'rev' => $rev]);
609
610
        //Fetch the Metadata
611
        $body = $response->getDecodedBody();
612
613
        //Make and Return the Model
614
        return new FileMetadata($body);
615
    }
616
617
    /**
618
     * Get Copy Reference
619
     *
620
     * @param  string $path Path to the file or folder to get a copy reference to
621
     *
622
     * @return \Kunnu\Dropbox\Models\CopyReference
623
     *
624
     * @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
625
     *
626
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-copy_reference-get
627
     *
628
     */
629 View Code Duplication
    public function getCopyReference($path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
630
    {
631
        //Path cannot be null
632
        if (is_null($path)) {
633
            throw new DropboxClientException("Path cannot be null.");
634
        }
635
636
        //Get Copy Reference
637
        $response = $this->postToAPI('/files/copy_reference/get', ['path' => $path]);
638
        $body = $response->getDecodedBody();
639
640
        //Make and Return the Model
641
        return new CopyReference($body);
642
    }
643
644
    /**
645
     * Save Copy Reference
646
     *
647
     * @param  string $path          Path to the file or folder to get a copy reference to
648
     * @param  string $copyReference Copy reference returned by getCopyReference
649
     *
650
     * @return \Kunnu\Dropbox\Models\FileMetadata|\Kunnu\Dropbox\Models\FolderMetadata
651
     *
652
     * @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
653
     *
654
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-copy_reference-save
655
     *
656
     */
657
    public function saveCopyReference($path, $copyReference)
658
    {
659
        //Path and Copy Reference cannot be null
660
        if (is_null($path) || is_null($copyReference)) {
661
            throw new DropboxClientException("Path and Copy Reference cannot be null.");
662
        }
663
664
        //Save Copy Reference
665
        $response = $this->postToAPI('/files/copy_reference/save', ['path' => $path, 'copy_reference' => $copyReference]);
666
        $body = $response->getDecodedBody();
667
668
        //Response doesn't have Metadata
669
        if (!isset($body['metadata']) || !is_array($body['metadata'])) {
670
            throw new DropboxClientException("Invalid Response.");
671
        }
672
673
        //Make and return the Model
674
        return ModelFactory::make($body['metadata']);
675
    }
676
677
    /**
678
     * Get a temporary link to stream contents of a file
679
     *
680
     * @param  string $path Path to the file you want a temporary link to
681
     *
682
     * https://www.dropbox.com/developers/documentation/http/documentation#files-get_temporary_link
683
     *
684
     * @return \Kunnu\Dropbox\Models\TemporaryLink
685
     *
686
     * @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
687
     */
688 View Code Duplication
    public function getTemporaryLink($path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
689
    {
690
        //Path cannot be null
691
        if (is_null($path)) {
692
            throw new DropboxClientException("Path cannot be null.");
693
        }
694
695
        //Get Temporary Link
696
        $response = $this->postToAPI('/files/get_temporary_link', ['path' => $path]);
697
698
        //Make and Return the Model
699
        return $this->makeModelFromResponse($response);
700
    }
701
702
    /**
703
     * Save a specified URL into a file in user's Dropbox
704
     *
705
     * @param  string $path Path where the URL will be saved
706
     * @param  string $url  URL to be saved
707
     *
708
     * @return string Async Job ID
709
     *
710
     * @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
711
     *
712
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-save_url
713
     *
714
     */
715 View Code Duplication
    public function saveUrl($path, $url)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
716
    {
717
        //Path and URL cannot be null
718
        if (is_null($path) || is_null($url)) {
719
            throw new DropboxClientException("Path and URL cannot be null.");
720
        }
721
722
        //Save URL
723
        $response = $this->postToAPI('/files/save_url', ['path' => $path, 'url' => $url]);
724
        $body = $response->getDecodedBody();
725
726
        if (!isset($body['async_job_id'])) {
727
            throw new DropboxClientException("Could not retrieve Async Job ID.");
728
        }
729
730
        //Return the Async Job ID
731
        return $body['async_job_id'];
732
    }
733
734
    /**
735
     * Save a specified URL into a file in user's Dropbox
736
     *
737
     * @param $asyncJobId
738
     *
739
     * @return \Kunnu\Dropbox\Models\FileMetadata|string Status (failed|in_progress) or FileMetadata (if complete)
740
     *
741
     * @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
742
     *
743
     * @link     https://www.dropbox.com/developers/documentation/http/documentation#files-save_url-check_job_status
744
     *
745
     */
746
    public function checkJobStatus($asyncJobId)
747
    {
748
        //Async Job ID cannot be null
749
        if (is_null($asyncJobId)) {
750
            throw new DropboxClientException("Async Job ID cannot be null.");
751
        }
752
753
        //Get Job Status
754
        $response = $this->postToAPI('/files/save_url/check_job_status', ['async_job_id' => $asyncJobId]);
755
        $body = $response->getDecodedBody();
756
757
        //Status
758
        $status = isset($body['.tag']) ? $body['.tag'] : '';
759
760
        //If status is complete
761
        if ($status === 'complete') {
762
            return new FileMetadata($body);
763
        }
764
765
        //Return the status
766
        return $status;
767
    }
768
769
    /**
770
     * Upload a File to Dropbox
771
     *
772
     * @param  string|DropboxFile $dropboxFile DropboxFile object or Path to file
773
     * @param  string             $path        Path to upload the file to
774
     * @param  array              $params      Additional Params
775
     *
776
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload
777
     *
778
     * @return \Kunnu\Dropbox\Models\FileMetadata
779
     */
780
    public function upload($dropboxFile, $path, array $params = [])
781
    {
782
        //Make Dropbox File
783
        $dropboxFile = $this->makeDropboxFile($dropboxFile);
784
785
        //If the file is larger than the Chunked Upload Threshold
786
        if ($dropboxFile->getSize() > static::AUTO_CHUNKED_UPLOAD_THRESHOLD) {
787
            //Upload the file in sessions/chunks
788
            return $this->uploadChunked($dropboxFile, $path, null, null, $params);
789
        }
790
791
        //Simple file upload
792
        return $this->simpleUpload($dropboxFile, $path, $params);
793
    }
794
795
    /**
796
     * Make DropboxFile Object
797
     *
798
     * @param  string|DropboxFile $dropboxFile DropboxFile object or Path to file
799
     * @param  int                $maxLength   Max Bytes to read from the file
800
     * @param  int                $offset      Seek to specified offset before reading
801
     * @param  string             $mode        The type of access
802
     *
803
     * @return \Kunnu\Dropbox\DropboxFile
804
     */
805
    public function makeDropboxFile($dropboxFile, $maxLength = null, $offset = null, $mode = DropboxFile::MODE_READ)
806
    {
807
        //Uploading file by file path
808
        if (!$dropboxFile instanceof DropboxFile) {
809
            //Create a DropboxFile Object
810
            $dropboxFile = new DropboxFile($dropboxFile, $mode);
811
        } elseif ($mode !== $dropboxFile->getMode()) {
812
            //Reopen the file with expected mode
813
            $dropboxFile->close();
814
            $dropboxFile = new DropboxFile($dropboxFile->getFilePath(), $mode);
815
        }
816
817
        if (!is_null($offset)) {
818
            $dropboxFile->setOffset($offset);
819
        }
820
821
        if (!is_null($maxLength)) {
822
            $dropboxFile->setMaxLength($maxLength);
823
        }
824
825
        //Return the DropboxFile Object
826
        return $dropboxFile;
827
    }
828
829
    /**
830
     * Upload file in sessions/chunks
831
     *
832
     * @param  string|DropboxFile $dropboxFile DropboxFile object or Path to file
833
     * @param  string             $path        Path to save the file to, on Dropbox
834
     * @param  int                $fileSize    The size of the file
835
     * @param  int                $chunkSize   The amount of data to upload in each chunk
836
     * @param  array              $params      Additional Params
837
     *
838
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-start
839
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-finish
840
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-append_v2
841
     *
842
     * @return \Kunnu\Dropbox\Models\FileMetadata
843
     */
844
    public function uploadChunked($dropboxFile, $path, $fileSize = null, $chunkSize = null, array $params = array())
845
    {
846
        //Make Dropbox File
847
        $dropboxFile = $this->makeDropboxFile($dropboxFile);
848
849
        //No file size specified explicitly
850
        if (is_null($fileSize)) {
851
            $fileSize = $dropboxFile->getSize();
852
        }
853
854
        //No chunk size specified, use default size
855
        if (is_null($chunkSize)) {
856
            $chunkSize = static::DEFAULT_CHUNK_SIZE;
857
        }
858
859
        //If the fileSize is smaller
860
        //than the chunk size, we'll
861
        //make the chunk size relatively
862
        //smaller than the file size
863
        if ($fileSize <= $chunkSize) {
864
            $chunkSize = intval($fileSize / 2);
865
        }
866
867
        //Start the Upload Session with the file path
868
        //since the DropboxFile object will be created
869
        //again using the new chunk size.
870
        $sessionId = $this->startUploadSession($dropboxFile->getFilePath(), $chunkSize);
871
872
        //Uploaded
873
        $uploaded = $chunkSize;
874
875
        //Remaining
876
        $remaining = $fileSize - $chunkSize;
877
878
        //While the remaining bytes are
879
        //more than the chunk size, append
880
        //the chunk to the upload session.
881
        while ($remaining > $chunkSize) {
882
            //Append the next chunk to the Upload session
883
            $sessionId = $this->appendUploadSession($dropboxFile, $sessionId, $uploaded, $chunkSize);
884
885
            //Update remaining and uploaded
886
            $uploaded = $uploaded + $chunkSize;
887
            $remaining = $remaining - $chunkSize;
888
        }
889
890
        //Finish the Upload Session and return the Uploaded File Metadata
891
        return $this->finishUploadSession($dropboxFile, $sessionId, $uploaded, $remaining, $path, $params);
892
    }
893
894
    /**
895
     * Start an Upload Session
896
     *
897
     * @param  string|DropboxFile $dropboxFile DropboxFile object or Path to file
898
     * @param  int                $chunkSize   Size of file chunk to upload
899
     * @param  boolean            $close       Closes the session for "appendUploadSession"
900
     *
901
     * @return string Unique identifier for the upload session
902
     *
903
     * @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
904
     *
905
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-start
906
     *
907
     */
908
    public function startUploadSession($dropboxFile, $chunkSize = -1, $close = false)
909
    {
910
        //Make Dropbox File with the given chunk size
911
        $dropboxFile = $this->makeDropboxFile($dropboxFile, $chunkSize);
912
913
        //Set the close param
914
        $params = [
915
            'close' => $close ? true : false,
916
            'file' => $dropboxFile
917
        ];
918
919
        //Upload File
920
        $file = $this->postToContent('/files/upload_session/start', $params);
921
        $body = $file->getDecodedBody();
922
923
        //Cannot retrieve Session ID
924
        if (!isset($body['session_id'])) {
925
            throw new DropboxClientException("Could not retrieve Session ID.");
926
        }
927
928
        //Return the Session ID
929
        return $body['session_id'];
930
    }
931
932
    /**
933
     * Make a HTTP POST Request to the Content endpoint type
934
     *
935
     * @param  string      $endpoint     Content Endpoint to send Request to
936
     * @param  array       $params       Request Query Params
937
     * @param  string      $accessToken  Access Token to send with the Request
938
     * @param  DropboxFile $responseFile Save response to the file
939
     *
940
     * @return \Kunnu\Dropbox\DropboxResponse
941
     */
942
    public function postToContent($endpoint, array $params = [], $accessToken = null, DropboxFile $responseFile = null)
943
    {
944
        return $this->sendRequest("POST", $endpoint, 'content', $params, $accessToken, $responseFile);
945
    }
946
947
    /**
948
     * Append more data to an Upload Session
949
     *
950
     * @param  string|DropboxFile $dropboxFile DropboxFile object or Path to file
951
     * @param  string             $sessionId   Session ID returned by `startUploadSession`
952
     * @param  int                $offset      The amount of data that has been uploaded so far
953
     * @param  int                $chunkSize   The amount of data to upload
954
     * @param  boolean            $close       Closes the session for futher "appendUploadSession" calls
955
     *
956
     * @return string Unique identifier for the upload session
957
     *
958
     * @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
959
     *
960
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-append_v2
961
     *
962
     */
963
    public function appendUploadSession($dropboxFile, $sessionId, $offset, $chunkSize, $close = false)
964
    {
965
        //Make Dropbox File
966
        $dropboxFile = $this->makeDropboxFile($dropboxFile, $chunkSize, $offset);
967
968
        //Session ID, offset, chunkSize and path cannot be null
969 View Code Duplication
        if (is_null($sessionId) || is_null($offset) || is_null($chunkSize)) {
970
            throw new DropboxClientException("Session ID, offset and chunk size cannot be null");
971
        }
972
973
        $params = [];
974
975
        //Set the File
976
        $params['file'] = $dropboxFile;
977
978
        //Set the Cursor: Session ID and Offset
979
        $params['cursor'] = ['session_id' => $sessionId, 'offset' => $offset];
980
981
        //Set the close param
982
        $params['close'] = $close ? true : false;
983
984
        //Since this endpoint doesn't have
985
        //any return values, we'll disable the
986
        //response validation for this request.
987
        $params['validateResponse'] = false;
988
989
        //Upload File
990
        $this->postToContent('/files/upload_session/append_v2', $params);
991
992
        //Make and Return the Model
993
        return $sessionId;
994
    }
995
996
    /**
997
     * Finish an upload session and save the uploaded data to the given file path
998
     *
999
     * @param  string|DropboxFile $dropboxFile DropboxFile object or Path to file
1000
     * @param  string             $sessionId   Session ID returned by `startUploadSession`
1001
     * @param  int                $offset      The amount of data that has been uploaded so far
1002
     * @param  int                $remaining   The amount of data that is remaining
1003
     * @param  string             $path        Path to save the file to, on Dropbox
1004
     * @param  array              $params      Additional Params
1005
     *
1006
     * @return \Kunnu\Dropbox\Models\FileMetadata
1007
     *
1008
     * @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
1009
     *
1010
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-finish
1011
     *
1012
     */
1013
    public function finishUploadSession($dropboxFile, $sessionId, $offset, $remaining, $path, array $params = [])
1014
    {
1015
        //Make Dropbox File
1016
        $dropboxFile = $this->makeDropboxFile($dropboxFile, $remaining, $offset);
1017
1018
        //Session ID, offset, remaining and path cannot be null
1019 View Code Duplication
        if (is_null($sessionId) || is_null($path) || is_null($offset) || is_null($remaining)) {
1020
            throw new DropboxClientException("Session ID, offset, remaining and path cannot be null");
1021
        }
1022
1023
        $queryParams = [];
1024
1025
        //Set the File
1026
        $queryParams['file'] = $dropboxFile;
1027
1028
        //Set the Cursor: Session ID and Offset
1029
        $queryParams['cursor'] = ['session_id' => $sessionId, 'offset' => $offset];
1030
1031
        //Set the path
1032
        $params['path'] = $path;
1033
        //Set the Commit
1034
        $queryParams['commit'] = $params;
1035
1036
        //Upload File
1037
        $file = $this->postToContent('/files/upload_session/finish', $queryParams);
1038
        $body = $file->getDecodedBody();
1039
1040
        //Make and Return the Model
1041
        return new FileMetadata($body);
1042
    }
1043
1044
    /**
1045
     * Upload a File to Dropbox in a single request
1046
     *
1047
     * @param  string|DropboxFile $dropboxFile DropboxFile object or Path to file
1048
     * @param  string             $path        Path to upload the file to
1049
     * @param  array              $params      Additional Params
1050
     *
1051
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload
1052
     *
1053
     * @return \Kunnu\Dropbox\Models\FileMetadata
1054
     */
1055
    public function simpleUpload($dropboxFile, $path, array $params = [])
1056
    {
1057
        //Make Dropbox File
1058
        $dropboxFile = $this->makeDropboxFile($dropboxFile);
1059
1060
        //Set the path and file
1061
        $params['path'] = $path;
1062
        $params['file'] = $dropboxFile;
1063
1064
        //Upload File
1065
        $file = $this->postToContent('/files/upload', $params);
1066
        $body = $file->getDecodedBody();
1067
1068
        //Make and Return the Model
1069
        return new FileMetadata($body);
1070
    }
1071
1072
    /**
1073
     * Get a thumbnail for an image
1074
     *
1075
     * @param  string $path   Path to the file you want a thumbnail to
1076
     * @param  string $size   Size for the thumbnail image ['thumb','small','medium','large','huge']
1077
     * @param  string $format Format for the thumbnail image ['jpeg'|'png']
1078
     *
1079
     * @return \Kunnu\Dropbox\Models\Thumbnail
1080
     *
1081
     * @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
1082
     *
1083
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail
1084
     *
1085
     */
1086
    public function getThumbnail($path, $size = 'small', $format = 'jpeg')
1087
    {
1088
        //Path cannot be null
1089
        if (is_null($path)) {
1090
            throw new DropboxClientException("Path cannot be null.");
1091
        }
1092
1093
        //Invalid Format
1094
        if (!in_array($format, ['jpeg', 'png'])) {
1095
            throw new DropboxClientException("Invalid format. Must either be 'jpeg' or 'png'.");
1096
        }
1097
1098
        //Thumbnail size
1099
        $size = $this->getThumbnailSize($size);
1100
1101
        //Get Thumbnail
1102
        $response = $this->postToContent('/files/get_thumbnail', ['path' => $path, 'format' => $format, 'size' => $size]);
1103
1104
        //Get file metadata from response headers
1105
        $metadata = $this->getMetadataFromResponseHeaders($response);
1106
1107
        //File Contents
1108
        $contents = $response->getBody();
1109
1110
        //Make and return a Thumbnail model
1111
        return new Thumbnail($metadata, $contents);
1112
    }
1113
1114
    /**
1115
     * Get thumbnail size
1116
     *
1117
     * @param  string $size Thumbnail Size
1118
     *
1119
     * @return string
1120
     */
1121
    protected function getThumbnailSize($size)
1122
    {
1123
        $thumbnailSizes = [
1124
            'thumb' => 'w32h32',
1125
            'small' => 'w64h64',
1126
            'medium' => 'w128h128',
1127
            'large' => 'w640h480',
1128
            'huge' => 'w1024h768'
1129
        ];
1130
1131
        return isset($thumbnailSizes[$size]) ? $thumbnailSizes[$size] : $thumbnailSizes['small'];
1132
    }
1133
1134
    /**
1135
     * Get metadata from response headers
1136
     *
1137
     * @param  DropboxResponse $response
1138
     *
1139
     * @return array
1140
     */
1141
    protected function getMetadataFromResponseHeaders(DropboxResponse $response)
1142
    {
1143
        //Response Headers
1144
        $headers = $response->getHeaders();
1145
1146
        //Empty metadata for when
1147
        //metadata isn't returned
1148
        $metadata = [];
1149
1150
        //If metadata is available
1151
        if (isset($headers[static::METADATA_HEADER])) {
1152
            //File Metadata
1153
            $data = $headers[static::METADATA_HEADER];
1154
1155
            //The metadata is present in the first index
1156
            //of the metadata response header array
1157
            if (is_array($data) && isset($data[0])) {
1158
                $data = $data[0];
1159
            }
1160
1161
            //Since the metadata is returned as a json string
1162
            //it needs to be decoded into an associative array
1163
            $metadata = json_decode((string)$data, true);
1164
        }
1165
1166
        //Return the metadata
1167
        return $metadata;
1168
    }
1169
1170
    /**
1171
     * Download a File
1172
     *
1173
     * @param  string                  $path        Path to the file you want to download
1174
     * @param  null|string|DropboxFile $dropboxFile DropboxFile object or Path to target file
1175
     *
1176
     * @return \Kunnu\Dropbox\Models\File
1177
     *
1178
     * @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
1179
     *
1180
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-download
1181
     *
1182
     */
1183 View Code Duplication
    public function download($path, $dropboxFile = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1184
    {
1185
        //Path cannot be null
1186
        if (is_null($path)) {
1187
            throw new DropboxClientException("Path cannot be null.");
1188
        }
1189
1190
        //Make Dropbox File if target is specified
1191
        $dropboxFile = $dropboxFile ? $this->makeDropboxFile($dropboxFile, null, null, DropboxFile::MODE_WRITE) : null;
1192
1193
        //Download File
1194
        $response = $this->postToContent('/files/download', ['path' => $path], null, $dropboxFile);
1195
1196
        //Get file metadata from response headers
1197
        $metadata = $this->getMetadataFromResponseHeaders($response);
1198
1199
        //File Contents
1200
        $contents = $dropboxFile ? $this->makeDropboxFile($dropboxFile) : $response->getBody();
1201
1202
        //Make and return a File model
1203
        return new File($metadata, $contents);
1204
    }
1205
1206
	/**
1207
	 * Download a folder as a zip file
1208
	 *
1209
	 * @param  string                  $path        Path to the file you want to download
1210
	 * @param  null|string|DropboxFile $dropboxFile DropboxFile object or Path to target file
1211
	 *
1212
	 * @return \Kunnu\Dropbox\Models\File
1213
	 *
1214
	 * @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
1215
	 *
1216
	 * @link https://www.dropbox.com/developers/documentation/http/documentation#files-download_zip
1217
	 *
1218
	 */
1219 View Code Duplication
	 public function downloadZip($path, $dropboxFile = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1220
	 {
1221
		 //Path cannot be null
1222
		 if (is_null($path)) {
1223
			 throw new DropboxClientException("Path cannot be null.");
1224
		 }
1225
1226
		 //Make Dropbox File if target is specified
1227
		 $dropboxFile = $dropboxFile ? $this->makeDropboxFile($dropboxFile, null, null, DropboxFile::MODE_WRITE) : null;
1228
1229
		 //Download File
1230
		 $response = $this->postToContent('/files/download_zip', ['path' => $path], null, $dropboxFile);
1231
1232
		 //Get file metadata from response headers
1233
		 $metadata = $this->getMetadataFromResponseHeaders($response);
1234
1235
		 //File Contents
1236
		 $contents = $dropboxFile ? $this->makeDropboxFile($dropboxFile) : $response->getBody();
1237
1238
		 //Make and return a File model
1239
		 return new File($metadata, $contents);
1240
	}
1241
1242
    /**
1243
     * Get Current Account
1244
     *
1245
     * @link https://www.dropbox.com/developers/documentation/http/documentation#users-get_current_account
1246
     *
1247
     * @return \Kunnu\Dropbox\Models\Account
1248
     */
1249
    public function getCurrentAccount()
1250
    {
1251
        //Get current account
1252
        $response = $this->postToAPI('/users/get_current_account', []);
1253
        $body = $response->getDecodedBody();
1254
1255
        //Make and return the model
1256
        return new Account($body);
1257
    }
1258
1259
    /**
1260
     * Get Account
1261
     *
1262
     * @param string $account_id Account ID of the account to get details for
1263
     *
1264
     * @link https://www.dropbox.com/developers/documentation/http/documentation#users-get_account
1265
     *
1266
     * @return \Kunnu\Dropbox\Models\Account
1267
     */
1268
    public function getAccount($account_id)
1269
    {
1270
        //Get account
1271
        $response = $this->postToAPI('/users/get_account', ['account_id' => $account_id]);
1272
        $body = $response->getDecodedBody();
1273
1274
        //Make and return the model
1275
        return new Account($body);
1276
    }
1277
1278
    /**
1279
     * Get Multiple Accounts in one call
1280
     *
1281
     * @param array $account_ids IDs of the accounts to get details for
1282
     *
1283
     * @link https://www.dropbox.com/developers/documentation/http/documentation#users-get_account_batch
1284
     *
1285
     * @return \Kunnu\Dropbox\Models\AccountList
1286
     */
1287
    public function getAccounts(array $account_ids = [])
1288
    {
1289
        //Get account
1290
        $response = $this->postToAPI('/users/get_account_batch', ['account_ids' => $account_ids]);
1291
        $body = $response->getDecodedBody();
1292
1293
        //Make and return the model
1294
        return new AccountList($body);
1295
    }
1296
1297
    /**
1298
     * Get Space Usage for the current user's account
1299
     *
1300
     * @link https://www.dropbox.com/developers/documentation/http/documentation#users-get_space_usage
1301
     *
1302
     * @return array
1303
     */
1304
    public function getSpaceUsage()
1305
    {
1306
        //Get space usage
1307
        $response = $this->postToAPI('/users/get_space_usage', []);
1308
        $body = $response->getDecodedBody();
1309
1310
        //Return the decoded body
1311
        return $body;
1312
    }
1313
}
1314