<commit_before>var version      = require( '../package.json').version;
var request      = require( 'request');
var _            = require( 'lodash' );
var EventEmitter = require( 'events' ).EventEmitter;
var util         = require( 'util' );
var url          = require( 'url' );
var FuelAuth     = require( 'fuel-auth' );


var FuelRest = function( authOptions, restEndpoint ) {
	'use strict';

	try {
		this.AuthClient = new FuelAuth( authOptions );
	} catch( err ) {
		console.log( err );
		return;
	}

	// adding version to object
	this.version = version;

	// setting up default headers
	this.defaultHeaders = {
		'User-Agent': 'node-fuel/' + this.version
		, 'Content-Type': 'application/json'
	};

	// configuring rest options
	this.requestOptions     = {};
	this.requestOptions.uri = restEndpoint || 'https://www.exacttargetapis.com';
};

// adding inheriting properties from EventEmitter
util.inherits( FuelRest, EventEmitter );

FuelRest.prototype.apiRequest = function( type, uri, requestOptions, authOptions, callback ) {
	'use strict';

	// setting up request options
	requestOptions        = _.merge( this.requestOptions, requestOptions || {} );
	requestOptions.uri    = url.resolve( requestOptions.uri, uri ); // resolving url to be used for request
	requestOptions.method = type;

	// setting up authOptions
	authOptions                = authOptions || {};
	authOptions.forceRequest   = authOptions.forceRequest || null;
	authOptions.requestOptions = authOptions.requestOptions || null;

	this.AuthClient.getAccessToken( authOptions.requestOptions, authOptions.forceRequest );

	this.AuthClient.on( 'response', function( body ) {
		// if there's no access token we have a problem
		if( !body.accessToken ) {
			this._deliverResponse( 'error', 'No token. API Response - ' + JSON.stringify( body ), callback );
			return;
		}

		request( requestOptions, function( err, res, body ) {
			if( err ) {
				this._deliverResponse( 'error', err, callback );
				return;
			}

			this._deliverResponse( 'reponse', body, callback );

		}.bind( this ) );
	});

	this.AuthClient.on( 'error', function( err ) {
		this._deliverResponse( 'error', 'FuelAuth error - ' + err, callback );
	});
};

FuelRest.prototype.get = function( uri, options, callback ) {
	'use strict';

	this.apiRequest( 'GET', uri, options.requestOptions, options.authOptions, callback );
};

FuelRest.prototype.post = function( uri, options, callback ) {
	'use strict';

	this.apiRequest( 'POST', uri, options.requestOptions, options.authOptions, callback );
};

FuelRest.prototype.put = function( uri, options, callback ) {
	'use strict';

	this.apiRequest( 'PUT', uri, options.requestOptions, options.authOptions, callback );
};

FuelRest.prototype.delete = function( uri, options, callback ) {
	'use strict';

	this.apiRequest( 'DELETE', uri, options.requestOptions, options.authOptions, callback );
};


FuelAuth.prototype._deliverResponse = function( type, data, callback ) {
	'use strict';

	// if it's a callback, lets use that
	if( _.isFunction( callback ) ) {
		if( type === 'error' ) {
			callback( data, null );
		} else if( type === 'response' ) {
			callback( null, data );
		}
		return;
	}

	this.emit( type, data );
};

module.exports = FuelRest;<commit_msg>adding origin to errors for better debugging<commit_after>var version      = require( '../package.json').version;
var request      = require( 'request');
var _            = require( 'lodash' );
var EventEmitter = require( 'events' ).EventEmitter;
var util         = require( 'util' );
var url          = require( 'url' );
var FuelAuth     = require( 'fuel-auth' );


var FuelRest = function( authOptions, restEndpoint ) {
	'use strict';

	try {
		this.AuthClient = new FuelAuth( authOptions );
	} catch( err ) {
		console.log( err );
		return;
	}

	// adding version to object
	this.version = version;

	// setting up default headers
	this.defaultHeaders = {
		'User-Agent': 'node-fuel/' + this.version
		, 'Content-Type': 'application/json'
	};

	// configuring rest options
	this.requestOptions     = {};
	this.requestOptions.uri = restEndpoint || 'https://www.exacttargetapis.com';
};

// adding inheriting properties from EventEmitter
util.inherits( FuelRest, EventEmitter );

FuelRest.prototype.apiRequest = function( type, uri, requestOptions, authOptions, callback ) {
	'use strict';

	// setting up request options
	requestOptions        = _.merge( this.requestOptions, requestOptions || {} );
	requestOptions.uri    = url.resolve( requestOptions.uri, uri ); // resolving url to be used for request
	requestOptions.method = type;

	// setting up authOptions
	authOptions                = authOptions || {};
	authOptions.forceRequest   = authOptions.forceRequest || null;
	authOptions.requestOptions = authOptions.requestOptions || null;

	this.AuthClient.getAccessToken( authOptions.requestOptions, authOptions.forceRequest );

	this.AuthClient.on( 'response', function( body ) {
		// if there's no access token we have a problem
		if( !body.accessToken ) {
			this._deliverResponse( 'error', body, callback, 'API Response - No token' );
			return;
		}

		request( requestOptions, function( err, res, body ) {
			if( err ) {
				this._deliverResponse( 'error', err, callback, 'Request Module inside apiRequest' );
				return;
			}

			this._deliverResponse( 'reponse', body, callback );

		}.bind( this ) );
	});

	this.AuthClient.on( 'error', function( err ) {
		this._deliverResponse( 'error', err, callback, 'FuelAuth' );
	});
};

FuelRest.prototype.get = function( uri, options, callback ) {
	'use strict';

	this.apiRequest( 'GET', uri, options.requestOptions, options.authOptions, callback );
};

FuelRest.prototype.post = function( uri, options, callback ) {
	'use strict';

	this.apiRequest( 'POST', uri, options.requestOptions, options.authOptions, callback );
};

FuelRest.prototype.put = function( uri, options, callback ) {
	'use strict';

	this.apiRequest( 'PUT', uri, options.requestOptions, options.authOptions, callback );
};

FuelRest.prototype.delete = function( uri, options, callback ) {
	'use strict';

	this.apiRequest( 'DELETE', uri, options.requestOptions, options.authOptions, callback );
};


FuelAuth.prototype._deliverResponse = function( type, data, callback, errorFrom ) {
	'use strict';

	// if it's an error and we have where it occured, let's tack it on
	if( type === 'error' && !!errorFrom ) {
		data.errorPropagatedFrom = errorFrom;
	}

	if( _.isFunction( callback ) ) {
		if( type === 'error' ) {
			callback( data, null );
		} else if( type === 'response' ) {
			callback( null, data );
		}
		return;
	}

	this.emit( type, data );
};

module.exports = FuelRest;
{'fraction_non_alphanumeric': 0.14045112781954888, 'fraction_numerical': 0.0, 'mean_word_length': 3.7371794871794872, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4892388', 'n_tokens_mistral': 2123, 'n_tokens_neox': 1989, 'n_words': 545}