<commit_before>package t9.remoting.jsonrpc;

import js.node.Url;
import js.node.Http;
import js.node.Querystring;
import js.node.stream.Readable;

import haxe.remoting.JsonRpc;
import haxe.Json;

import promhx.Promise;
import promhx.deferred.DeferredPromise;

using StringTools;

class JsonRpcConnectionHttpGet
	implements JsonRpcConnection
{
	public static function urlConnect(url :String)
	{
		return new JsonRpcConnectionHttpGet(url);
	}

	public function new(url)
	{
		_url = url;
	}

	public function request(method :String, ?params :Dynamic) :Promise<Dynamic>
	{
		var request :RequestDef = {
			id: (++_idCount) + '',
			method: method,
			params: params,
			jsonrpc: JsonRpcConstants.JSONRPC_VERSION_2
		};
		return callInternal(request)
			.then(function(response: ResponseDef) {
				if (response.error != null) {
					throw response.error;
				}
				return response.result;
			});
	}

	public function notify(method :String, ?params :Dynamic) :Promise<Bool>
	{
		var request :RequestDef = {
			method: method,
			params: params,
			jsonrpc: JsonRpcConstants.JSONRPC_VERSION_2
		};
		return callInternal(request)
			.then(function(_) {
				return true;
			});
	}

	function callInternal(request :RequestDef) :Promise<ResponseDef>
	{
		var promise = new DeferredPromise<ResponseDef>();

#if nodejs
		// An object of options to indicate where to post to
		var urlObj = js.node.Url.parse(_url);
		var requestOptions :HttpRequestOptions = {
			hostname: urlObj.hostname,
			port: urlObj.port != null ? Std.parseInt(urlObj.port) : 80,
			path: urlObj.path + (urlObj.path.endsWith('/') ? '' : '/') + request.method + (request.params != null ? '?' + Querystring.stringify(request.params) : ''),
			method: 'GET',
			protocol: urlObj.protocol
		};
		// Set up the request
		var getReq = js.node.Http.request(requestOptions, function(res) {
			res.setEncoding('utf8');
			var responseData = '';
			res.on(ReadableEvent.Data, function (chunk) {
				responseData += chunk;
			});
			res.on(ReadableEvent.Error, function (err) {
				promise.boundPromise.reject(err);
			});
			res.on(ReadableEvent.End, function () {
				if (request.id != null) {
					try {
						var jsonRes = Json.parse(responseData);
						promise.resolve(jsonRes);
					} catch(err :Dynamic) {
						promise.resolve({
							id :request.id,
							error: {code:-32603, message:'Invalid JSON was received by the client.', data:responseData},
							jsonrpc: JsonRpcConstants.JSONRPC_VERSION_2
						});
					}
				} else {
					promise.resolve(null);
				}
			});
		});

		getReq.on('error', function(err) {
			promise.boundPromise.reject(err);
		});

		getReq.end();
#else
		var h = new haxe.Http(_url);
		h.setHeader("content-type","application/json-rpc");

		h.setPostData(Json.stringify(request));

		h.onData = function(response :String) {
			try {<commit_msg>Minor fix<commit_after>package t9.remoting.jsonrpc;

import js.node.Url;
import js.node.Http;
import js.node.Querystring;
import js.node.stream.Readable;

import haxe.remoting.JsonRpc;
import haxe.Json;

import promhx.Promise;
import promhx.deferred.DeferredPromise;

using StringTools;

class JsonRpcConnectionHttpGet
	implements JsonRpcConnection
{
	public static function urlConnect(url :String)
	{
		return new JsonRpcConnectionHttpGet(url);
	}

	public function new(url)
	{
		_url = url;
	}

	public function request(method :String, ?params :Dynamic) :Promise<Dynamic>
	{
		var request :RequestDef = {
			id: (++_idCount) + '',
			method: method,
			params: params,
			jsonrpc: JsonRpcConstants.JSONRPC_VERSION_2
		};
		return callInternal(request)
			.then(function(response: ResponseDef) {
				if (response.error != null) {
					throw response.error;
				}
				return response.result;
			});
	}

	public function notify(method :String, ?params :Dynamic) :Promise<Bool>
	{
		var request :RequestDef = {
			method: method,
			params: params,
			jsonrpc: JsonRpcConstants.JSONRPC_VERSION_2
		};
		return callInternal(request)
			.then(function(_) {
				return true;
			});
	}

	function callInternal(request :RequestDef) :Promise<ResponseDef>
	{
		var promise = new DeferredPromise<ResponseDef>();

#if nodejs
		// An object of options to indicate where to post to
		var urlObj = js.node.Url.parse(_url);
		var requestOptions :HttpRequestOptions = {
			hostname: urlObj.hostname,
			port: urlObj.port != null ? Std.parseInt(urlObj.port) : 80,
			path: urlObj.path + (urlObj.path.endsWith('/') ? '' : '/') + request.method + (request.params != null ? '?' + Querystring.stringify(request.params) : ''),
			method: 'GET',
			protocol: urlObj.protocol
		};
		// Set up the request
		var getReq = js.node.Http.request(requestOptions, function(res) {
			res.setEncoding('utf8');
			var responseData = '';
			res.on(ReadableEvent.Data, function (chunk) {
				responseData += chunk;
			});
			res.on(ReadableEvent.Error, function (err) {
				promise.boundPromise.reject(err);
			});
			res.on(ReadableEvent.End, function () {
				if (promise.boundPromise.isResolved()) {
					return;
				}
				if (request.id != null) {
					try {
						var jsonRes = Json.parse(responseData);
						promise.resolve(jsonRes);
					} catch(err :Dynamic) {
						promise.resolve({
							id :request.id,
							error: {code:-32603, message:'Invalid JSON was received by the client.', data:responseData},
							jsonrpc: JsonRpcConstants.JSONRPC_VERSION_2
						});
					}
				} else {
					promise.resolve(null);
				}
			});
		});

		getReq.on(ReadableEvent.Error, function(err) {
			if (!promise.boundPromise.isResolved()) {
				promise.boundPromise.reject(err);
			}
		});

		getReq.end();
#else
		var h = new haxe.Http(_url);
		h.setHeader("content-type","application/json-rpc");

		h.setPostData(Json.stringify(request));

		h.onData = function(response :String) {
			try {
{'fraction_non_alphanumeric': 0.1363400725764645, 'fraction_numerical': 0.00414722654224987, 'mean_word_length': 3.6082802547770703, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '5997361', 'n_tokens_mistral': 2282, 'n_tokens_neox': 1912, 'n_words': 396}