lib/unifile-dropbox.js
function callAPI(session, path, data, subdomain = 'api', isJson = true, headers = null) {
	const authorization = 'Bearer ' + session.token;

	const reqOptions = {
		url: `https://${subdomain}.dropboxapi.com/2${path}`,
		method: 'POST',
		headers: {
			'Authorization': authorization,
			'User-Agent': 'Unifile'
		},
		json: isJson,
		encoding: null
	};

	if(data && Object.keys(data).length !== 0) reqOptions.body = data;

	if(headers) {
		for(const header in headers) {
			reqOptions.headers[header] = headers[header];
		}
	}

	return new Promise(function(resolve, reject) {
		request(reqOptions, function(err, res, body) {
			if(err) {
				reject(err);
			} else if(res.statusCode >= 400) {
				let errorMessage = null;
				// In case of users/get_current_account, Dropbox return a String with the error
				// Since isJson = true, it gets parsed by request
				if(Buffer.isBuffer(body)) {
					try {
						errorMessage = JSON.parse(body.toString()).error_summary;
					} catch (e) {
						errorMessage = body.toString();
					}
				} else {
					errorMessage = (isJson ? body : JSON.parse(body)).error_summary;
				}
				// Dropbox only uses 409 for endpoints specific errors
				let filename = null;
				try {
					filename = res.request.headers.hasOwnProperty('Dropbox-API-Arg') ?
						JSON.parse(res.request.headers['Dropbox-API-Arg']).path
						: JSON.parse(res.request.body).path;
				} catch (e) {}
				if(errorMessage.includes('/not_found/')) {
					reject(new UnifileError(UnifileError.ENOENT, `Not Found: ${filename}`));
				} else if(errorMessage.startsWith('path/conflict/')) {
					reject(new UnifileError(UnifileError.EINVAL, `Creation failed due to conflict: ${filename}`));
				} else if(errorMessage.startsWith('path/not_file/')) {
					reject(new UnifileError(UnifileError.EINVAL, `Path is a directory: ${filename}`));
				} else if(res.statusCode === 401) {
					reject(new UnifileError(UnifileError.EACCES, errorMessage));
				} else {
					reject(new UnifileError(UnifileError.EIO, errorMessage));
				}
			} else resolve(body);
		});
	});
}
function callAPI(session, path, data, subdomain = 'api', isJson = true, headers = null) {
	const authorization = 'Bearer ' + session.token;

	const reqOptions = {
		url: `https://${subdomain}.dropboxapi.com/2${path}`,
		method: 'POST',
		headers: {
			'Authorization': authorization,
			'User-Agent': 'Unifile'
		},
		json: isJson,
		encoding: null
	};

	if(data && Object.keys(data).length !== 0) reqOptions.body = data;

	if(headers) {
		for(const header in headers) {
			reqOptions.headers[header] = headers[header];
		}
	}

	return new Promise(function(resolve, reject) {
		request(reqOptions, function(err, res, body) {
			if(err) {
				reject(err);
			} else if(res.statusCode >= 400) {
				let errorMessage = null;
				// In case of users/get_current_account, Dropbox return a String with the error
				// Since isJson = true, it gets parsed by request
				if(Buffer.isBuffer(body)) {
					try {
						errorMessage = JSON.parse(body.toString()).error_summary;
					} catch (e) {
						errorMessage = body.toString();
					}
				} else {
					errorMessage = (isJson ? body : JSON.parse(body)).error_summary;
				}
				// Dropbox only uses 409 for endpoints specific errors
				let filename = null;
				try {
					filename = res.request.headers.hasOwnProperty('Dropbox-API-Arg') ?
						JSON.parse(res.request.headers['Dropbox-API-Arg']).path
						: JSON.parse(res.request.body).path;
				} catch (e) {}
				if(errorMessage.includes('/not_found/')) {
					reject(new UnifileError(UnifileError.ENOENT, `Not Found: ${filename}`));
				} else if(errorMessage.startsWith('path/conflict/')) {
					reject(new UnifileError(UnifileError.EINVAL, `Creation failed due to conflict: ${filename}`));
				} else if(errorMessage.startsWith('path/not_file/')) {
					reject(new UnifileError(UnifileError.EINVAL, `Path is a directory: ${filename}`));
				} else if(res.statusCode === 401) {
					reject(new UnifileError(UnifileError.EACCES, errorMessage));
				} else {
					reject(new UnifileError(UnifileError.EIO, errorMessage));
				}
			} else resolve(body);
		});
	});
}
function callAPI ( session , path , data , subdomain = 'api' , isJson = true , headers = null ) { const authorization = 'Bearer ' + session . token ; const reqOptions = { url : ` ${ subdomain } ${ path } ` , method : 'POST' , headers : { 'Authorization' : authorization , 'User-Agent' : 'Unifile' } , json : isJson , encoding : null } ; if ( data && Object . keys ( data ) . length !== 0 ) reqOptions . body = data ; if ( headers ) { for ( const header in headers ) { reqOptions . headers [ header ] = headers [ header ] ; } } return new Promise ( function ( resolve , reject ) { request ( reqOptions , function ( err , res , body ) { if ( err ) { reject ( err ) ; } else if ( res . statusCode >= 400 ) { let errorMessage = null ; // In case of users/get_current_account, Dropbox return a String with the error // Since isJson = true, it gets parsed by request if ( Buffer . isBuffer ( body ) ) { try { errorMessage = JSON . parse ( body . toString ( ) ) . error_summary ; } catch ( e ) { errorMessage = body . toString ( ) ; } } else { errorMessage = ( isJson ? body : JSON . parse ( body ) ) . error_summary ; } // Dropbox only uses 409 for endpoints specific errors let filename = null ; try { filename = res . request . headers . hasOwnProperty ( 'Dropbox-API-Arg' ) ? JSON . parse ( res . request . headers [ 'Dropbox-API-Arg' ] ) . path : JSON . parse ( res . request . body ) . path ; } catch ( e ) { } if ( errorMessage . includes ( '/not_found/' ) ) { reject ( new UnifileError ( UnifileError . ENOENT , ` ${ filename } ` ) ) ; } else if ( errorMessage . startsWith ( 'path/conflict/' ) ) { reject ( new UnifileError ( UnifileError . EINVAL , ` ${ filename } ` ) ) ; } else if ( errorMessage . startsWith ( 'path/not_file/' ) ) { reject ( new UnifileError ( UnifileError . EINVAL , ` ${ filename } ` ) ) ; } else if ( res . statusCode === 401 ) { reject ( new UnifileError ( UnifileError . EACCES , errorMessage ) ) ; } else { reject ( new UnifileError ( UnifileError . EIO , errorMessage ) ) ; } } else resolve ( body ) ; } ) ; } ) ; }
Make a call to the Dropbox API
@param {Object} session - Dropbox session storage
@param {string} path - End point path
@param {Object} data - Data to pass. Convert to querystring if method is GET or to the request body
@param {string} [subdomain=api] - Subdomain of the endpoint to call (api/content)
@param {boolean} [isJson=true] - Whether to stringify the body or not
@param {Object} [headers={}] - Override of addition to the request headers
@return {Promise} a Promise of the result send by server
@private
Make a call to the Dropbox API