public final class DbxClient extends Object
Use this class to make remote calls to the Dropbox API. You'll need an access token first,
normally acquired via DbxWebAuth.
This class has no mutable state, so it's thread safe as long as you pass in a thread safe
HttpRequestor implementation.
| Modifier and Type | Class and Description |
|---|---|
static class |
DbxClient.Downloader
A pairing of the file content's metadata and an
InputStream to read the
file content. |
static class |
DbxClient.Uploader
For uploading file content to Dropbox.
|
| Constructor and Description |
|---|
DbxClient(DbxRequestConfig requestConfig,
String accessToken) |
DbxClient(DbxRequestConfig requestConfig,
String accessToken,
DbxHost host)
The same as
DbxClient(DbxRequestConfig, String) except you can also set the
hostnames of the Dropbox API servers. |
| Modifier and Type | Method and Description |
|---|---|
<T> T |
doPost(String host,
String path,
String[] params,
ArrayList<HttpRequestor.Header> headers,
DbxRequestUtil.ResponseHandler<T> handler) |
String |
getAccessToken()
Returns the
DbxAccessToken that was passed in to the constructor. |
DbxAccountInfo |
getAccountInfo()
Retrieve the user's account information.
|
DbxDelta<DbxEntry> |
getDelta(String cursor)
Return "delta" entries for the contents of a user's Dropbox.
|
<C> DbxDeltaC<C,DbxEntry> |
getDeltaC(String cursor,
Collector<DbxDeltaC.Entry<DbxEntry>,C> collector)
This is a more generic version of
getDelta(java.lang.String). |
DbxEntry.File |
getFile(String path,
String revision,
OutputStream target)
Retrieves a file's content and writes it to the given
OutputStream. |
DbxEntry |
getMetadata(String path)
Get the file or folder metadata for a given path.
|
DbxEntry.WithChildren |
getMetadataWithChildren(String path)
Get the metadata for a given path; if the path refers to a folder,
get all the children's metadata as well.
|
<C> DbxEntry.WithChildrenC<C> |
getMetadataWithChildrenC(String path,
Collector<DbxEntry,? extends C> collector)
Same as
getMetadataWithChildren(java.lang.String) except instead of always returning a list of
DbxEntry objects, you specify a Collector that processes the DbxEntry
objects one by one and aggregates them however you want. |
Maybe<DbxEntry.WithChildren> |
getMetadataWithChildrenIfChanged(String path,
String previousFolderHash)
Get the metadata for a given path and its children if anything has
changed since the last time you got them (as determined by the value
of
DbxEntry.WithChildren.hash from the last result). |
<C> Maybe<DbxEntry.WithChildrenC<C>> |
getMetadataWithChildrenIfChangedC(String path,
String previousFolderHash,
Collector<DbxEntry,? extends C> collector)
Same as
getMetadataWithChildrenIfChanged(java.lang.String, java.lang.String) except instead of always returning a list of
DbxEntry objects, you specify a Collector that processes the DbxEntry
objects one by one and aggregates them however you want. |
DbxRequestConfig |
getRequestConfig()
Returns the
DbxRequestConfig that was passed in to the constructor. |
DbxEntry.File |
getThumbnail(DbxThumbnailSize size,
DbxThumbnailFormat format,
String path,
String revision,
OutputStream target) |
DbxClient.Downloader |
startGetFile(String path,
String revision)
Retrieve a file's content and content metadata.
|
DbxClient.Downloader |
startGetThumbnail(DbxThumbnailSize size,
DbxThumbnailFormat format,
String path,
String revision) |
DbxClient.Uploader |
startUploadFile(String targetPath,
DbxWriteMode writeMode,
long numBytes)
Start an API request to upload a file to Dropbox.
|
<E extends Throwable> |
uploadFile(String targetPath,
DbxWriteMode writeMode,
long numBytes,
DbxStreamWriter<E> writer)
Upload file contents to Dropbox, getting contents from the given
DbxStreamWriter. |
DbxEntry.File |
uploadFile(String targetPath,
DbxWriteMode writeMode,
long numBytes,
InputStream contents)
A wrapper around
uploadFile(String, DbxWriteMode, long, DbxStreamWriter) that
lets you pass in an InputStream. |
public DbxClient(DbxRequestConfig requestConfig, String accessToken)
accessToken - The OAuth 2 access token (that you got from Dropbox) that gives your app the ability
to make Dropbox API calls against some particular user's account. The standard way
to get one of these is to use DbxWebAuth to send your user through Dropbox's
OAuth 2 authorization flow.public DbxClient(DbxRequestConfig requestConfig, String accessToken, DbxHost host)
DbxClient(DbxRequestConfig, String) except you can also set the
hostnames of the Dropbox API servers. This is used in testing. You don't normally need
to call this.public DbxRequestConfig getRequestConfig()
DbxRequestConfig that was passed in to the constructor.public String getAccessToken()
DbxAccessToken that was passed in to the constructor.public DbxEntry getMetadata(String path) throws DbxException
DbxClient dbxClient = ...
DbxEntry entry = dbxClient.getMetadata("/Photos");
if (entry == null) {
System.out.println("No file or folder at that path.");
} else {
System.out.print(entry.toStringMultiline());
}
path - The path to the file or folder (see DbxPath).null.DbxExceptionpublic DbxEntry.WithChildren getMetadataWithChildren(String path) throws DbxException
DbxClient dbxClient = ...
DbxEntry entry = dbxClient.getMetadata("/Photos");
if (entry == null) {
System.out.println("No file or folder at that path.");
} else {
System.out.print(entry.toStringMultiline());
}
path - The path (starting with "/") to the file or folder (see DbxPath).null.
Otherwise, return the metadata for that path and the metadata for all its immediate
children (if it's a folder).DbxExceptionpublic <C> DbxEntry.WithChildrenC<C> getMetadataWithChildrenC(String path, Collector<DbxEntry,? extends C> collector) throws DbxException
getMetadataWithChildren(java.lang.String) except instead of always returning a list of
DbxEntry objects, you specify a Collector that processes the DbxEntry
objects one by one and aggregates them however you want.
This allows your to process the DbxEntry values as they arrive, instead of having to
wait for the entire API call to finish before processing the first one. Be careful, though,
because the API call may fail in the middle (after you've already processed some entries).
Make sure your code can handle that situation. For example, if you're inserting stuff into a
database as they arrive, you might want do everything in a transaction and commit only if
the entire call succeeds.
DbxExceptionpublic Maybe<DbxEntry.WithChildren> getMetadataWithChildrenIfChanged(String path, String previousFolderHash) throws DbxException
DbxEntry.WithChildren.hash from the last result).path - The path (starting with "/") to the file or folder (see DbxPath).previousFolderHash - The value of DbxEntry.WithChildren.hash from the last time
you got the metadata for this folder (and children).null. If the folder at the given path hasn't changed
since you last retrieved it (i.e. its contents match previousFolderHash), return
Maybe.Nothing. If it doesn't match previousFolderHash return either
Maybe.Just(null) if there's nothing there or Maybe.Just with the
metadata.DbxExceptionpublic <C> Maybe<DbxEntry.WithChildrenC<C>> getMetadataWithChildrenIfChangedC(String path, String previousFolderHash, Collector<DbxEntry,? extends C> collector) throws DbxException
getMetadataWithChildrenIfChanged(java.lang.String, java.lang.String) except instead of always returning a list of
DbxEntry objects, you specify a Collector that processes the DbxEntry
objects one by one and aggregates them however you want.
This allows your to process the DbxEntry values as they arrive, instead of having to
wait for the entire API call to finish before processing the first one. Be careful, though,
because the API call may fail in the middle (after you've already processed some entries).
Make sure your code can handle that situation. For example, if you're inserting stuff into a
database as they arrive, you might want do everything in a transaction and commit only if
the entire call succeeds.
DbxExceptionpublic DbxAccountInfo getAccountInfo() throws DbxException
DbxExceptionpublic DbxEntry.File getFile(String path, String revision, OutputStream target) throws DbxException, IOException
OutputStream.
DbxClient dbxClient = ...
DbxEntry.File md;
File target = new File("Copy of House.jpeg");
OutputStream out = new FileOutputStream(target);
try {
md = dbxClient.getFile("/Photos/House.jpeg", out);
}
finally {
out.close();
}
revision - The DbxEntry.File.revisionrevision} of the file to retrieve,
or null if you want the latest revision of the file.IOException - If there's an error writing to target.DbxExceptionpublic DbxClient.Downloader startGetFile(String path, String revision) throws DbxException
DbxClient.Downloader
which is just an InputStream (can be used to read the file contents) and
a DbxEntry.File (the file's metadata).
You need to close the DbxClient.Downloader yourself.
Use a try/finally to make sure you close it in all cases.
DbxClient dbxClient = ...
DbxClient.Downloader downloader = dbxClient.getFileStart("/ReadMe.txt")
try {
printStream(downloader.body)
}
finally {
downloader.close()
}
revision - The DbxEntry.File.revisionrevision} of the file to retrieve,
or null if you want the latest revision of the file.path - The path (starting with "/") to the file or folder on Dropbox.
(see DbxPath).DbxExceptionpublic DbxEntry.File uploadFile(String targetPath, DbxWriteMode writeMode, long numBytes, InputStream contents) throws DbxException, IOException
uploadFile(String, DbxWriteMode, long, DbxStreamWriter) that
lets you pass in an InputStream. The entire stream contents will
be uploaded and the stream will be closed automatically (whether or not the upload
succeeds or fails).
DbxClient dbxClient = ...
File f = new File("ReadMe.txt")
dbxClient.uploadFile("/ReadMe.txt", DbxWriteMode.add(), f.length(), new FileInputStream(f))
targetPath - The path to the file on Dropbox (see DbxPath). If a file at
that path already exists on Dropbox, then the writeMode parameter
will determine what happens.writeMode - Determines what to do if there's already a file at the given targetPath.numBytes - The number of bytes in the given stream. Use -1 if you don't know.contents - The source of file contents. This stream will be automatically closed (whether or not the
upload succeeds).IOException - If there's an error reading from in.DbxExceptionpublic <E extends Throwable> DbxEntry.File uploadFile(String targetPath, DbxWriteMode writeMode, long numBytes, DbxStreamWriter<E> writer) throws DbxException, E extends Throwable
DbxStreamWriter.
DbxClient dbxClient = ...
// Create a file on Dropbox with 100 3-digit random numbers, one per line.
final int numRandoms = 100;
int fileSize = numRandoms * 4; 3 digits, plus a newline
dbxClient.uploadFile("/Randoms.txt", DbxWriteMode.add(), fileSize,
new DbxStreamWriter<RuntimeException>() {
public void write(OutputStream out) throws IOException
{
Random rand = new Random();
PrintWriter pw = new PrintWriter(out);
for (int i = 0; i < numRandoms; i++) {
pw.printf("%03d\n", rand.nextInt(1000));
}
pw.flush();
}
});
targetPath - The path to the file on Dropbox (see DbxPath). If a file at
that path already exists on Dropbox, then the writeMode parameter
will determine what happens.writeMode - Determines what to do if there's already a file at the given targetPath.numBytes - The number of bytes you're going to upload via the returned DbxClient.Uploader.
Use -1 if you don't know ahead of time.writer - A callback that will be called when it's time to actually write out the
body of the file. We will always call DbxStreamWriter.close() on it (whether
or not the upload succeeds) so you can put resource cleanup code in it (for example,
closing an InputStream).E - If writer.write() throws an exception, it will propagate out of this function.DbxExceptionE extends Throwablepublic DbxClient.Uploader startUploadFile(String targetPath, DbxWriteMode writeMode, long numBytes) throws DbxException
DbxClient.Uploader object
that lets you actually send the file contents via DbxClient.Uploader.body. When
you're done copying the file body, call DbxClient.Uploader.finish().
You need to close the DbxClient.Uploader when you're done with it.
Use a try/finally to make sure you close it in all cases.
DbxClient dbxClient = ...
DbxClient.Uploader uploader = dbxClient.startUploadFile(...)
DbxEntry.File md;
try {
writeMyData(uploader.body);
md = uploader.finish();
}
finally {
uploader.close();
}
targetPath - The path to the file on Dropbox (see DbxPath). If a file at
that path already exists on Dropbox, then the writeMode parameter
will determine what happens.writeMode - Determines what to do if there's already a file at the given targetPath.numBytes - The number of bytes you're going to upload via the returned DbxClient.Uploader.
Use -1 if you don't know ahead of time.DbxExceptionpublic DbxDelta<DbxEntry> getDelta(String cursor) throws DbxException
DbxDelta for more documentation on what each entry means.
To start, pass in null for cursor. For subsequent calls
To get the next set of delta entries, pass in the cursor returned
by the previous call.
To catch up to the current state, keep calling this method until the returned
object's hasMore field is false.
If your app is a "Full Dropbox" app, this will return all entries for the user's entire Dropbox folder. If your app is an "App Folder" app, this will only return entries for the App Folder's contents.
DbxExceptionpublic <C> DbxDeltaC<C,DbxEntry> getDeltaC(String cursor, Collector<DbxDeltaC.Entry<DbxEntry>,C> collector) throws DbxException
getDelta(java.lang.String). It allows you to specify
a collector, which lets you process the delta entries as they arrive over
the network, and aggregate them however you want.DbxExceptionpublic DbxEntry.File getThumbnail(DbxThumbnailSize size, DbxThumbnailFormat format, String path, String revision, OutputStream target) throws DbxException, IOException
DbxExceptionIOExceptionpublic DbxClient.Downloader startGetThumbnail(DbxThumbnailSize size, DbxThumbnailFormat format, String path, String revision) throws DbxException
DbxExceptionpublic <T> T doPost(String host, String path, String[] params, ArrayList<HttpRequestor.Header> headers, DbxRequestUtil.ResponseHandler<T> handler) throws DbxException
DbxExceptionCopyright © 2013. All rights reserved.