function _nativeHttpClientCheckFail() {
 VAR_WAS_ERROR = native("httpclient", "was_error", "{}") == 'true';
 if ((VAR_WAS_ERROR) && (native("httpclient", "get_failOnError", "{}")) == 'true') fail(native("httpclient", "error_string", "{}"));
}

function _nativeHttpClientCb() {
  var res = _result(); 
 _nativeHttpClientCheckFail();
 _return(res);
}

function _nativeHttpClient(funcName, funcParams, stringify, isAsync) {
	if (typeof funcParams === "undefined" || funcParams.length == 0) funcParams = {};
	if (typeof stringify === "undefined" || stringify) funcParams = JSON.stringify(funcParams);
	if (typeof isAsync === "undefined") isAsync = false;
	if (!isAsync) {
		var res = native("httpclient", funcName, funcParams);
		_nativeHttpClientCheckFail();
		return res;
	} else {
		native_async("httpclient", funcName, funcParams, _nativeHttpClientCb)!;
	}
}

function nativeHttpClient_set_FailOnError(value) {  // {enabled:true}
	_nativeHttpClient("set_failOnError", value);
}

function nativeHttpClient_wasError() {
	return _nativeHttpClient("was_error");
}

function nativeHttpClient_getError() {
	return native("httpclient", "error_string", "{}")
}

function nativeHttpClient_set_timeout(timeout) {
 _nativeHttpClient("set_timeout", {timeout:timeout});
}


function nativeHttpClient_clear_headers() {
 _nativeHttpClient("clear_headers");
} 

function nativeHttpClient_save_cookies(cookies) {  // [[SAVED_COOKIES]]
  _nativeHttpClient("save_cookies", cookies, false);
} 

function nativeHttpClient_get_cookies() {
 return _nativeHttpClient("get_cookies");
} 

function nativeHttpClient_restore_cookies() {
 _nativeHttpClient("restore_cookies");
} 

function nativeHttpClient_set_proxy(proxy, proxy_type, proxy_login, proxy_password) {  // 127.0.0.1:8080
    var proxy = proxy_parse(proxy);
	proxy["IsHttp"] = proxy_type=="http";
	var login = proxy_login;
	var password = proxy_password;
	if(login.length > 0 && password.length > 0){
		proxy["name"] = login;
		proxy["password"] = password;
	};
 _nativeHttpClient("set_proxy", proxy);
} 

function nativeHttpClient_send(method, requestObj, allowRedirect) { // GET,  {method:"HEADERS",url:"https://www.google.com",headers:"Upgrade: 1\nUser-Agent: Mozilla",tls:"tls1.2"}
	if (allowRedirect) {											// POST,   {method:"PUT",url:"https://www.google.com",bodyBase64:false,body:"1=1&2=2",headers:"Upgrade: 1\nUser-Agent: Mozilla",tls:"tls1.3",
                                                                                              //  contentType:"application/x-www-form-urlencoded"}
		 _nativeHttpClient(method, requestObj, true, true);
	} else {
		 _nativeHttpClient(method +"_no_redirect", requestObj, true, true);
	}
} 

function _nativeHttpClient_download(requestObj) { //  {url: "", savePath:"" ,headers:"Upgrade: 1\nUser-Agent: Mozilla",tls:"tls1.2"}
	return _nativeHttpClient("download", requestObj, true, true);
} 


function nativeHttpClient_status() {
	return _nativeHttpClient("status");
} 

//function nativeHttpClient_get_content() {
//	var getBase64 = _function_argument("getBase64");
//	if (getBase64) {
//		_function_return(_nativeHttpClient("content_base64"));
//	} else {
//		_function_return(_nativeHttpClient("content"));
//	}
//}

function nativeHttpClient_get_content(getBase64) {
	if (getBase64) {
		return _nativeHttpClient("content_base64");
	} else {
		return _nativeHttpClient("content");
	}
}

function nativeHttpClient_get_headers() {
	return _nativeHttpClient("get_response_headers");
} 

function nativeHttpClient_get_url() {
	return _nativeHttpClient("get_url");
} 

function nativeHttpClient_set_header(header) { // {name: "accept-language",value: "ru-Ru"}
 _nativeHttpClient("set_header", header);
} 

