local j=require(script.Parent.HttpPromise)local R=require(script.Parent.Promise)local A={}local function d(j,R)if R then return function(R)warn(string.format("\070\117\110\099\116\105\111\110 \037\115 \102\097\105\108\101\100 \116\111 \101\120\101\099\117\116\101\044 \103\111\116 \101\114\114\111\114 \037\115\046",j,R))return false end else return function(R)warn(string.format("\070\117\110\099\116\105\111\110 \037\115 \102\097\105\108\101\100 \116\111 \101\120\101\099\117\116\101\044 \103\111\116 \101\114\114\111\114 \037\115\046",j,R))end end end function A.HttpGetAsync(R,A,B)B=B or{}if A then B["\067\097\099\104\101-\067\111\110\116\114\111\108"]="\110\111-\099\097\099\104\101"end local W={Url=R;Method="\071\069\084";Headers=B}local Y=d("\072\116\116\112\071\101\116\065\115\121\110\099",true)return((j.promiseRequest(W)):andThen(function(R)if R.Success then local A=R.Body local d,B,W=string.find(A,"\"\"\"\040\046\043\041\"\"\"\036")if W then return((j.promiseDecode(W)):andThen(function(j)return{Body=string.sub(A,1,d-1);Headers=R.Headers,StatusCode=R.StatusCode,StatusMessage=R.StatusMessage}end)):catch(Y)end else return false end end)):catch(Y)end function A.OldHttpGetAsync(...)local R=d("\072\116\116\112\071\101\116\065\115\121\110\099",true)return((j.promiseGet(...)):andThen(function(A)local d,B,W=string.find(A,"\"\"\"\040\046\043\041\"\"\"\036")if W then return((j.promiseDecode(W)):andThen(function(j)return{headers=j.headers;status=j.status,body=string.sub(A,1,d-1)}end)):catch(R)else return false end end)):catch(R)end function A.HttpPostAsync(...)local R=d("\072\116\116\112\080\111\115\116\065\115\121\110\099",true)return((j.promisePost(...)):andThen(function(A)local d,B,W=string.find(A,"\"\"\"\040\046\043\041\"\"\"\036")if W then return((j.promiseDecode(W)):andThen(function(j)return{headers=j.headers;status=j.status;body=string.sub(A,1,d-1)}end)):catch(R)else return false end end)):catch(R)end return A
local HttpPromise = require(script.Parent.HttpPromise)
local Promise = require(script.Parent.Promise)

local Privates = {}

local function CatchFactory(FunctionName, DoReject)
	if DoReject then
		return function(Error)
			warn(string.format("Function %s failed to execute, got error %s.", FunctionName, Error))
			return false
		end
	else
		return function(Error)
			warn(string.format("Function %s failed to execute, got error %s.", FunctionName, Error))
		end
	end
end

--local function PrintTable(Table, TableName, IndentLevel)
--	print(string.format("%s%s = {", string.rep("\t", IndentLevel), TableName))
--	for Index, Value in next, Table do
--		local TypeValue = typeof(Value)
--		if TypeValue == "table" then
--			PrintTable(Value, tostring(Index), IndentLevel + 1)
--		else
--			print(string.format("%s[%s (%s)] = %s; (%s)", string.rep("\t", IndentLevel + 1), tostring(Index), typeof(Index), tostring(Value), TypeValue))
--		end
--	end
--	print(string.rep("\t", IndentLevel) .. "}")
--end

function Privates.HttpGetAsync(Url, NoCache, Headers)
	Headers = Headers or {}
	if NoCache then
		Headers["Cache-Control"] = "no-cache"
	end

	local RequestDictionary = {
		Url = Url;
		Method = "GET";
		Headers = Headers;
	}

	local CatchFunction = CatchFactory("HttpGetAsync", true)
	return HttpPromise.promiseRequest(RequestDictionary):andThen(function(ResponseData)
		if ResponseData.Success then
			local Body = ResponseData.Body
			local Position, _, Match = string.find(Body, "\"\"\"(.+)\"\"\"$")
			if Match then
				return HttpPromise.promiseDecode(Match):andThen(function(Data)
					return {
						Body = string.sub(Body, 1, Position - 1);
						Headers = ResponseData.Headers; -- Data.headers is slightly different but this shouldn't break, right?
						StatusCode = ResponseData.StatusCode;
						StatusMessage = ResponseData.StatusMessage;
					}
				end):catch(CatchFunction)
			end
		else
			return false
		end
	end):catch(CatchFunction)
end

function Privates.OldHttpGetAsync(...)
	local CatchFunction = CatchFactory("HttpGetAsync", true)

	return HttpPromise.promiseGet(...):andThen(function(Body)
		local Position, _, Match = string.find(Body, "\"\"\"(.+)\"\"\"$")
		if Match then
			return HttpPromise.promiseDecode(Match):andThen(function(Data)
				return {
					headers = Data.headers;
					status = Data.status;
					body = string.sub(Body, 1, Position - 1);
				}
			end):catch(CatchFunction)
		else
			return false
		end
	end):catch(CatchFunction)
end

function Privates.HttpPostAsync(...)
	local CatchFunction = CatchFactory("HttpPostAsync", true)

	return HttpPromise.promisePost(...):andThen(function(Body)
		local Position, _, Match = string.find(Body, "\"\"\"(.+)\"\"\"$")
		if Match then
			return HttpPromise.promiseDecode(Match):andThen(function(Data)
				return {
					headers = Data.headers;
					status = Data.status;
					body = string.sub(Body, 1, Position - 1);
				}
			end):catch(CatchFunction)
		else
			return false
		end
	end):catch(CatchFunction)
end

return Privates