From 5eb9f82bfe07bb02d8b7d677d2589038ba8d6da5 Mon Sep 17 00:00:00 2001
From: tommy <thefamousdoge@hotmail.com>
Date: Mon, 23 Jun 2025 22:39:52 -0400
Subject: [PATCH] Refactor HTTP Request module

---
 src/Modules/HttpRequest.lua | 82 +++++++++++++++++++++++++++++++++++++
 src/Modules/http.lua        | 40 ------------------
 2 files changed, 82 insertions(+), 40 deletions(-)
 create mode 100644 src/Modules/HttpRequest.lua
 delete mode 100644 src/Modules/http.lua

diff --git a/src/Modules/HttpRequest.lua b/src/Modules/HttpRequest.lua
new file mode 100644
index 0000000..1232369
--- /dev/null
+++ b/src/Modules/HttpRequest.lua
@@ -0,0 +1,82 @@
+local Http = require('coro-http')
+local HTTPRequest = Http.request
+
+local json = require('json')
+
+local METHODS = {
+    GET = true,
+    POST = true
+}
+
+local function QueryParams(Params) -- {Name = Value, ...}
+    local QueryString = "?"
+
+    for ParamName, ParamValue in next, Params do
+        if ParamValue ~= nil then
+            QueryString = QueryString .. ParamName .. "=" .. ParamValue .. "&"
+        end
+    end
+
+    return string.sub(QueryString, 1, -2) -- Remove last character (will always be a "&")
+end
+
+local function CreateHeaders(Headers) -- {Name = Value, ...}
+    local RequestHeaders = {}
+
+    for HeaderName, HeaderValue in next, Headers do
+        RequestHeaders[#RequestHeaders + 1] = { HeaderName, HeaderValue }
+    end
+
+    return RequestHeaders
+end
+
+local function TryDecodeJson(Body)
+    local Success, Result = pcall(json.decode, Body)
+    if not Success then
+        return Body
+    end
+    return Result
+end
+
+local function NormalizeHeaders(Response)
+    for Index, Header in next, Response do
+        if type(Header) == "table" and #Header == 2 then
+            local HeaderName, HeaderValue = table.unpack(Header)
+            Response[HeaderName] = HeaderValue
+            Response[Index] = nil
+        end
+    end
+end
+
+local function Request(Method, Url, Params, Headers, Callback)
+    if not METHODS[Method] then
+        error("[HTTP] Method " .. Method .. " is not supported.")
+    end
+
+    if type(Url) ~= "string" then
+        error("[HTTP] Url is not a string")
+    end
+
+    local QueryString = QueryParams(Params)       -- at worse (I think), this is an empty string (which cannot mess up the request)
+
+    local RequestHeaders = CreateHeaders(Headers) -- At worse, this will just be an empty table (which cannot mess up the request)
+
+    local RequestUrl = Url .. QueryString
+    print(RequestUrl)
+
+    if Callback and type(Callback) == "function" then
+        return coroutine.wrap(function()
+            local Response, Body = HTTPRequest(Method, RequestUrl, RequestHeaders)
+            NormalizeHeaders(Response)
+            Callback(Response, TryDecodeJson(Body))
+        end)
+    else
+        local Response, Body = HTTPRequest(Method, RequestUrl, RequestHeaders)
+        NormalizeHeaders(Response)
+        return Response, TryDecodeJson(Body)
+    end
+end
+
+return {
+    Request = Request
+}
diff --git a/src/Modules/http.lua b/src/Modules/http.lua
deleted file mode 100644
index 3b1d753..0000000
--- a/src/Modules/http.lua
+++ /dev/null
@@ -1,40 +0,0 @@
-local http = require('coro-http')
-local json = require('json')
-function wait(n)local c=os.clock local t=c()while c()-t<=n do end;end
---[[
-1: method
-2: url
-3: headers
-4: body
-5: options]]
-local STRAFES_NET_RATELIMIMT = {
-    HOUR = 3000,
-    MINUTE = 100,
-}
-local remaining_timeout = 0
-local function request(method,url,headers,body,options)
-    if type(body)=='table' then body=json.encode(body) end
-    local headers,body=http.request(method,url,headers,body,options)
-    local rbody=json.decode(body) or body
-    local rheaders={}
-    for _,t in pairs(headers) do
-        if type(t)=='table' then
-            rheaders[t[1]]=t[2]
-        else
-            rheaders[_]=t
-        end
-    end
-    local remaining = tonumber(rheaders['RateLimit-Remaining'])
-    local remaining_hour = tonumber(rheaders['X-RateLimit-Remaining-Hour'])
-    local reset = tonumber(rheaders['RateLimit-Reset'])
-    local retry_after = tonumber(rheaders['Retry-After'])
-    if remaining and reset then
-        local t = remaining==0 and reset or .38
-        if retry_after then t = retry_after end
-        wait(t)
-    end
-    return rbody,rheaders
-end
-
--- local urlparamencode=function()
-return request
\ No newline at end of file