Skip to content

Commit 2973c84

Browse files
author
Kannan Goundan
committed
Add support for /delta's new "path_prefix" parameter.
1 parent 1496e43 commit 2973c84

3 files changed

Lines changed: 108 additions & 24 deletions

File tree

ChangeLog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
certificates (instead of using the system defaults).
33
- Add DbxOAuth1Upgrader, which upgrades existing OAuth 1 access tokens
44
to OAuth 2 access tokens.
5+
- Add support for /delta's new "path_prefix" parameter.
56

67
---------------------------------------------
78
1.7.5 (2013-09-16)

src/com/dropbox/core/DbxClient.java

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,11 +1363,61 @@ public IODbxException(DbxException underlying)
13631363
*/
13641364
public DbxDelta<DbxEntry> getDelta(String cursor)
13651365
throws DbxException
1366+
{
1367+
return _getDelta(cursor, null);
1368+
}
1369+
1370+
/**
1371+
* A more generic version of {@link #getDelta}. You provide a <em>collector</em>,
1372+
* which lets you process the delta entries as they arrive over the network.
1373+
*/
1374+
public <C> DbxDeltaC<C> getDeltaC(Collector<DbxDeltaC.Entry<DbxEntry>, C> collector, String cursor)
1375+
throws DbxException
1376+
{
1377+
return _getDeltaC(collector, cursor, null);
1378+
}
1379+
1380+
/**
1381+
* Same as {@link #getDelta}, except results are limited to files and folders whose
1382+
* paths are equal to or under the specified {@code pathPrefix}.
1383+
*
1384+
* <p>
1385+
* The {@code pathPrefix} is fixed for a given cursor. Whatever {@code pathPrefix}
1386+
* you use on the first call to {@code getDelta()} must also be passed in on
1387+
* subsequent calls that use the returned cursor.
1388+
* </p>
1389+
*
1390+
* @param pathPrefix
1391+
* A path on Dropbox to limit results to.
1392+
*/
1393+
public DbxDelta<DbxEntry> getDeltaWithPathPrefix(String cursor, String pathPrefix)
1394+
throws DbxException
1395+
{
1396+
DbxPath.checkArg("path", pathPrefix);
1397+
return _getDelta(cursor, pathPrefix);
1398+
}
1399+
1400+
/**
1401+
* A more generic version of {@link #getDeltaWithPathPrefix}. You provide a <em>collector</em>,
1402+
* which lets you process the delta entries as they arrive over the network.
1403+
*/
1404+
public <C> DbxDeltaC<C> getDeltaCWithPathPrefix(Collector<DbxDeltaC.Entry<DbxEntry>, C> collector, String cursor, String pathPrefix)
1405+
throws DbxException
1406+
{
1407+
DbxPath.checkArg("path", pathPrefix);
1408+
return _getDeltaC(collector, cursor, pathPrefix);
1409+
}
1410+
1411+
private DbxDelta<DbxEntry> _getDelta(String cursor, String pathPrefix)
1412+
throws DbxException
13661413
{
13671414
String host = this.host.api;
13681415
String apiPath = "1/delta";
13691416

1370-
String[] params = {"cursor", cursor};
1417+
String[] params = {
1418+
"cursor", cursor,
1419+
"path_prefix", pathPrefix,
1420+
};
13711421

13721422
return doPost(host, apiPath, params, null, new DbxRequestUtil.ResponseHandler<DbxDelta<DbxEntry>>() {
13731423
@Override
@@ -1379,18 +1429,18 @@ public DbxDelta<DbxEntry> handle(HttpRequestor.Response response) throws DbxExce
13791429
});
13801430
}
13811431

