rojo/plugin/http/init.lua
Joe 25ca0bb27e Improve logging performance by conditionally checking trace level
- Introduced a new `isTraceEnabled` function in the logging module to check if trace logging is enabled based on the current log level.
- Updated the `performRequest` function to only log HTTP request details and responses if trace logging is enabled, reducing unnecessary log output and improving performance.

These changes aim to enhance the efficiency of logging during HTTP requests by avoiding excessive logging when not needed.
2026-03-01 16:43:21 -06:00

85 lines
1.8 KiB
Lua

local HttpService = game:GetService("HttpService")
local Log = require(script.Parent.Log)
local msgpack = require(script.Parent.msgpack)
local Promise = require(script.Parent.Promise)
local HttpError = require(script.Error)
local HttpResponse = require(script.Response)
local lastRequestId = 0
local Http = {}
Http.Error = HttpError
Http.Response = HttpResponse
local function performRequest(requestParams)
local requestId = lastRequestId + 1
lastRequestId = requestId
if Log.isTraceEnabled() then
Log.trace("HTTP {}({}) {}", requestParams.Method, requestId, requestParams.Url)
if requestParams.Body ~= nil then
Log.trace("{}", requestParams.Body)
end
end
return Promise.new(function(resolve, reject)
coroutine.wrap(function()
local success, response = pcall(function()
return HttpService:RequestAsync(requestParams)
end)
if success then
if Log.isTraceEnabled() then
Log.trace("Request {} success, response {:#?}", requestId, response)
end
local httpResponse = HttpResponse.fromRobloxResponse(response)
if httpResponse:isSuccess() then
resolve(httpResponse)
else
reject(HttpError.fromResponse(httpResponse))
end
else
if Log.isTraceEnabled() then
Log.trace("Request {} failure: {:?}", requestId, response)
end
reject(HttpError.fromRobloxErrorString(response))
end
end)()
end)
end
function Http.get(url)
return performRequest({
Url = url,
Method = "GET",
})
end
function Http.post(url, body)
return performRequest({
Url = url,
Method = "POST",
Body = body,
})
end
function Http.jsonEncode(object)
return HttpService:JSONEncode(object)
end
function Http.jsonDecode(source)
return HttpService:JSONDecode(source)
end
function Http.msgpackEncode(object)
return msgpack.encode(object)
end
function Http.msgpackDecode(source)
return msgpack.decode(source)
end
return Http