mirror of
https://github.com/gmod-integration/lua.git
synced 2025-03-15 22:07:34 +00:00
fix: better log
This commit is contained in:
parent
ad609e3ae2
commit
823bcf9ddc
|
@ -42,7 +42,7 @@ function gmInte.http.requestAPI(params)
|
||||||
local url = getAPIURL(params.endpoint)
|
local url = getAPIURL(params.endpoint)
|
||||||
local method = params.method
|
local method = params.method
|
||||||
local success = params.success || function() end
|
local success = params.success || function() end
|
||||||
local failed = params.failed || function() if (!gmInte.config.debug) then gmInte.log("HTTP Failed, if this error persists please contact support") end end
|
local failed = params.failed || function() end
|
||||||
local version = gmInte.config.version
|
local version = gmInte.config.version
|
||||||
local showableBody = showableBody(params.endpoint)
|
local showableBody = showableBody(params.endpoint)
|
||||||
local requestID = genRequestID()
|
local requestID = genRequestID()
|
||||||
|
@ -57,51 +57,40 @@ function gmInte.http.requestAPI(params)
|
||||||
["Authorization"] = "Bearer " .. token,
|
["Authorization"] = "Bearer " .. token,
|
||||||
["Version"] = version
|
["Version"] = version
|
||||||
}
|
}
|
||||||
local type = "application/json"
|
|
||||||
|
|
||||||
// Log
|
|
||||||
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: " .. requestID, 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)
|
||||||
|
|
||||||
// Send
|
|
||||||
HTTP({
|
HTTP({
|
||||||
["url"] = url,
|
["url"] = url,
|
||||||
["method"] = method,
|
["method"] = method,
|
||||||
["headers"] = headers,
|
["headers"] = headers,
|
||||||
["body"] = body,
|
["body"] = body,
|
||||||
["type"] = type,
|
["type"] = "application/json",
|
||||||
["success"] = function(code, body, headers)
|
["success"] = function(code, body, headers)
|
||||||
// Log
|
|
||||||
gmInte.log("HTTP Request ID: " .. requestID, true)
|
gmInte.log("HTTP Request ID: " .. requestID, true)
|
||||||
gmInte.log("HTTP Response: " .. code, true)
|
gmInte.log("HTTP Response: " .. code, true)
|
||||||
if (gmInte.config.debug) then gmInte.log("HTTP Body: " .. body, true) end
|
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 not application/json return failed
|
|
||||||
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({ ["error"] = "Invalid Content-Type" }, code, headers)
|
return failed(code, body)
|
||||||
end
|
end
|
||||||
|
|
||||||
// Parse body
|
|
||||||
body = util.JSONToTable(body || "{}")
|
body = util.JSONToTable(body || "{}")
|
||||||
|
|
||||||
// if not 2xx return failed
|
|
||||||
if (code < 200 || code >= 300) then
|
|
||||||
return failed(code, body, headers)
|
|
||||||
end
|
|
||||||
|
|
||||||
// Return success
|
|
||||||
return success(code, body)
|
return success(code, body)
|
||||||
end,
|
end,
|
||||||
["failed"] = function(error)
|
["failed"] = function(error)
|
||||||
// Log
|
|
||||||
gmInte.log("HTTP Request ID: " .. requestID, true)
|
gmInte.log("HTTP Request ID: " .. requestID, true)
|
||||||
gmInte.log("HTTP Failed: " .. error, true)
|
gmInte.log("HTTP Failed: " .. error, true)
|
||||||
|
|
||||||
// Return failed
|
|
||||||
return failed({ ["error"] = error })
|
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user