1382-
/**
1383-
* This is a more generic version of {@link #getDelta}. It allows you to specify
1384-
* a <em>collector</em>, which lets you process the delta entries as they arrive over
1385-
* the network, and aggregate them however you want.
1386-
*/
1387-
public <C> DbxDeltaC<C> getDeltaC(String cursor, final Collector<DbxDeltaC.Entry<DbxEntry>, C> collector)
1432+
private <C> DbxDeltaC<C> _getDeltaC(final Collector<DbxDeltaC.Entry<DbxEntry>, C> collector, String cursor, String pathPrefix)
13881433
throws DbxException
13891434
{
13901435
String host = this.host.api;
13911436
String apiPath = "1/delta";
13921437

1393-
return doPost(host, apiPath, new String[]{"cursor", cursor}, null, new DbxRequestUtil.ResponseHandler<DbxDeltaC<C>>() {
1438+
String[] params = {
1439+
"cursor", cursor,
1440+
"path_prefix", pathPrefix,
1441+
};
1442+
1443+
return doPost(host, apiPath, params, null, new DbxRequestUtil.ResponseHandler<DbxDeltaC<C>>() {
13941444
@Override
13951445
public DbxDeltaC<C> handle(HttpRequestor.Response response) throws DbxException {
13961446
if (response.statusCode != 200) throw DbxRequestUtil.unexpectedStatus(response);

test/com/dropbox/core/DbxClientTest.java

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -190,25 +190,58 @@ public void testDelta()
190190
{
191191
init();
192192

193-
// Eat up all the deltas up to the current point.
194-
DbxDelta<DbxEntry> d = client.getDelta(null);
195-
assertTrue(d.reset);
196-
while (d.hasMore) {
197-
d = client.getDelta(d.cursor);
198-
}
199-
200-
String path = p("make a delta.txt");
201-
addFile(path, 100);
193+
DbxEntry.Folder top = (DbxEntry.Folder) client.getMetadata(p());
194+
DbxEntry.File a = addFile(p("a.txt"), 10);
195+
DbxEntry.Folder b = client.createFolder(p("b"));
196+
DbxEntry.File b_1 = addFile(p("b/1.txt"), 20);
197+
DbxEntry.File b_2 = addFile(p("b/2.txt"), 30);
198+
DbxEntry.Folder c = client.createFolder(p("b/c"));
199+
DbxEntry.File c_1 = addFile(p("b/c/1.txt"), 40);
200+
DbxEntry.File c_2 = addFile(p("b/c/2.txt"), 50);
201+
202+
// getDelta
203+
{
204+
HashSet<DbxEntry> expected = new HashSet<DbxEntry>(Arrays.asList(top, a, b, b_1, b_2, c, c_1, c_2));
205+
206+
String lcPrefix = p().toLowerCase();
207+
String cursor = null;
208+
while (true) {
209+
DbxDelta<DbxEntry> d = client.getDelta(cursor);
210+
for (DbxDelta.Entry<DbxEntry> e : d.entries) {
211+
if (e.lcPath.startsWith(lcPrefix+"/") || e.lcPath.equals(lcPrefix)) {
212+
assertNotNull(e.metadata); // We shouldn't see deletes in our test folder.
213+
boolean removed = expected.remove(e.metadata);
214+
assertTrue(removed);
215+
}
216+
}
217+
cursor = d.cursor;
218+
if (!d.hasMore) break;
219+
}
202220

203-
d = client.getDelta(d.cursor);
204-
assertEquals(d.entries.size(), 1);
221+
assertEquals(expected.size(), 0);
222+
}
205223

206-
DbxDelta.Entry<DbxEntry> deltaEntry = d.entries.get(0);
207-
assertEquals(deltaEntry.lcPath, path.toLowerCase());
224+
// getDeltaWithPathPrefix
225+
{
226+
HashSet<DbxEntry> expected = new HashSet<DbxEntry>(Arrays.asList(b, b_1, b_2, c, c_1, c_2));
227+
228+
String prefix = b.path;
229+
String lcPrefix = prefix.toLowerCase();
230+
String cursor = null;
231+
while (true) {
232+
DbxDelta<DbxEntry> d = client.getDeltaWithPathPrefix(cursor, prefix);
233+
for (DbxDelta.Entry<DbxEntry> e : d.entries) {
234+
assertTrue(e.lcPath.startsWith(lcPrefix+"/") || e.lcPath.equals(lcPrefix));
235+
assertNotNull(e.metadata); // We should never see deletes.
236+
boolean removed = expected.remove(e.metadata);
237+
assertTrue(removed);
238+
}
239+
cursor = d.cursor;
240+
if (!d.hasMore) break;
241+
}
208242

209-
DbxEntry.File md = deltaEntry.metadata.asFile();
210-
assertEquals(md.path, path);
211-
assertEquals(md.numBytes, 100);
243+
assertEquals(expected.size(), 0);
244+
}
212245
}
213246

214247
@Test

0 commit comments

Comments
 (0)