<commit_before>/**
 * @class  	HTTP
 * @author  <NAME> <<EMAIL>>
 */

/**
 * @property config
 * @property {String}  config.base The base URL of the API
 * @property {Number}  [config.timeout=30000] Global timeout for the reques. after this value (express in milliseconds) the requests throw an error.
 * @property {Object}  [config.headers={}] Global headers for all requests.
 * @property {Object}  [config.useCache=true] Global cache flag.
 * @property {Object}  [config.offlineCache=false] Global offline cache.
 * @property {Boolean} [config.log=false] Log the requests.
 * @property {Boolean} [config.bodyEncodingInJSON=false] Force to encoding in JSON of body data is the input is a JS object.
 */
exports.config = _.extend({
	base: '',
	timeout: 30000,
	headers: {},
	useCache: true,
	offlineCache: false,
	log: false,
	bodyEncodingInJSON: false
}, Alloy.CFG.T ? Alloy.CFG.T.http : {});

var Event = require('T/event');
var Util = require('T/util');
var Cache = require('T/cache');
var Q = require('T/ext/q');

function extractHTTPText(data, info) {
	if (info != null && data != null) {
		if (info.format === 'json') {
			return Util.parseJSON(data);
		}
	}
	return data;
}

function HTTPRequest(opt) {
	var self = this;
	if (opt.url == null) {
		throw new Error('HTTP.Request: URL not set');
	}

	this.opt = opt;

	// if the url is not matching a protocol, assign the base URL
	if (/\:\/\//.test(opt.url)) {
		this.url = opt.url;
	} else {
		this.url = exports.config.base.replace(/\/$/, '') + '/' + opt.url.replace(/^\//, '');
	}

	this.method = opt.method != null ? opt.method.toUpperCase() : 'GET';
	this.headers = _.extend({}, exports.getHeaders(), opt.headers);
	this.timeout = opt.timeout != null ? opt.timeout : exports.config.timeout;

	// Rebuild the URL if is a GET and there's data
	if (opt.data != null) {
		if (this.method === 'GET') {
			if (typeof opt.data === 'object') {
				var exQuery = /\?.*/.test(this.url);
				this.url = this.url + Util.buildQuery(opt.data, exQuery ? '&' : '?');
			}
		} else {
			if (exports.config.bodyEncodingInJSON == true || opt.bodyEncodingInJSON == true) {
				this.headers['Content-Type'] = 'application/json';
				this.data = JSON.stringify(opt.data);
			} else {
				this.data = opt.data;
			}
		}
	}

	this.hash = this._calculateHash();
	this.uniqueId = exports.getUniqueId();

	// Fill the defer, we will manage the callbacks through it
	this.defer = Q.defer();
	this.defer.promise.then(function() { self._onSuccess.apply(self, arguments); });
	this.defer.promise.fail(function() { self._onError.apply(self, arguments); });

	if (!ENV_PRODUCTION) {
		Ti.API.debug('HTTP: <' + this.uniqueId + '> [' + this.getDebugString() + ']');
	}
}

HTTPRequest.prototype.toString = function() {
	return this.hash;
};

HTTPRequest.prototype.getDebugString = function() {
	return this.method + ' ' + this.url + (this.data ? (' ' + this.data) : '');
};

HTTPRequest.prototype._maybeCacheResponse = function(data) {
	if (exports.config.useCache === false || this.opt.cache === false || this.method !== 'GET') {
		Ti.API.debug('HTTP: <' + this.uniqueId + '> can\'t apply cache strategies upfront');
		return;
	}

	if (this.responseInfo.ttl <= 0) {
		Ti.API.debug('HTTP: <' + this.uniqueId + '> can\'t apply cache strategies');
		return;
	}

	Cache.set(this.hash, data, this.responseInfo.ttl, this.responseInfo);
};

HTTPRequest.prototype.getCachedResponse = function() {
	if (exports.config.useCache === false || this.opt.cache === false || this.opt.refresh === true || this.method !== 'GET') {
		return;
	}

	this.cachedData = Cache.get(this.hash);
<commit_msg>Better log handling in HTTP<commit_after>/**
 * @class  	HTTP
 * @author  <NAME> <<EMAIL>>
 */

/**
 * @property config
 * @property {String}  config.base The base URL of the API
 * @property {Number}  [config.timeout=30000] Global timeout for the reques. after this value (express in milliseconds) the requests throw an error.
 * @property {Object}  [config.headers={}] Global headers for all requests.
 * @property {Object}  [config.useCache=true] Global cache flag.
 * @property {Object}  [config.offlineCache=false] Global offline cache.
 * @property {Boolean} [config.log=false] Log the requests.
 * @property {Boolean} [config.bodyEncodingInJSON=false] Force to encoding in JSON of body data is the input is a JS object.
 */
exports.config = _.extend({
	base: '',
	timeout: 30000,
	headers: {},
	useCache: true,
	offlineCache: false,
	log: false,
	bodyEncodingInJSON: false
}, Alloy.CFG.T ? Alloy.CFG.T.http : {});

var Event = require('T/event');
var Util = require('T/util');
var Cache = require('T/cache');
var Q = require('T/ext/q');

function extractHTTPText(data, info) {
	if (info != null && data != null) {
		if (info.format === 'json') {
			return Util.parseJSON(data);
		}
	}
	return data;
}

function HTTPRequest(opt) {
	var self = this;
	if (opt.url == null) {
		throw new Error('HTTP.Request: URL not set');
	}

	this.opt = opt;

	// if the url is not matching a protocol, assign the base URL
	if (/\:\/\//.test(opt.url)) {
		this.url = opt.url;
	} else {
		this.url = exports.config.base.replace(/\/$/, '') + '/' + opt.url.replace(/^\//, '');
	}

	this.method = opt.method != null ? opt.method.toUpperCase() : 'GET';
	this.headers = _.extend({}, exports.getHeaders(), opt.headers);
	this.timeout = opt.timeout != null ? opt.timeout : exports.config.timeout;

	// Rebuild the URL if is a GET and there's data
	if (opt.data != null) {
		if (this.method === 'GET') {
			if (typeof opt.data === 'object') {
				var exQuery = /\?.*/.test(this.url);
				this.url = this.url + Util.buildQuery(opt.data, exQuery ? '&' : '?');
			}
		} else {
			if (exports.config.bodyEncodingInJSON == true || opt.bodyEncodingInJSON == true) {
				this.headers['Content-Type'] = 'application/json';
				this.data = JSON.stringify(opt.data);
			} else {
				this.data = opt.data;
			}
		}
	}

	this.hash = this._calculateHash();
	this.uniqueId = exports.getUniqueId();

	// Fill the defer, we will manage the callbacks through it
	this.defer = Q.defer();
	this.defer.promise.then(function() { self._onSuccess.apply(self, arguments); });
	this.defer.promise.fail(function() { self._onError.apply(self, arguments); });

	if (!ENV_PRODUCTION) {
		Ti.API.debug('HTTP: <' + this.uniqueId + '>', this.method, this.url, this.data);
	}
}

HTTPRequest.prototype.toString = function() {
	return this.hash;
};

HTTPRequest.prototype._maybeCacheResponse = function(data) {
	if (exports.config.useCache === false || this.opt.cache === false || this.method !== 'GET') {
		Ti.API.debug('HTTP: <' + this.uniqueId + '> can\'t apply cache strategies upfront');
		return;
	}

	if (this.responseInfo.ttl <= 0) {
		Ti.API.debug('HTTP: <' + this.uniqueId + '> can\'t apply cache strategies');
		return;
	}

	Cache.set(this.hash, data, this.responseInfo.ttl, this.responseInfo);
};

HTTPRequest.prototype.getCachedResponse = function() {
	if (exports.config.useCache === false || this.opt.cache === false || this.opt.refresh === true || this.method !== 'GET') {
		return;
	}

	this.cachedData = Cache.get(this.hash);

{'fraction_non_alphanumeric': 0.18012684989429176, 'fraction_numerical': 0.0031007751937984496, 'mean_word_length': 3.693121693121693, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7345154', 'n_tokens_mistral': 2521, 'n_tokens_neox': 2386, 'n_words': 591}