You are here

private function BackupMigrateDropboxAPI::sendMessage in Backup and Migrate Dropbox 7.2

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: https://www.dropbox.com/developers/documentation/http/documentation#formats

string $command:

array|null $parameters:

string|null $content: File contents for the request.

Return value

object The json decoded response.

Throws

\RuntimeException

8 calls to BackupMigrateDropboxAPI::sendMessage()
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::_file_upload_session_append in ./backup_migrate_dropbox.dropbox_api.inc
Uploads 1 part of a multi-request upload.

... See full list

File

./backup_migrate_dropbox.dropbox_api.inc, line 306
backup_migrate_dropbox.api.inc

Class

BackupMigrateDropboxAPI
@file backup_migrate_dropbox.api.inc

Code

private function sendMessage($endpointType, $command, $parameters = null, $content = null) {

  // Prepare the request: url, headers and the body
  $url = "https://{$endpointType}.dropboxapi.com/2/{$command}";
  $headers = array();
  $headers[] = 'Content-type: ' . ($endpointType === 'api' ? 'application/json; charset=utf-8' : 'application/octet-stream');
  $headers[] = 'Authorization: Bearer ' . $this->token;
  $headers[] = 'Accept: application/json, application/octet-stream';

  // Content end points may have real content in the body and therefore expect
  // the parameters in the 'Dropbox-API-Arg' header.
  if ($endpointType === 'content' && !empty($parameters)) {
    $headers[] = 'Dropbox-API-Arg: ' . json_encode($parameters);
  }

  // Api endpoints expect the parameters in the body as a json encoded string,
  // otherwise any passed in $contents is placed in the body.
  $body = $endpointType === 'api' && !empty($parameters) ? json_encode($parameters) : $content;
  $response = $this
    ->sendHttpRequest($url, $headers, $body);
  if ($this
    ->isJsonResponse($endpointType, $command)) {
    $result = json_decode($response, FALSE);
    if ($result === NULL) {

      // No json was returned, but (probably) a plain error message. E.g, I
      // once got the string 'Incorrect host for API function
      // "files/list_folder". You must issue the request to
      // "api.dropboxapi.com".' as response.
      throw new RuntimeException("Dropbox error: {$response}");
    }
    elseif (isset($result->error_summary)) {
      throw new RuntimeException("Dropbox error: {$result->error_summary}");
    }
  }
  else {

    // Plain result: no decoding needed.
    $result = $response;
  }
  return $result;
}