Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically enable the HttpService if running from BuiltInPlugins folder. #249

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions plugin/src/Http.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,28 @@ local HttpResponse = require(script.Parent.HttpResponse)

local lastRequestId = 0

local isBuiltInPlugin = pcall(function ()
-- This is a hack, but there is no explicit way to detect the security context of a thread.
-- If this routine runs without throwing, then we are running from the BuiltInPlugins folder!
local CorePackages = game:GetService("CorePackages")
tostring(CorePackages)
end)

-- TODO: Factor out into separate library, especially error handling
local Http = {}

local function tryEnableHttp()
if isBuiltInPlugin and not HttpService:GetHttpEnabled() then
Logging.warn("Automatically enabling the HttpService.")
HttpService:SetHttpEnabled(true)
end
end

function Http.get(url)
local requestId = lastRequestId + 1
lastRequestId = requestId


tryEnableHttp()
Logging.trace("GET(%d) %s", requestId, url)

return Promise.new(function(resolve, reject)
Expand All @@ -41,6 +56,7 @@ function Http.post(url, body)
local requestId = lastRequestId + 1
lastRequestId = requestId

tryEnableHttp()
Logging.trace("POST(%d) %s\n%s", requestId, url, body)

return Promise.new(function(resolve, reject)
Expand Down Expand Up @@ -72,4 +88,4 @@ function Http.jsonDecode(source)
return HttpService:JSONDecode(source)
end

return Http
return Http