Dropbox API for Delphi
The usage of Dropbox API for Delphi is simple and is similar to Dropbox for .NET and Dropbox for Java. Dropbox API for Delphi implements a set of HTTP endpoints that help your app integrate with Dropbox. The structure and members of classes from Dropbox API for Delphi helps you to easily migrate an existing .NET or Java code to Delphi.
Dropbox API utilizes JSON objects for exchanging data with connected users. The library provides a set of object types for specifying command parameters and reading responses from Dropbox service. JSON serialization is implemented by using of custom attributes and Delphi RTTI. To learn more on how it works, please check the JSON object serializer for Delphi article.
The object structure provides basic abstract service classes that allow you to implement any solution for HTTP, OAUTH and JSON functionality. You are free to implement any available solution, including open source libraries, such as Indy.
By default, we used the Internet components from Clever Internet Suite, since it provides all required features, works fast and stable and can be flexibly customized to meet individual non-standard requirements.
Download and upload files
Dropbox API for Delphi provides two levels of communication with Dropbox service: API objects and functions that are similar to standard Dropbox for .NET and Dropbox for Java libraries and a special DropboxManager object that implements simplified interface for commonly used Dropbox tasks: connect to the user's Dropbox, download and upload files, create folders, etc. in a single line of source code.
If you want more options or more flexible control when working with Dropbox service, you can use the DropboxClient API object and all related objects from the Dropbox API for Delphi library.
Manage files and folders
There are several Dropbox commands for managing files and folders on the user's Dropbox: copy, move, delete, create_folder, list_folder, etc. Each command uses its own RPC (Remote Procedure Call) end-point. To create a new folder you need to fill the request parameters, that are provided by the TCreateFolderArg object and call to the CreateFolder method of the DropboxClient's Files route:
The same command can be easily performed in a single line of source code by using of the DropboxManager object:
When you need to list the specific Dropbox folder, you need to use the ListFolder method. The DropboxClient API provides an advanced ability to manage large number of files within the folder. You can handle portions of listing by using of both ListFolder and ListFolderContinue methods. This allows you to stop file listing and provide your UI application with the progress information, etc. The TListFolderArg parameter allows you to set up the desired listing parameters:
The usage of this method is relatively complicated. You can use the simplified DropboxManager object for listing files and directories on user's Dropbox:
Save URL to Dropbox
The library presents the following two functions for saving the specific URL to the user's Dropbox: SaveUrl and SaveUrlCheckJobStatus. This command is asynchronous. The SaveUrl function initiates an asynchronous task at the Dropbox service, that downloads and stores the URL resource in a background, You can check the status of this asyncrhonous task by using of the SaveUrlCheckJobStatus method. The SaveUrl returns the async_job_id parameter, that can be used when checking the task status with the SaveUrlCheckJobStatus method:
The DropboxManager object provides the SaveUrl and SaveUrlCheckStatus methods correspondingly.
To work with Dropbox API you need first to create the DropboxClient instance and set up the OAuth credentials. Your application must send an OAuth 2.0 token with any request that accesses private user data. Open your Dropbox My apps section and set up the desired App name, App key, secret and Redirect URIs that are necessary to generate OAuth 2.0 credentials for your application. The Dropbox Service initialization code may look like the following:
In case of using of the DropboxManager object, the process of initialization is implemented in three lines of code:
Supported compiler versions
Dropbox API for Delphi can be used in RAD Studio XE3 and later. If you modify the sources and remove all references to the RAD Studio namespaces in the 'uses' sections, you can use the library in RAD Studio 2009, 2010, XE and XE2 as well.
Download source code
The current version of Dropbox API for Delphi needs for the non-free library Clever Internet Suite. This is a drawback, and we suggest the task of changing the program so that it does the same job without the non-free library. Anyone who thinks of doing substantial further work on the program, first may free it from dependence on the non-free library. The class structure allows you to easily replace the Clever Internet Suite Http and Oauth components with any other third-party free and non-free software.
The current Dropbox API for Delphi implementation supports only the Auth and Files routes of the Dropbox API v2 interface. The following routes are not yet implemented: Paper, Sharing and Users. Also, the following commands are implemented for the Files route: Copy, CreateFolder, Delete, Download, ListFolder, ListFolderContinue, Move, SaveUrl, SaveUrlCheckJobStatus, Search and Upload. We still are working on this library. The updated version will be available in the next article.