local j=game:GetService("\072\116\116\112\083\101\114\118\105\099\101")local R=require(script.Parent.Promise)local A={}function A.promiseGuid(A)return R.new(function(R,d)local B,W=pcall(j.GenerateGUID,j,A);(B and R or d)(W)end)end function A.promiseDecode(A)return R.new(function(R,d)local B,W=pcall(j.JSONDecode,j,A);(B and R or d)(W)end)end function A.promiseEncode(A)return R.new(function(R,d)local B,W=pcall(j.JSONEncode,j,A);(B and R or d)(W)end)end function A.promiseUrlEncode(A)return R.new(function(R,d)local B,W=pcall(j.UrlEncode,j,A);(B and R or d)(W)end)end function A.promiseRequest(A)return R.defer(function(R,d)local B,W=pcall(j.RequestAsync,j,A);(B and R or d)(W)end)end function A.promiseGet(j,R,d)d=d or{}if R then d["\099\097\099\104\101-\099\111\110\116\114\111\108"]="\110\111-\099\097\099\104\101"end local B={Url=j;Method="\071\069\084";Headers=d}return(A.promiseRequest(B)):andThen(function(j)return j.Body end)end function A.promisePost(d,B,W,Y,m)m=m or{}W=W or Enum.HttpContentType.ApplicationJson Y=Y==nil and false or Y if Y then return R.defer(function(R,A)local s,E=pcall(j.PostAsync,j,d,B,W,Y,m);(s and R or A)(E)end)else if W==Enum.HttpContentType.ApplicationJson then m["\099\111\110\116\101\110\116-\116\121\112\101"]="\097\112\112\108\105\099\097\116\105\111\110\047\106\115\111\110"elseif W==Enum.HttpContentType.ApplicationUrlEncoded then m["\099\111\110\116\101\110\116-\116\121\112\101"]="\097\112\112\108\105\099\097\116\105\111\110\047\120-\119\119\119-\102\111\114\109-\117\114\108\101\110\099\111\100\101\100"elseif W==Enum.HttpContentType.ApplicationXml then m["\099\111\110\116\101\110\116-\116\121\112\101"]="\097\112\112\108\105\099\097\116\105\111\110\047\120\109\108"elseif W==Enum.HttpContentType.TextPlain then m["\099\111\110\116\101\110\116-\116\121\112\101"]="\116\101\120\116\047\112\108\097\105\110"elseif W==Enum.HttpContentType.TextXml then m["\099\111\110\116\101\110\116-\116\121\112\101"]="\116\101\120\116\047\120\109\108"end local j={Url=d;Method="\080\079\083\084",Headers=m,Body=B}return(A.promiseRequest(j)):andThen(function(j)return j.Body end)end end return A
local HttpService = game:GetService("HttpService")
local Promise = require(script.Parent.Promise)

local HttpPromise = {}

--[[**
	Generates a UUID/GUID random string, optionally with curly braces.

	@param [t:optional<t:boolean>] wrapInCurlyBraces Whether the returned string should be wrapped in {curly braces}. Defaults to true.
	@returns [tPlus:promise] A promise which calls HttpService:GenerateGUID.
**--]]
function HttpPromise.promiseGuid(wrapInCurlyBraces)
	return Promise.new(function(resolve, reject)
		local success, value = pcall(HttpService.GenerateGUID, HttpService, wrapInCurlyBraces);
		(success and resolve or reject)(value)
	end)
end

--[[**
	Decodes a JSON string into a Lua table.

	@param [t:string] input The JSON object being decoded.
	@returns [tPlus:promise] A promise which calls HttpService:JSONDecode.
**--]]
function HttpPromise.promiseDecode(input)
	return Promise.new(function(resolve, reject)
		local success, value = pcall(HttpService.JSONDecode, HttpService, input);
		(success and resolve or reject)(value)
	end)
end

