<commit_before>// native imports
import util from 'util';

// libraries
import arginfo from 'arginfo';
import extend from 'extend';
import sparqljs from 'sparqljs';
import request from 'request';
import rapunzel from 'rapunzel';

// local modules
import overloader from './overloader';
import query_builder from './query-builder';

//
const sparql_parser = sparqljs.Parser;

// colored output
require(__dirname+'/console-color.js');


/**
* private static:
**/

// meta class setup
const __class_name = 'Spaz';


// class constants


//
const map_to_hash = (h_map) => {

	// create plain object (hash)
	var h_hash = {};

	// iterate keys of map
	for(let [s_key, z_value] of h_map) {

		// push each key/value pair to hahs
		h_hash[s_key] = z_value;
	}

	//
	return h_hash;
};


//
const A_VALID_TABLE_HTTP_METHODS = ['get', 'post'];
const A_VALID_GRAPH_HTTP_METHODS = ['get', 'post'];
const A_VALID_UPDATE_HTTP_METHODS = ['put'];

//
const setup_remote = (h_engine) => {

	// prepare http method to use for table-result queries
	let s_table_method = A_VALID_TABLE_HTTP_METHODS[0];

	// prepare http method to use for graph-result queries
	let s_graph_method = A_VALID_GRAPH_HTTP_METHODS[0];

	// prepare results parser
	let f_parser = (s_body) => {
		return new Promise((f_resolve, f_reject) => {
			try {
				f_resolve(
					JSON.parse(s_body)
				);
			}
			catch(e) {
				f_reject(e);
			}
		});
	};


	// engine gives method option
	if(h_engine.http_methods) {

		// ref http methods
		let h_http_methods = h_engine.http_methods;

		// http method to use for table-result queries
		if(h_http_methods.table) {

			// invalid http method for table-result queries
			if(!A_VALID_TABLE_HTTP_METHODS.includes(h_http_methods.table)) {
				return local.fail('invalid HTTP method to use for table-result queries: '+h_http_methods.table);
			}

			// set http method for table-result queries
			s_table_method = h_http_methods.table;
		}

		// http method to use for graph-result queries
		if(h_http_methods.graph) {

			// ref http method
			let s_method = h_http_methods.graph.toLowerCase();

			// invalid http method for table-result queries
			if(!A_VALID_GRAPH_HTTP_METHODS.includes(s_method)) {
				return local.fail('invalid HTTP method to use for graph-result queries: '+s_method);
			}

			// set http method for graph-result queries
			s_graph_method = s_method;
		}
	}

	// engine interface function
	let k_engine = (s_sparql, s_type) => {

		// prepare http method
		let s_method;

		// prepare http accept header
		let s_accept;

		// tabular result query (ASK, SELECT)
		if('table' === s_type) {
			s_method = s_table_method;
			s_accept = 'application/sparql-results+json';
		}
		// graph result query (DESCRIBE)
		else if('graph' === s_type) {
			s_method = s_graph_method;
			s_accept = 'application/ld+json';
		}
		// invalid execution type
		else {
			return local.fail('invalid SPARQL execution type: '+s_type);
		}

		// prepare request options
		let h_request = {
			method: s_method,
			url: h_engine.url,
			headers: {
				Accept: s_accept,
			},
		};

		// set query data
		let h_query_data = {
			query: s_sparql
		};

		// GET method
		if('get' === s_method) {

			// set query string
			h_request.qs = h_query_data;
		}
		// POST method
		else if('post' === s_method) {

			// set form data
			h_request.form = h_query_data;
		}

		//
		return new Promise((f_resolve, f_reject) => {

			// submit request
			request(h_request, (h_err, h_response, s_body) => {

				// 
				if(h_err) {
					return f_reject(h_err);
				}

				// not okay response
				if(200 !== h_response.statusCode) {
					return f_reject('Bad HTTP response from host: '+h_response.statusCode+' - '+h_response.statusMessage);
				}

				// parse body
				f_parser(s_body)
					// success
					.then((h_json) => {

						// forward to callback
						f_resolve(h_json);
					})
					// parse error
					.catch((e) => {

						// decide how to handle errors
						f_reject(e);
						local.fail(e);
					});
			});
		});
	};

	//
	return k_engine;
};


// engine setup router<commit_msg>reject w/ http reject info<commit_after>// native imports
import util from 'util';

// libraries
import arginfo from 'arginfo';
import extend from 'extend';
import sparqljs from 'sparqljs';
import request from 'request';
import rapunzel from 'rapunzel';

