mirror of
https://github.com/gmod-integration/lua.git
synced 2025-03-15 22:27:34 +00:00
refactor: start working on http complet rewrite
This commit is contained in:
parent
43a567750e
commit
a9e13bae51
|
@ -1,42 +1,77 @@
|
||||||
|
local GmodIntegration_HTTP = {}
|
||||||
|
GmodIntegration_HTTP.__index = GmodIntegration_HTTP
|
||||||
|
setmetatable(gmInte, GmodIntegration_HTTP)
|
||||||
|
function GmodIntegration_HTTP:New()
|
||||||
|
local self = setmetatable({}, GmodIntegration_HTTP)
|
||||||
|
self.method = "GET"
|
||||||
|
self.endpoint = "/"
|
||||||
|
self.data = {}
|
||||||
|
self.isStackableLog = false
|
||||||
|
self.requestID = util.CRC(tostring(SysTime()))
|
||||||
|
self.success = function() end
|
||||||
|
self.failed = function() end
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
function GmodIntegration_HTTP:Method(method)
|
||||||
|
self.method = method.upper()
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
function GmodIntegration_HTTP:Endpoint(endpoint)
|
||||||
|
self.endpoint = endpoint
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
function GmodIntegration_HTTP:Data(data)
|
||||||
|
self.data = data
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
function GmodIntegration_HTTP:OnSuccess(callback)
|
||||||
|
self.success = callback
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
function GmodIntegration_HTTP:OnFailed(callback)
|
||||||
|
self.failed = callback
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
function GmodIntegration_HTTP:IsStackableLog(isStackable)
|
||||||
|
self.isStackableLog = isStackable
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
local apiVersion = "v3"
|
local apiVersion = "v3"
|
||||||
gmInte.http = gmInte.http || {}
|
function GmodIntegration_HTTP:GetFinalUrl()
|
||||||
local function getAPIURL(endpoint)
|
|
||||||
local method = gmInte.isPrivateIP(gmInte.config.apiFQDN) && "http" || "https"
|
local method = gmInte.isPrivateIP(gmInte.config.apiFQDN) && "http" || "https"
|
||||||
endpoint = string.gsub(endpoint, ":serverID", gmInte.config.id)
|
local endpoint = string.gsub(self.endpoint, ":serverID", gmInte.config.id)
|
||||||
if CLIENT then endpoint = string.gsub(endpoint, ":steamID64", LocalPlayer():SteamID64()) end
|
if CLIENT then endpoint = string.gsub(endpoint, ":steamID64", LocalPlayer():SteamID64()) end
|
||||||
return method .. "://" .. gmInte.config.apiFQDN .. "/" .. apiVersion .. endpoint
|
return method .. "://" .. gmInte.config.apiFQDN .. "/" .. apiVersion .. endpoint
|
||||||
end
|
end
|
||||||
|
|
||||||
local function showableBody(endpoint)
|
function GmodIntegration_HTTP:ShowableBody()
|
||||||
if string.sub(endpoint, 1, 8) == "/streams" || string.sub(endpoint, 1, 12) == "/screenshots" then return false end
|
if string.sub(self.endpoint, 1, 8) == "/streams" || string.sub(self.endpoint, 1, 12) == "/screenshots" then return false end
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
function gmInte.http.requestAPI(params)
|
function GmodIntegration_HTTP:Send()
|
||||||
local body = params.body && util.TableToJSON(params.body || {}) || ""
|
local body = util.TableToJSON(self.data)
|
||||||
local bodyLength = string.len(body)
|
local token = gmInte.config.token
|
||||||
local token = params.token || gmInte.config.token || ""
|
local url = self:GetFinalUrl()
|
||||||
local url = getAPIURL(params.endpoint || "")
|
local method = self.method
|
||||||
local method = params.method || "GET"
|
local success = self.success
|
||||||
local success = params.success || function() end
|
local failed = self.failed
|
||||||
local failed = params.failed || function() end
|
local version = gmInte.version
|
||||||
local version = gmInte.version || "Unknown"
|
local showableBody = self:ShowableBody()
|
||||||
local showableBody = showableBody(params.endpoint)
|
local localRequestID = self.requestID
|
||||||
local localRequestID = util.CRC(tostring(SysTime()))
|
|
||||||
if token == "" then
|
if token == "" then
|
||||||
return failed(401, {
|
return failed(401, {
|
||||||
["error"] = "No token provided"
|
["error"] = "No token provided"
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
local headers = {
|
|
||||||
["Content-Type"] = "application/json",
|
|
||||||
["Content-Length"] = bodyLength,
|
|
||||||
["Authorization"] = "Bearer " .. token,
|
|
||||||
["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: " .. localRequestID, true)
|
gmInte.log("HTTP Request ID: " .. localRequestID, true)
|
||||||
gmInte.log("HTTP Request: " .. method .. " " .. url, true)
|
gmInte.log("HTTP Request: " .. method .. " " .. url, true)
|
||||||
|
@ -44,14 +79,20 @@ function gmInte.http.requestAPI(params)
|
||||||
HTTP({
|
HTTP({
|
||||||
["url"] = url,
|
["url"] = url,
|
||||||
["method"] = method,
|
["method"] = method,
|
||||||
["headers"] = headers,
|
["headers"] = {
|
||||||
|
["Content-Type"] = "application/json",
|
||||||
|
["Content-Length"] = string.len(body),
|
||||||
|
["Authorization"] = "Bearer " .. token,
|
||||||
|
["Gmod-Integrations-Version"] = version,
|
||||||
|
["Gmod-Integrations-Request-ID"] = localRequestID
|
||||||
|
},
|
||||||
["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: " .. localRequestID, 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 string.sub(headers["Content-Type"], 1, 16) != "application/json" then
|
if !headers["Content-Type"] || 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)
|
||||||
end
|
end
|
||||||
|
@ -72,40 +113,28 @@ function gmInte.http.requestAPI(params)
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
// Retrocompatibility
|
||||||
|
gmInte.http = {}
|
||||||
|
local function retroCompatibilityRequest(mathod, endpoint, data, onSuccess, onFailed)
|
||||||
|
local newRequest = GmodIntegration_HTTP:New():Endpoint(endpoint)
|
||||||
|
if data then newRequest:Data(data) end
|
||||||
|
if onSuccess then newRequest:OnSuccess(onSuccess) end
|
||||||
|
if onFailed then newRequest:OnFailed(onFailed) end
|
||||||
|
newRequest:Send()
|
||||||
|
end
|
||||||
|
|
||||||
function gmInte.http.get(endpoint, onSuccess, onFailed)
|
function gmInte.http.get(endpoint, onSuccess, onFailed)
|
||||||
gmInte.http.requestAPI({
|
retroCompatibilityRequest("GET", endpoint, nil, onSuccess, onFailed)
|
||||||
["endpoint"] = endpoint,
|
|
||||||
["method"] = "GET",
|
|
||||||
["success"] = onSuccess,
|
|
||||||
["failed"] = onFailed
|
|
||||||
})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function gmInte.http.post(endpoint, data, onSuccess, onFailed)
|
function gmInte.http.post(endpoint, data, onSuccess, onFailed)
|
||||||
gmInte.http.requestAPI({
|
retroCompatibilityRequest("POST", endpoint, data, onSuccess, onFailed)
|
||||||
["endpoint"] = endpoint,
|
|
||||||
["method"] = "POST",
|
|
||||||
["body"] = data,
|
|
||||||
["success"] = onSuccess,
|
|
||||||
["failed"] = onFailed
|
|
||||||
})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function gmInte.http.put(endpoint, data, onSuccess, onFailed)
|
function gmInte.http.put(endpoint, data, onSuccess, onFailed)
|
||||||
gmInte.http.requestAPI({
|
retroCompatibilityRequest("PUT", endpoint, data, onSuccess, onFailed)
|
||||||
["endpoint"] = endpoint,
|
|
||||||
["method"] = "PUT",
|
|
||||||
["body"] = data,
|
|
||||||
["success"] = onSuccess,
|
|
||||||
["failed"] = onFailed
|
|
||||||
})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function gmInte.http.delete(endpoint, onSuccess, onFailed)
|
function gmInte.http.delete(endpoint, onSuccess, onFailed)
|
||||||
gmInte.http.requestAPI({
|
retroCompatibilityRequest("DELETE", endpoint, nil, onSuccess, onFailed)
|
||||||
["endpoint"] = endpoint,
|
|
||||||
["method"] = "DELETE",
|
|
||||||
["success"] = onSuccess,
|
|
||||||
["failed"] = onFailed
|
|
||||||
})
|
|
||||||
end
|
end
|
Loading…
Reference in New Issue
Block a user