--[[**
	Generate a JSON string from a Lua table.

	@param [t:table] input The input Lua table.
	@returns [tPlus:promise] A promise which calls HttpService:JSONEncode.
**--]]
function HttpPromise.promiseEncode(input)
	return Promise.new(function(resolve, reject)
		local success, value = pcall(HttpService.JSONEncode, HttpService, input);
		(success and resolve or reject)(value)
	end)
end

--[[**
	Replaces URL-unsafe characters with ‘%’ and two hexadecimal characters.

	@param [t:string] input The string (URL) to encode.
	@returns [tPlus:promise] A promise which calls HttpService:UrlEncode.
**--]]
function HttpPromise.promiseUrlEncode(input)
	return Promise.new(function(resolve, reject)
		local success, value = pcall(HttpService.UrlEncode, HttpService, input);
		(success and resolve or reject)(value)
	end)
end

--[[**
	Sends an HTTP request using any HTTP method given a dictionary of information.

	@param [t:requestInterface] requestDictionary A dictionary containing information to be requested from the server specified.
	@returns [tPlus:promise] A promise which calls HttpService:RequestAsync.
**--]]
function HttpPromise.promiseRequest(requestOptions)
	return Promise.defer(function(resolve, reject)
		local success, value = pcall(HttpService.RequestAsync, HttpService, requestOptions);
		(success and resolve or reject)(value)
	end)
end

--[[**
	Sends an HTTP GET request.

	@param [t:string] url The web address you are requesting data from.
	@param [t:optional<t:boolean>] noCache Whether the request stores (caches) the response. Defaults to false.
	@param [t:optional<t:any>] headers Used to specify some HTTP request headers.
	@returns [tPlus:promise] A promise which calls HttpService:GetAsync.
**--]]
function HttpPromise.promiseGet(url, noCache, headers)
	headers = headers or {}
	if noCache then
		headers["cache-control"] = "no-cache"
	end

	local requestDictionary = {
		Url = url;
		Method = "GET";
		Headers = headers;
	}

	return HttpPromise.promiseRequest(requestDictionary):andThen(function(response)
		return response.Body
	end)
end

--[[**
	Sends an HTTP POST request.

	@param [t:string] url The destination address for the data.
	@param [t:string] data The data being sent.
	@param [t:optional<t:enum<Enum.HttpContentType>>] contentType Modifies the value in the Content-Type header sent with the request. Defaults to Enum.HttpContentType.ApplicationJson.
	@param [t:optional<t:boolean>] compress Determines whether the data is compressed (gzipped) when sent. Defaults to false.
	@param [t:optional<t:any>] headers Used to specify some HTTP request headers.
	@returns [tPlus:promise] A promise which calls HttpService:PostAsync.
**--]]
function HttpPromise.promisePost(url, data, contentType, compress, headers)
	headers = headers or {}
	contentType = contentType or Enum.HttpContentType.ApplicationJson
	compress = compress == nil and false or compress

	if compress then
		return Promise.defer(function(Resolve, Reject)
			local Success, Value = pcall(HttpService.PostAsync, HttpService, url, data, contentType, compress, headers);
			(Success and Resolve or Reject)(Value)
		end)
	else
		if contentType == Enum.HttpContentType.ApplicationJson then
			headers["content-type"] = "application/json"
		elseif contentType == Enum.HttpContentType.ApplicationUrlEncoded then
			headers["content-type"] = "application/x-www-form-urlencoded"
		elseif contentType == Enum.HttpContentType.ApplicationXml then
			headers["content-type"] = "application/xml"
		elseif contentType == Enum.HttpContentType.TextPlain then
			headers["content-type"] = "text/plain"
		elseif contentType == Enum.HttpContentType.TextXml then
			headers["content-type"] = "text/xml"
		end

		local RequestDictionary = {
			Url = url;
			Method = "POST";
			Headers = headers;
			Body = data;
		}

		return HttpPromise.promiseRequest(RequestDictionary):andThen(function(response)
			return response.Body
		end)
	end
end

return HttpPromise