From c6f7a08490e72468cfc4e12a381d1abedaf93ec2 Mon Sep 17 00:00:00 2001 From: Mr Rogers Date: Sat, 4 Mar 2017 12:31:34 -0800 Subject: [PATCH 1/2] Add a list_all_files method to client * uses the files/list_folder/continue endpoint with a cursor * replaces `/delta` from v1 - https://www.dropbox.com/developers/reference/migration-guide --- lib/dropbox/client.rb | 25 ++++++++++++- test/stubs/recursive_folder_contents.json | 33 +++++++++++++++++ .../recursive_folder_contents_part_2.json | 37 +++++++++++++++++++ .../recursive_folder_contents_part_3.json | 37 +++++++++++++++++++ test/test_files.rb | 21 +++++++++++ 5 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 test/stubs/recursive_folder_contents.json create mode 100644 test/stubs/recursive_folder_contents_part_2.json create mode 100644 test/stubs/recursive_folder_contents_part_3.json diff --git a/lib/dropbox/client.rb b/lib/dropbox/client.rb index 2d80c2d..4ddb428 100644 --- a/lib/dropbox/client.rb +++ b/lib/dropbox/client.rb @@ -122,15 +122,38 @@ def get_thumbnail(path, format='jpeg', size='w64h64') return FileMetadata.new(resp), body end + def _list_folder(path, opts = nil) + args = { path: path }.merge(opts || {}) + request('/files/list_folder', args) + end + # Get the contents of a folder. # # @param [String] path # @return [Array] def list_folder(path) - resp = request('/files/list_folder', path: path) + resp = _list_folder(path) resp['entries'].map { |e| parse_tagged_response(e) } end + # Get all files (recursively) from a folder + # + # @param [String] path + # @return [Array] + def list_all_files(path) + resp = _list_folder(path, recursive: true) + has_more = resp['has_more'] + cursor = resp['cursor'] + entries = resp['entries'] + while has_more && !cursor.nil? + resp = request('/files/list_folder/continue', cursor: cursor) + has_more = resp['has_more'] + cursor = resp['cursor'] + entries += resp['entries'] + end + entries.map { |e| parse_tagged_response(e) if e['.tag'] == 'file' }.compact + end + # Get the contents of a folder that are after a cursor. # # @param [String] cursor diff --git a/test/stubs/recursive_folder_contents.json b/test/stubs/recursive_folder_contents.json new file mode 100644 index 0000000..f7e82d7 --- /dev/null +++ b/test/stubs/recursive_folder_contents.json @@ -0,0 +1,33 @@ +HTTP/1.1 200 OK +Content-Type: application/json + +{ + "entries": [ + { + ".tag": "folder", + "name": "math", + "id": "id:a4ayc_80_OEAAAAAAAAAXz", + "path_lower": "/homework/math", + "path_display": "/Homework/math", + "sharing_info": { + "read_only": false, + "parent_shared_folder_id": "84528192421", + "traverse_only": false, + "no_access": false + }, + "property_groups": [ + { + "template_id": "ptid:1a5n2i6d3OYEAAAAAAAAAYa", + "fields": [ + { + "name": "Security Policy", + "value": "Confidential" + } + ] + } + ] + } + ], + "cursor": "PART1_CURSOR", + "has_more": true +} diff --git a/test/stubs/recursive_folder_contents_part_2.json b/test/stubs/recursive_folder_contents_part_2.json new file mode 100644 index 0000000..4d977b3 --- /dev/null +++ b/test/stubs/recursive_folder_contents_part_2.json @@ -0,0 +1,37 @@ +HTTP/1.1 200 OK +Content-Type: application/json + +{ + "entries": [ + { + ".tag": "file", + "name": "Prime_Numbers.txt", + "id": "id:a4ayc_80_OEAAAAAAAAAXw", + "client_modified": "2015-05-12T15:50:38Z", + "server_modified": "2015-05-12T15:50:38Z", + "rev": "a1c10ce0dd78", + "size": 7212, + "path_lower": "/homework/math/prime_numbers.txt", + "path_display": "/Homework/math/Prime_Numbers.txt", + "sharing_info": { + "read_only": true, + "parent_shared_folder_id": "84528192421", + "modified_by": "dbid:AAH4f99T0taONIb-OurWxbNQ6ywGRopQngc" + }, + "property_groups": [ + { + "template_id": "ptid:1a5n2i6d3OYEAAAAAAAAAYa", + "fields": [ + { + "name": "Security Policy", + "value": "Confidential" + } + ] + } + ], + "has_explicit_shared_members": false + } + ], + "cursor": "PART2_CURSOR", + "has_more": true +} diff --git a/test/stubs/recursive_folder_contents_part_3.json b/test/stubs/recursive_folder_contents_part_3.json new file mode 100644 index 0000000..e71c6dd --- /dev/null +++ b/test/stubs/recursive_folder_contents_part_3.json @@ -0,0 +1,37 @@ +HTTP/1.1 200 OK +Content-Type: application/json + +{ + "entries": [ + { + ".tag": "file", + "name": "Irrational_Numbers.txt", + "id": "id:a4ayc_80_OEAAAAAAAAAXZ", + "client_modified": "2015-05-12T15:50:38Z", + "server_modified": "2015-05-12T15:50:38Z", + "rev": "a1c10ce0dd79", + "size": 7200, + "path_lower": "/homework/math/irrational_numbers.txt", + "path_display": "/Homework/math/Irrational_Numbers.txt", + "sharing_info": { + "read_only": true, + "parent_shared_folder_id": "84528192421", + "modified_by": "dbid:AAH4f99T0taONIb-OurWxbNQ6ywGRopQngd" + }, + "property_groups": [ + { + "template_id": "ptid:1a5n2i6d3OYEAAAAAAAAAYa", + "fields": [ + { + "name": "Security Policy", + "value": "Confidential" + } + ] + } + ], + "has_explicit_shared_members": false + } + ], + "cursor": "ZZZZZ_EHj3x7PMkVuFIhwKYXEpwpLwyxp9vMKomUhllil9q7eWiAB", + "has_more": false +} diff --git a/test/test_files.rb b/test/test_files.rb index 655896b..17bd699 100644 --- a/test/test_files.rb +++ b/test/test_files.rb @@ -161,6 +161,27 @@ def test_list_folder assert_equal 'math', entries[1].name end + def test_list_all_files + stub_request(:post, url('files/list_folder')) + .with(body: { recursive: true, path: '/Homework/math' } ) + .to_return(stub('recursive_folder_contents')) + stub_request(:post, url('files/list_folder/continue')) + .with(body: { cursor: "PART1_CURSOR" }) + .to_return(stub('recursive_folder_contents_part_2')) + stub_request(:post, url('files/list_folder/continue')) + .with(body: { cursor: "PART2_CURSOR" }) + .to_return(stub('recursive_folder_contents_part_3')) + + entries = @client.list_all_files('/Homework/math') + + assert_equal 2, entries.length + assert entries[0].is_a?(Dropbox::FileMetadata) + assert_equal 'Prime_Numbers.txt', entries[0].name + assert_equal 7212, entries[0].size + assert_equal 'Irrational_Numbers.txt', entries[1].name + assert_equal 7200, entries[1].size + end + def test_list_folder_empty stub_request(:post, url('files/list_folder')).to_return(stub('empty_folder_contents')) entries = @client.list_folder('/empty_folder') From 1c75167dd628c5fd1df6ee95d0c2228a4608fc83 Mon Sep 17 00:00:00 2001 From: Mr Rogers Date: Sat, 4 Mar 2017 12:39:09 -0800 Subject: [PATCH 2/2] move back to multiple request calls --- lib/dropbox/client.rb | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/dropbox/client.rb b/lib/dropbox/client.rb index 4ddb428..74427f3 100644 --- a/lib/dropbox/client.rb +++ b/lib/dropbox/client.rb @@ -122,17 +122,12 @@ def get_thumbnail(path, format='jpeg', size='w64h64') return FileMetadata.new(resp), body end - def _list_folder(path, opts = nil) - args = { path: path }.merge(opts || {}) - request('/files/list_folder', args) - end - # Get the contents of a folder. # # @param [String] path # @return [Array] def list_folder(path) - resp = _list_folder(path) + resp = request('/files/list_folder', path: path) resp['entries'].map { |e| parse_tagged_response(e) } end @@ -141,7 +136,7 @@ def list_folder(path) # @param [String] path # @return [Array] def list_all_files(path) - resp = _list_folder(path, recursive: true) + resp = request('/files/list_folder', path: path, recursive: true) has_more = resp['has_more'] cursor = resp['cursor'] entries = resp['entries']