fix: http json parser

This commit is contained in:
Linventif 2024-03-31 14:13:31 +02:00
parent 8cdce17cab
commit d9340b2436
No known key found for this signature in database
GPG Key ID: FAC0CA60F9AEEC24

View File

@ -25,43 +25,34 @@ local function showableBody(endpoint)
return true return true
end end
local function genRequestID()
return "gmInte-" .. util.CRC(tostring(SysTime()))
end
local lastErrorMessages = 0
local function noTokenError()
if CurTime() - lastErrorMessages < 5 then return end
lastErrorMessages = CurTime()
gmInte.log("HTTP Failed: No token provided")
end
function gmInte.http.requestAPI(params) function gmInte.http.requestAPI(params)
local body = params.body && util.TableToJSON(params.body || {}) || "" local body = params.body && util.TableToJSON(params.body || {}) || ""
local bodyLength = string.len(body) local bodyLength = string.len(body)
local token = params.token || gmInte.config.token || "" local token = params.token || gmInte.config.token || ""
local url = getAPIURL(params.endpoint) local url = getAPIURL(params.endpoint || "")
local method = params.method local method = params.method || "GET"
local success = params.success || function() end local success = params.success || function() end
local failed = params.failed || function() end local failed = params.failed || function() end
local version = gmInte.config.version local version = gmInte.config.version || "Unknown"
local showableBody = showableBody(params.endpoint) local showableBody = showableBody(params.endpoint)
local requestID = genRequestID() local localRequestID = util.CRC(tostring(SysTime()))
if (token == "") then if (token == "") then
noTokenError() return failed(401, {
return failed(401, "No token provided") ["error"] = "No token provided"
})
end end
local headers = { local headers = {
["Content-Type"] = "application/json", ["Content-Type"] = "application/json",
["Content-Length"] = bodyLength, ["Content-Length"] = bodyLength,
["Authorization"] = "Bearer " .. token, ["Authorization"] = "Bearer " .. token,
["Version"] = version ["Gmod-Integrations-Version"] = version,
["Gmod-Integrations-Request-ID"] = localRequestID
} }
gmInte.log("HTTP FQDN: " .. gmInte.config.apiFQDN, true) gmInte.log("HTTP FQDN: " .. gmInte.config.apiFQDN, true)
gmInte.log("HTTP Request ID: " .. requestID, true) gmInte.log("HTTP Request ID: " .. localRequestID, true)
gmInte.log("HTTP Request: " .. method .. " " .. url, true) gmInte.log("HTTP Request: " .. method .. " " .. url, true)
gmInte.log("HTTP Body: " .. (showableBody && body || "HIDDEN"), true) gmInte.log("HTTP Body: " .. (showableBody && body || "HIDDEN"), true)
@ -72,15 +63,10 @@ function gmInte.http.requestAPI(params)
["body"] = body, ["body"] = body,
["type"] = "application/json", ["type"] = "application/json",
["success"] = function(code, body, headers) ["success"] = function(code, body, headers)
gmInte.log("HTTP Request ID: " .. requestID, true) gmInte.log("HTTP Request ID: " .. localRequestID, true)
gmInte.log("HTTP Response: " .. code, true) gmInte.log("HTTP Response: " .. code, true)
gmInte.log("HTTP Body: " .. body, true) gmInte.log("HTTP Body: " .. body, true)
if (code < 200 || code >= 300) then
gmInte.log("HTTP Failed: Invalid Status Code", true)
return failed(code, body)
end
if (string.sub(headers["Content-Type"], 1, 16) != "application/json") then if (string.sub(headers["Content-Type"], 1, 16) != "application/json") then
gmInte.log("HTTP Failed: Invalid Content-Type", true) gmInte.log("HTTP Failed: Invalid Content-Type", true)
return failed(code, body) return failed(code, body)
@ -88,10 +74,15 @@ function gmInte.http.requestAPI(params)
body = util.JSONToTable(body || "{}") body = util.JSONToTable(body || "{}")
if (code < 200 || code >= 300) then
gmInte.log("HTTP Failed: Invalid Status Code", true)
return failed(code, body)
end
return success(code, body) return success(code, body)
end, end,
["failed"] = function(error) ["failed"] = function(error)
gmInte.log("HTTP Request ID: " .. requestID, true) gmInte.log("HTTP Request ID: " .. localRequestID, true)
gmInte.log("HTTP Failed: " .. error, true) gmInte.log("HTTP Failed: " .. error, true)
end end
}) })