local Y=require((game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")):WaitForChild("\078\101\118\101\114\109\111\114\101"))local w=game:GetService("\072\116\116\112\083\101\114\118\105\099\101")local T=Y("\080\114\111\109\105\115\101")local t=false local M=false local p={}function p.Request(Y)if t then print("\083\101\110\100\105\110\103 \114\101\113\117\101\115\116",w:JSONEncode(Y))end return T.spawn(function(T,t)local p local f,y=pcall(function()p=w:RequestAsync(Y)end)if M then print(("\082\101\115\112\111\110\115\101\058 \037\100 \037\115 \037\115"):format(p.StatusCode,Y.Method,Y.Url),p.Body)end if not f then t(y)return end if not p.Success then t(p)return end T(p)return end)end function p.LogFailedRequests(...)for Y,w in pairs({...})do if type(w)=="\115\116\114\105\110\103"then warn(w)elseif type(w)=="\116\097\098\108\101"and type(w.StatusCode)=="\110\117\109\098\101\114"then warn(("\070\097\105\108\101\100 \114\101\113\117\101\115\116 \037\100"):format(w.StatusCode,tostring(w.Body)))end end end function p.DecodeJson(Y)assert(Y)if type(Y.Body)~="\115\116\114\105\110\103"then return T.rejected("\066\111\100\121 \105\115 \110\111\116 \111\102 \116\121\112\101 \115\116\114\105\110\103")end return T.new(function(T,t)local M local p,f=pcall(function()M=w:JSONDecode(Y.Body)end)if not p then t(f)return elseif M then T(M)return else t("\100\101\099\111\100\101\100 \110\111\116\104\105\110\103")return end end)end return p
--- Provides a wrapper around HttpService with a promise API
-- @module HttpPromise

local require = require(game:GetService("ReplicatedStorage"):WaitForChild("Nevermore"))

local HttpService = game:GetService("HttpService")

local Promise = require("Promise")

local DEBUG_REQUEST = false
local DEBUG_RESPONSE = false

local HttpPromise = {}

function HttpPromise.Request(request)
	if DEBUG_REQUEST then
		print("Sending request", HttpService:JSONEncode(request))
	end

	return Promise.spawn(function(resolve, reject)
		local response
		local ok, err = pcall(function()
			response = HttpService:RequestAsync(request)
		end)

		if DEBUG_RESPONSE then
			print(("Response: %d %s %s"):format(response.StatusCode, request.Method, request.Url), response.Body)
		end

		if not ok then
			reject(err)
			return
		end

		if not response.Success then
			reject(response)
			return
		end

		resolve(response)
		return
	end)
end

function HttpPromise.LogFailedRequests(...)
	for _, item in pairs({...}) do
		if type(item) == "string" then
			warn(item)
		elseif type(item) == "table" and type(item.StatusCode) == "number" then
			warn(("Failed request %d"):format(item.StatusCode, tostring(item.Body)))
		end
	end
end

function HttpPromise.DecodeJson(response)
	assert(response)
	if type(response.Body) ~= "string" then
		return Promise.rejected("Body is not of type string")
	end

	return Promise.new(function(resolve, reject)
		local decoded
		local ok, err = pcall(function()
			decoded = HttpService:JSONDecode(response.Body)
		end)

		if not ok then
			reject(err)
			return
		elseif decoded then
			resolve(decoded)
			return
		else
			reject("decoded nothing")
			return
		end
	end)
end

return HttpPromise