function nativeHttpClient_get_header(header) { // {name: "Set-Cookie","index":2}
 return _nativeHttpClient("get_header", header);
} 

function nativeHttpClient_close() { 
 return _nativeHttpClient("close");
} 

function nativeHttpClient_setFailOnError(setFailOnError) { // {"enabled":true}
 return _nativeHttpClient("set_failOnError", setFailOnError);
}  

function nativeHttpClient_getFailOnError() { 
 return _nativeHttpClient("get_FailOnError");
} 

function nativeHttpClient_showSniffer() {
 _nativeHttpClient("showSniffer");
}


/////////////////////////////////////////////////////////////////////////////////////

function nativeHttpClient_Get(){
	var value = _function_argument("Value");
	var headers = _function_argument("Headers");
	var method = _function_argument("Method");
	var redirect = _function_argument("Redirect");
	var timeout = _function_argument("Timeout");
	var maximum_failes = _function_argument("MaximumFailes");
	var TLSVersion = _function_argument("TLSVersion");
	

_do(function(){
		var cycle_index = _iterator();
		
		_call(function(){
			_on_fail(function(){
				VAR_LAST_ERROR = _result();
				VAR_ERROR_ID = ScriptWorker.GetCurrentAction();
				VAR_WAS_ERROR = false;
				_break(1,true);
			});
			CYCLES.Current().RemoveLabel("function");
			
		
		//nativeHttpClient_set_timeout(timeout);
		nativeHttpClient_send("get",{method:(method), url:(value), tls:(TLSVersion), headers:(headers)}, redirect);	
		
		},null)!

		_if_else(VAR_WAS_ERROR, function(){
			if(cycle_index>=maximum_failes){	
				fail(VAR_LAST_ERROR)
			}
			sleep(1000)!
		}, function(){
			_break("function")
		})!
	})!

}

function nativeHttpClient_Download() {
	var value = _function_argument("Value");
	var file = _function_argument("File");
	var timeout = _function_argument("Timeout");
	var maximum_failes = _function_argument("MaximumFailes");
	var TLSVersion = _function_argument("TLSVersion");
	var Headers = _function_argument("Headers");

	_do(function(){
		var cycle_index = _iterator();
		
		_call(function(){
			_on_fail(function(){
				VAR_LAST_ERROR = _result();
				VAR_ERROR_ID = ScriptWorker.GetCurrentAction();
				VAR_WAS_ERROR = false;
				_break(1,true);
			});
			CYCLES.Current().RemoveLabel("function");
			
	 		 nativeHttpClient_set_timeout(timeout);
			_nativeHttpClient_download({url: value, savePath: file, headers:Headers, tls:TLSVersion})
		},null)!
		
		_if_else(VAR_WAS_ERROR, function(){
			if(cycle_index>=maximum_failes) {
				fail(VAR_LAST_ERROR)
			}
			sleep(1000)!
		}, function(){
			_break("function")
		})!
	})!
};

function nativeHttpClient_Post(){
	var value = _function_argument("Value");
	var params = _function_argument("Params");
	var content_type = _function_argument("ContentType");
	var encoding =  _function_argument("Encoding");
	var method = _function_argument("Method");
	var headers = _function_argument("Headers");
	var maximum_failes = _function_argument("MaximumFailes");
	var redirect = _function_argument("Redirect");
	var timeout = _function_argument("Timeout");
	var TLSVersion = _function_argument("TLSVersion");
	var bodyBase64 = _function_argument("bodyBase64");

	_do(function(){
		var cycle_index = _iterator();
		
		_call(function(){
			_on_fail(function(){
				VAR_LAST_ERROR = _result();
				VAR_ERROR_ID = ScriptWorker.GetCurrentAction();
				VAR_WAS_ERROR = false;
				_break(1,true);
			});
			CYCLES.Current().RemoveLabel("function");
			
	 nativeHttpClient_set_timeout(timeout);
	 var result = nativeHttpClient_send("post",{method:(method), url:(value),bodyBase64:bodyBase64,body:params,contentType: content_type, tls:(TLSVersion), headers:(headers)}, redirect);
	
	},null)!
		
	_if_else(VAR_WAS_ERROR, function(){
			if(cycle_index>=maximum_failes) {
				fail(VAR_LAST_ERROR)
			}
			sleep(1000)!
		}, function(){
			_break("function")
		})!
	})!
};