var request = require('superagent');
var Promise = require('es6-promise').Promise;

var BASE_URL = 'https://api.dropboxapi.com/2/';

// This doesn't match what was spec'd in paper doc yet
var buildCustomError = function (error, response) {
  return {
    status: error.status,
    error: response.text,
    response: response
  };
};

var rpcRequest = function (path, body, accessToken, selectUser) {
  var promiseFunction = function (resolve, reject) {
    var apiRequest;

    function success(data) {
      if (resolve) {
        resolve(data);
      }
    }

    function failure(error) {
      if (reject) {
        reject(error);
      }
    }

    function responseHandler(error, response) {
      if (error) {
        failure(buildCustomError(error, response));
      } else {
        success(response.body);
      }
    }

    apiRequest = request.post(BASE_URL + path)
      .type('application/json')
      .set('Authorization', 'Bearer ' + accessToken);

    if (selectUser) {
      apiRequest = apiRequest.set('Dropbox-API-Select-User', selectUser);
    }

    apiRequest.send(body)
      .end(responseHandler);
  };

  return new Promise(promiseFunction);
};

module.exports = rpcRequest;

@@ -817,32 +817,169 @@
 %0A      %7D%0A    %7D%0A%0A
+    // The API expects null to be passed for endpoints that dont accept any%0A    // parameters%0A    if (!body) %7B%0A      body = null;%0A    %7D%0A%0A
     apiRequest =

Set request body to null if undefined fixes #41