private function BackupMigrateDropboxAPI::send_message in Backup and Migrate Dropbox 7.3
Sends a request to Dropbox and returns the response.
Parameters
string $endpointType: This type determines the url to use and how to process parameters. It can be one of:
- 'api'
- 'content'
More info about end points can be found at: {@link https://www.dropbox.com/developers/documentation/http/documentation#formats}
string $command: The Dropbox API function to call. Will be used as the path part of the url.
array|null $parameters: The parameters for this command. Will be send in the body or as a header.
string|null $content: File contents for the request. Will be send in the body.
Return value
object|string The response form the Dropbox Api. If the response was a json encoded object, that object will be returned, otherwise the unaltered response will be returned, e.g. the contents of a file.
Throws
RuntimeException Excerpt from the Dropbox documentation on error handling:
- 400 Bad input parameter. The response body is a plaintext message with more information.
- 401 Bad or expired token. This can happen if the access token is expired or if the access token has been revoked by Dropbox or the user. To fix this, you should re-authenticate the user. The Content-Type of the response is JSON of typeAuthError.
- 403 The user or team account doesn't have access to the endpoint or feature. The Content-Type of the response is JSON of typeAccessError.
- 409 Endpoint-specific error. Look to the JSON response body for the specifics of the error.
- 429 Your app is making too many requests for the given user or team and is being rate limited. Your app should wait for the number of seconds specified in the "Retry-After" response header before trying again. The Content-Type of the response can be JSON or plaintext. If it is JSON, it will be typeRateLimitError. You can find more information in the data ingress guide.
- 5xx An error occurred on the Dropbox servers. Check status.dropbox.com for announcements about Dropbox service issues.
{
See also
https://www.dropbox.com/developers/documentation/http/documentation#erro...}.
10 calls to BackupMigrateDropboxAPI::send_message()
- BackupMigrateDropboxAPI::create_folder in ./
backup_migrate_dropbox.dropbox_api.inc - Creates a folder on Dropbox.
- BackupMigrateDropboxAPI::file_delete in ./
backup_migrate_dropbox.dropbox_api.inc - Deletes the file at the given $path.
- BackupMigrateDropboxAPI::file_download in ./
backup_migrate_dropbox.dropbox_api.inc - Downloads the file from the given Dropbox $path.
- BackupMigrateDropboxAPI::list_folder in ./
backup_migrate_dropbox.dropbox_api.inc - Returns the contents of a Dropbox folder.
- BackupMigrateDropboxAPI::obtain_refresh_token in ./
backup_migrate_dropbox.dropbox_api.inc - Obtains a first bearer and a refresh token.
File
- ./
backup_migrate_dropbox.dropbox_api.inc, line 558
Class
- BackupMigrateDropboxAPI
- BackupMigrateDropboxAPI contains all the details about the Dropbox api, authorization calls, endpoints, uris, parameters, error handling, and split requests for large uploads/downloads
Code
private function send_message($endpointType, $command, $parameters = null, $content = null) {
// Prepare the request: url, headers and the body.
$headers = [];
$headers[] = 'Accept: application/json, application/octet-stream';
if ($command === 'oauth2/token') {
// The oauth2/token process actually belongs to a 3rd endpoint type,
// having a different URL and content-type (and therefore body encoding).
$headers[] = 'Content-type: application/x-www-form-urlencoded';
$url = "https://{$endpointType}.dropbox.com/{$command}";
$body = http_build_query($parameters);
}
else {
$url = "https://{$endpointType}.dropboxapi.com/2/{$command}";
$headers[] = 'Authorization: Bearer ' . $this
->get_bearer_token();
if ($endpointType === 'content') {
// Content end points (may) have real content in the body and therefore
// expect the parameters in the 'Dropbox-API-Arg' header.
$headers[] = 'Content-type: application/octet-stream';
if (!empty($parameters)) {
$headers[] = 'Dropbox-API-Arg: ' . json_encode($parameters);
}
$body = $content;
}
else {
$headers[] = 'Content-type: application/json; charset=utf-8';
$body = json_encode($parameters);
}
}
$http_result = $this
->send_http_request($url, $headers, $body);
//$this->log($http_result); // enable during development, or in case of problems.
if ($http_result->code === 200) {
if ($this
->is_json_response($endpointType, $command)) {
$result = json_decode($http_result->body);
// Not sure if errors can be returned with a 200 or if that is only done
// with a 4xx, to be sure I just check for the error_summary field.
if (isset($result->error_summary)) {
$message = $this
->get_dropbox_error_message($http_result->body);
throw new RuntimeException("Dropbox error: {$message}");
}
}
else {
// Plain result, e.g. file contents: no decoding needed.
$result = $http_result->body;
}
}
elseif ($http_result->code === 401) {
$error_message = json_decode($http_result->body)->error_summary;
if (strpos($error_message, 'expired_access_token') !== FALSE && !$this->is_resending_after_refresh) {
try {
$this->is_resending_after_refresh = TRUE;
$this
->refresh_bearer_token();
$result = $this
->send_message($endpointType, $command, $parameters, $content);
} finally {
$this->is_resending_after_refresh = FALSE;
}
}
else {
throw new RuntimeException("Dropbox authentication error: {$error_message}", $http_result->code);
}
}
elseif ($http_result->code < 500) {
$message = $this
->get_dropbox_error_message($http_result->body);
throw new RuntimeException("Dropbox error: {$message}", $http_result->code);
}
else {
$message = 'An error occurred on the Dropbox servers. Check https://status.dropbox.com/ for announcements about Dropbox service issues.';
$body_message = $this
->get_dropbox_error_message($http_result->body);
if (!empty($body_message)) {
$message .= " Details: {$body_message}";
}
throw new RuntimeException($message, $http_result->code);
}
return $result;
}