<commit_before>var axios = require('axios');

function AxiosClient(opt) {
	opt = opt || {};
	this.baseURI = opt.baseURI;
	this.defaultObject = opt.defaultObj;
	if (opt.parser)
		this.parser = function(data, req) {
			if (typeof data !== "string")
				return data;
			try {
				return opt.parser(data, req);
			} catch (e) {
				console.error('AxiosClient : error while parsing ! ', e);
				return null;
			}
		};
	if (opt.useCache)
		this.cache = {};
}

AxiosClient.prototype = {
	get: function(req, opt) {
		if (this.cache && this.cache[req])
			return this.cache[req];
		var self = this,
			uri = this.baseURI + req
		return axios.get(uri)
			.then(function(s) {
				if (self.parser)
					s.data = self.parser(s.data, req);
				if (self.cache)
					self.cache[req] = s.data;
				return s.data;
			})
			.catch(function(e) {
				console.error("AxiosClient get error (uri : %s) : ", uri, e);
				throw e;
			});
	},
	del: function(id, opt) {
		var self = this,
			req = this.baseURI + id;
		return axios.delete(req)
			.then(function(s) {
				if (self.cache && self.cache[req])
					delete self.cache[req];
				return s.data;
			})
			.catch(function(e) {
				console.error("AxiosClient del error (uri : %s) : ", req, e);
			});
	},
	post: function(data, opt) {
		var self = this;
		return axios.post(this.baseURI, data)
			.then(function(s) {
				data.id = s.data.id;
				var req = self.baseURI + s.data.id;
				if (self.parser)
					s.data = self.parser(s.data, req);
				if (self.cache)
					self.cache[req] = s.data;
				return s.data;
			})
			.catch(function(e) {
				console.error("AxiosClient post error (uri : %s) : ", self.baseURI, e);
			});
	},
	patch: function(id, value, path, opt) {
		var self = this;
		return axios.post(this.baseURI + 'patch', {
				id: id,
				path: path,
				value: value
			})
			.then(function(s) {
				return s.data;
			})
			.catch(function(e) {
				console.error("AxiosClient post error (uri : %s) : ", self.baseURI, e);
			});
	},
	put: function(data, opt) {
		var self = this,
			req = this.baseURI + data.id;
		return axios.put(req, data)
			.then(function(s) {
				if (self.parser)
					s.data = self.parser(s.data, req);
				if (self.cache)
					self.cache[req] = s.data;
				return s.data;
			})
			.catch(function(e) {
				console.error("AxiosClient put error (uri : %s) : ", req, e);
			});
	},
	default: function() {
		return y.utils.copy(this.defaultObject);
	},
	first: function(filter) {
		var uri = this.baseURI + 'findOne/' + (filter || '');
		return axios.get(uri)
			.then(function(s) {
				return s.data;
			})
			.catch(function(e) {
				console.error("AxiosClient .first error (uri : %s) : ", uri, e);
			});
	}
};

module.exports = AxiosClient;<commit_msg>add remote and better error output<commit_after>var axios = require('axios');
require('nomocas-utils/lib/promise-log');

function rethrowError(e) {
	throw e.data.error;
}

function AxiosClient(opt) {
	opt = opt || {};
	this.baseURI = opt.baseURI;
	this.defaultObject = opt.defaultObj ||  {};
	if (opt.parser)
		this.parser = function(data, req) {
			if (typeof data !== "string")
				return data;
			try {
				return opt.parser(data, req);
			} catch (e) {
				console.error('AxiosClient : error while parsing response ! ', e);
				return null;
			}
		};
	if (opt.useCache)
		this.cache = {};
}

AxiosClient.prototype = {
	get: function(req, opt) {
		if (this.cache && this.cache[req])
			return this.cache[req];
		var self = this,
			uri = this.baseURI + req;
		return axios.get(uri)
			.then(function(s) {
				if (self.parser)
					s.data = self.parser(s.data, req);
				if (self.cache)
					self.cache[req] = s.data;
				return s.data;
			})
			.catch(rethrowError)
			.addToError({
				level: 'AxiosClient',
				method: 'get',
				uri: uri
			});
	},
	del: function(id, opt) {
		var self = this,
			req = this.baseURI + id;
		return axios.delete(req)
			.then(function(s) {
				if (self.cache && self.cache[req])
					delete self.cache[req];
				return s.data;
			})
			.catch(rethrowError)
			.addToError({
				level: 'AxiosClient',
				method: 'del',
				uri: req
			});
	},
	post: function(data, opt) {
		var self = this;
		return axios.post(this.baseURI, data)
			.then(function(s) {
				data.id = s.data.id;
				var req = self.baseURI + s.data.id;
				if (self.parser)
					s.data = self.parser(s.data, req);
				if (self.cache)
					self.cache[req] = s.data;
				return s.data;
			})
			.catch(rethrowError)
			.addToError({
				level: 'AxiosClient',
				method: 'post',
				uri: this.baseURI,
				data: data
			});
	},
	patch: function(id, value, path, opt) {
		var self = this,
			uri = this.baseURI + 'patch';
		return axios.post(uri, {
				id: id,
				path: path,
				value: value
			})
			.then(function(s) {
				return s.data;
			})
			.catch(rethrowError)
			.addToError({
				level: 'AxiosClient',
				method: 'patch',
				uri: uri,
				id: id,
				value: value,
				path: path
			});
	},
	put: function(data, opt) {
		var self = this,
			req = this.baseURI + data.id;
		return axios.put(req, data)
			.then(function(s) {
				if (self.parser)
					s.data = self.parser(s.data, req);
				if (self.cache)
					self.cache[req] = s.data;
				return s.data;
			})
			.catch(rethrowError)
			.addToError({
				level: 'AxiosClient',
				method: 'put',
				uri: req,
				data: data
			});
	},
	default: function() {
		return y.utils.copy(this.defaultObject);
	},
	first: function(filter, opt) {
		var uri = this.baseURI + 'findOne/' + (filter || '');
		return axios.get(uri)
			.then(function(s) {
				return s.data;
			})
			.catch(rethrowError)
			.addToError({
				level: 'AxiosClient',
				method: 'first',
				uri: uri
			});
	},
	remote: function(method, data, opt) {
		var uri = this.baseURI + method;
		return axios.post(uri, data)
			.then(function(s) {
				return s.data;
			})
			.catch(rethrowError)
			.addToError({
				level: 'AxiosClient',
				method: 'remote',
				uri: uri,
				remoteMethod: method,
				data: data
			});
	}
};

module.exports = AxiosClient;
{'fraction_non_alphanumeric': 0.1716094635121626, 'fraction_numerical': 0.0, 'mean_word_length': 2.6603658536585364, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1642361', 'n_tokens_mistral': 2790, 'n_tokens_neox': 2301, 'n_words': 497}