// local modules
import overloader from './overloader';
import query_builder from './query-builder';

//
const sparql_parser = sparqljs.Parser;

// colored output
require(__dirname+'/console-color.js');


/**
* private static:
**/

// meta class setup
const __class_name = 'Spaz';


// class constants


//
const map_to_hash = (h_map) => {

	// create plain object (hash)
	var h_hash = {};

	// iterate keys of map
	for(let [s_key, z_value] of h_map) {

		// push each key/value pair to hahs
		h_hash[s_key] = z_value;
	}

	//
	return h_hash;
};


//
const A_VALID_TABLE_HTTP_METHODS = ['get', 'post'];
const A_VALID_GRAPH_HTTP_METHODS = ['get', 'post'];
const A_VALID_UPDATE_HTTP_METHODS = ['put'];

//
const setup_remote = (h_engine) => {

	// prepare http method to use for table-result queries
	let s_table_method = A_VALID_TABLE_HTTP_METHODS[0];

	// prepare http method to use for graph-result queries
	let s_graph_method = A_VALID_GRAPH_HTTP_METHODS[0];

	// prepare results parser
	let f_parser = (s_body) => {
		return new Promise((f_resolve, f_reject) => {
			try {
				f_resolve(
					JSON.parse(s_body)
				);
			}
			catch(e) {
				f_reject(e);
			}
		});
	};


	// engine gives method option
	if(h_engine.http_methods) {

		// ref http methods
		let h_http_methods = h_engine.http_methods;

		// http method to use for table-result queries
		if(h_http_methods.table) {

			// invalid http method for table-result queries
			if(!A_VALID_TABLE_HTTP_METHODS.includes(h_http_methods.table)) {
				return local.fail('invalid HTTP method to use for table-result queries: '+h_http_methods.table);
			}

			// set http method for table-result queries
			s_table_method = h_http_methods.table;
		}

		// http method to use for graph-result queries
		if(h_http_methods.graph) {

			// ref http method
			let s_method = h_http_methods.graph.toLowerCase();

			// invalid http method for table-result queries
			if(!A_VALID_GRAPH_HTTP_METHODS.includes(s_method)) {
				return local.fail('invalid HTTP method to use for graph-result queries: '+s_method);
			}

			// set http method for graph-result queries
			s_graph_method = s_method;
		}
	}

	// engine interface function
	let k_engine = (s_sparql, s_type) => {

		// prepare http method
		let s_method;

		// prepare http accept header
		let s_accept;

		// tabular result query (ASK, SELECT)
		if('table' === s_type) {
			s_method = s_table_method;
			s_accept = 'application/sparql-results+json';
		}
		// graph result query (DESCRIBE)
		else if('graph' === s_type) {
			s_method = s_graph_method;
			s_accept = 'application/ld+json';
		}
		// invalid execution type
		else {
			return local.fail('invalid SPARQL execution type: '+s_type);
		}

		// prepare request options
		let h_request = {
			method: s_method,
			url: h_engine.url,
			headers: {
				Accept: s_accept,
			},
		};

		// set query data
		let h_query_data = {
			query: s_sparql
		};

		// GET method
		if('get' === s_method) {

			// set query string
			h_request.qs = h_query_data;
		}
		// POST method
		else if('post' === s_method) {

			// set form data
			h_request.form = h_query_data;
		}

		//
		return new Promise((f_resolve, f_reject) => {

			// wrap rejector with http request info
			let f_reject_http = (s_msg) => {
				f_reject(s_msg+'\nrequest data: '+arginfo(h_request));
			};

			// submit request
			request(h_request, (h_err, h_response, s_body) => {

				// 
				if(h_err) {
					return f_reject_http(h_err);
				}

				// not okay response
				if(200 !== h_response.statusCode) {
					return f_reject_http('Bad HTTP response from host: '+h_response.statusCode+' - '+h_response.statusMessage);
				}

				// parse body
				f_parser(s_body)
					// success
					.then((h_json) => {

						// forward to callback
						f_resolve(h_json);
					})
					// parse error
					.catch((e) => {

						// decide how to handle errors
						f_reject(e);
						local.fail(e);
					});
			});
		});
	};

	//
	return k_engine;
};


// engine setup router
{'fraction_non_alphanumeric': 0.12647270978600625, 'fraction_numerical': 0.0012022120702091848, 'mean_word_length': 2.957659372026641, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, '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': '1293750', 'n_tokens_mistral': 3389, 'n_tokens_neox': 2865, 'n_words': 723}