From a9e13bae51da10e129a864e2cde6904a12bbe069 Mon Sep 17 00:00:00 2001 From: Linventif Date: Sun, 11 Aug 2024 01:11:41 +0000 Subject: [PATCH] refactor: start working on http complet rewrite --- lua/gmod_integration/shared/sh_http.lua | 133 +++++++++++++++--------- 1 file changed, 81 insertions(+), 52 deletions(-) diff --git a/lua/gmod_integration/shared/sh_http.lua b/lua/gmod_integration/shared/sh_http.lua index b44ca3d..4a6b2d3 100644 --- a/lua/gmod_integration/shared/sh_http.lua +++ b/lua/gmod_integration/shared/sh_http.lua @@ -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" -gmInte.http = gmInte.http || {} -local function getAPIURL(endpoint) +function GmodIntegration_HTTP:GetFinalUrl() 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 return method .. "://" .. gmInte.config.apiFQDN .. "/" .. apiVersion .. endpoint end -local function showableBody(endpoint) - if string.sub(endpoint, 1, 8) == "/streams" || string.sub(endpoint, 1, 12) == "/screenshots" then return false end +function GmodIntegration_HTTP:ShowableBody() + if string.sub(self.endpoint, 1, 8) == "/streams" || string.sub(self.endpoint, 1, 12) == "/screenshots" then return false end return true end -function gmInte.http.requestAPI(params) - local body = params.body && util.TableToJSON(params.body || {}) || "" - local bodyLength = string.len(body) - local token = params.token || gmInte.config.token || "" - local url = getAPIURL(params.endpoint || "") - local method = params.method || "GET" - local success = params.success || function() end - local failed = params.failed || function() end - local version = gmInte.version || "Unknown" - local showableBody = showableBody(params.endpoint) - local localRequestID = util.CRC(tostring(SysTime())) +function GmodIntegration_HTTP:Send() + local body = util.TableToJSON(self.data) + local token = gmInte.config.token + local url = self:GetFinalUrl() + local method = self.method + local success = self.success + local failed = self.failed + local version = gmInte.version + local showableBody = self:ShowableBody() + local localRequestID = self.requestID if token == "" then return failed(401, { ["error"] = "No token provided" }) 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 Request ID: " .. localRequestID, true) gmInte.log("HTTP Request: " .. method .. " " .. url, true) @@ -44,14 +79,20 @@ function gmInte.http.requestAPI(params) HTTP({ ["url"] = url, ["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, ["type"] = "application/json", ["success"] = function(code, body, headers) gmInte.log("HTTP Request ID: " .. localRequestID, true) gmInte.log("HTTP Response: " .. code, 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) return failed(code, body) end @@ -72,40 +113,28 @@ function gmInte.http.requestAPI(params) }) 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) - gmInte.http.requestAPI({ - ["endpoint"] = endpoint, - ["method"] = "GET", - ["success"] = onSuccess, - ["failed"] = onFailed - }) + retroCompatibilityRequest("GET", endpoint, nil, onSuccess, onFailed) end function gmInte.http.post(endpoint, data, onSuccess, onFailed) - gmInte.http.requestAPI({ - ["endpoint"] = endpoint, - ["method"] = "POST", - ["body"] = data, - ["success"] = onSuccess, - ["failed"] = onFailed - }) + retroCompatibilityRequest("POST", endpoint, data, onSuccess, onFailed) end function gmInte.http.put(endpoint, data, onSuccess, onFailed) - gmInte.http.requestAPI({ - ["endpoint"] = endpoint, - ["method"] = "PUT", - ["body"] = data, - ["success"] = onSuccess, - ["failed"] = onFailed - }) + retroCompatibilityRequest("PUT", endpoint, data, onSuccess, onFailed) end function gmInte.http.delete(endpoint, onSuccess, onFailed) - gmInte.http.requestAPI({ - ["endpoint"] = endpoint, - ["method"] = "DELETE", - ["success"] = onSuccess, - ["failed"] = onFailed - }) + retroCompatibilityRequest("DELETE", endpoint, nil, onSuccess, onFailed) end \ No newline at end of file