lib/request.js
function dispatchSecure(path, httpMethod, requestData, headers, authtype,
		hostInfo, credentials, logger, callback) {
	var url;
	var is2Legged = authtype === '2-legged';
	var token = is2Legged ? null : requestData.accesstoken;
	var secret = is2Legged ? null : requestData.accesssecret;
	var mergedHeaders = createHeaders(hostInfo.host, headers);
	var oauthClient = oauthHelper.createOAuthWrapper(credentials.consumerkey,
			credentials.consumersecret, mergedHeaders);
	var methodLookup = {
		'POST' : oauthClient.post.bind(oauthClient),
		'PUT' : oauthClient.put.bind(oauthClient)
	};
	var oauthMethod = methodLookup[httpMethod];

	hostInfo.port = hostInfo.port || 443;

	requestData = prepare(requestData, credentials.consumerkey);

	if (!is2Legged) {
		delete requestData.accesstoken;
		delete requestData.accesssecret;
	}

	url = buildSecureUrl(httpMethod, hostInfo, path, requestData);

	logger.info('token: ' + token + ' secret: ' + secret);
	logger.info(httpMethod + ': ' + url + ' (' + authtype + ' oauth)');
	logHeaders(logger, mergedHeaders);

	function cbWithDataAndResponse(err, data, response) {
		var apiError;

		if (err) {
			// API server error
			if (err.statusCode && err.statusCode >= 400) {
				// non 200 status and string for response body is usually an
				// oauth error from one of the endpoints
				if (typeof err.data === 'string' && /oauth/i.test(err.data)) {
					apiError = new OAuthError(err.data, err.data + ': ' +
						path);
				} else {
					apiError = new ApiHttpError(
					response.statusCode, err.data, path);
				}

				return callback(apiError);
			}

			// Something unknown went wrong
			logger.error(err);
			return callback(err);
		}

		return callback(null, data, response);
	}

	if (httpMethod === 'GET') {
		return oauthClient.get(url, token, secret, cbWithDataAndResponse);
	}

	if ( oauthMethod ) {
		logger.info('DATA: ' + qs.stringify(requestData));
		return oauthMethod(url, token, secret, requestData,
			'application/x-www-form-urlencoded', cbWithDataAndResponse);
	}

	return callback(new Error('Unsupported HTTP verb: ' + httpMethod));
}
function dispatchSecure(path, httpMethod, requestData, headers, authtype,
		hostInfo, credentials, logger, callback) {
	var url;
	var is2Legged = authtype === '2-legged';
	var token = is2Legged ? null : requestData.accesstoken;
	var secret = is2Legged ? null : requestData.accesssecret;
	var mergedHeaders = createHeaders(hostInfo.host, headers);
	var oauthClient = oauthHelper.createOAuthWrapper(credentials.consumerkey,
			credentials.consumersecret, mergedHeaders);
	var methodLookup = {
		'POST' : oauthClient.post.bind(oauthClient),
		'PUT' : oauthClient.put.bind(oauthClient)
	};
	var oauthMethod = methodLookup[httpMethod];

	hostInfo.port = hostInfo.port || 443;

	requestData = prepare(requestData, credentials.consumerkey);

	if (!is2Legged) {
		delete requestData.accesstoken;
		delete requestData.accesssecret;
	}

	url = buildSecureUrl(httpMethod, hostInfo, path, requestData);

	logger.info('token: ' + token + ' secret: ' + secret);
	logger.info(httpMethod + ': ' + url + ' (' + authtype + ' oauth)');
	logHeaders(logger, mergedHeaders);

	function cbWithDataAndResponse(err, data, response) {
		var apiError;

		if (err) {
			// API server error
			if (err.statusCode && err.statusCode >= 400) {
				// non 200 status and string for response body is usually an
				// oauth error from one of the endpoints
				if (typeof err.data === 'string' && /oauth/i.test(err.data)) {
					apiError = new OAuthError(err.data, err.data + ': ' +
						path);
				} else {
					apiError = new ApiHttpError(
					response.statusCode, err.data, path);
				}

				return callback(apiError);
			}

			// Something unknown went wrong
			logger.error(err);
			return callback(err);
		}

		return callback(null, data, response);
	}

	if (httpMethod === 'GET') {
		return oauthClient.get(url, token, secret, cbWithDataAndResponse);
	}

	if ( oauthMethod ) {
		logger.info('DATA: ' + qs.stringify(requestData));
		return oauthMethod(url, token, secret, requestData,
			'application/x-www-form-urlencoded', cbWithDataAndResponse);
	}

	return callback(new Error('Unsupported HTTP verb: ' + httpMethod));
}
function dispatchSecure ( path , httpMethod , requestData , headers , authtype , hostInfo , credentials , logger , callback ) { var url ; var is2Legged = authtype === '2-legged' ; var token = is2Legged ? null : requestData . accesstoken ; var secret = is2Legged ? null : requestData . accesssecret ; var mergedHeaders = createHeaders ( hostInfo . host , headers ) ; var oauthClient = oauthHelper . createOAuthWrapper ( credentials . consumerkey , credentials . consumersecret , mergedHeaders ) ; var methodLookup = { 'POST' : oauthClient . post . bind ( oauthClient ) , 'PUT' : oauthClient . put . bind ( oauthClient ) } ; var oauthMethod = methodLookup [ httpMethod ] ; hostInfo . port = hostInfo . port || 443 ; requestData = prepare ( requestData , credentials . consumerkey ) ; if ( ! is2Legged ) { delete requestData . accesstoken ; delete requestData . accesssecret ; } url = buildSecureUrl ( httpMethod , hostInfo , path , requestData ) ; logger . info ( 'token: ' + token + ' secret: ' + secret ) ; logger . info ( httpMethod + ': ' + url + ' (' + authtype + ' oauth)' ) ; logHeaders ( logger , mergedHeaders ) ; function cbWithDataAndResponse ( err , data , response ) { var apiError ; if ( err ) { // API server error if ( err . statusCode && err . statusCode >= 400 ) { // non 200 status and string for response body is usually an // oauth error from one of the endpoints if ( typeof err . data === 'string' && / oauth / i . test ( err . data ) ) { apiError = new OAuthError ( err . data , err . data + ': ' + path ) ; } else { apiError = new ApiHttpError ( response . statusCode , err . data , path ) ; } return callback ( apiError ) ; } // Something unknown went wrong logger . error ( err ) ; return callback ( err ) ; } return callback ( null , data , response ) ; } if ( httpMethod === 'GET' ) { return oauthClient . get ( url , token , secret , cbWithDataAndResponse ) ; } if ( oauthMethod ) { logger . info ( 'DATA: ' + qs . stringify ( requestData ) ) ; return oauthMethod ( url , token , secret , requestData , 'application/x-www-form-urlencoded' , cbWithDataAndResponse ) ; } return callback ( new Error ( 'Unsupported HTTP verb: ' + httpMethod ) ) ; }
Dispatches an oauth signed request to the API  - @param {String} url - the path of the API url to request. - @param {String} httpMethod - @param {Object} requestData - hash of the parameters for the request. - @param {Object} headers - Headers for this request. - @param {String} authType - OAuth request type: '2-legged' or '3-legged' - @param {Object} hostInfo - API host information - @param {Function} callback - The callback to call with the response.
Dispatches an oauth signed request to the API -