lua/lua/gmod_integration/shared/sh_http.lua

102 lines
2.2 KiB
Lua
Raw Normal View History

2023-08-09 05:04:43 +00:00
//
// Functions
2023-08-09 05:04:43 +00:00
//
function gmInte.httpError(error)
gmInte.log("Web request failed")
gmInte.log("Error details: "..error)
end
//
// HTTP
//
2023-08-09 05:04:43 +00:00
local function sendHTTP(params)
2023-09-23 12:43:59 +00:00
gmInte.log("HTTP Request: " .. params.method .. " " .. params.endpoint, true)
gmInte.log("HTTP Body: " .. (params.body or "No body"), true)
HTTP({
url = "https://api.gmod-integration.com" .. params.endpoint,
method = params.method,
headers = {
["Content-Type"] = "application/json",
2023-09-23 12:43:59 +00:00
["Content-Length"] = params.body and string.len(params.body) or 0,
["id"] = gmInte.config.id,
["token"] = gmInte.config.token,
["version"] = gmInte.version
},
2023-09-23 12:43:59 +00:00
body = params.body && params.body or "",
type = "application/json",
success = function(code, body, headers)
2023-09-23 12:56:56 +00:00
gmInte.log("HTTP Response: " .. code, true)
gmInte.log("HTTP Body: " .. body, true)
2023-09-25 09:14:13 +00:00
if (string.sub(code, 1, 1) == "2") then
params.success && params.success(body, code, headers)
2023-08-09 05:04:43 +00:00
else
gmInte.httpError(body)
end
end,
failed = gmInte.httpError,
})
2023-08-09 05:04:43 +00:00
end
2023-09-23 12:43:59 +00:00
function gmInte.get(endpoint, onSuccess)
sendHTTP({
2023-09-23 00:35:51 +00:00
endpoint = endpoint,
method = "GET",
success = onSuccess
})
2023-08-09 05:04:43 +00:00
end
function gmInte.post(endpoint, data, onSuccess)
sendHTTP({
endpoint = endpoint,
method = "POST",
body = util.TableToJSON(data),
success = onSuccess
})
2023-08-09 05:04:43 +00:00
end
2023-09-23 12:43:59 +00:00
function gmInte.put(endpoint, data, onSuccess)
sendHTTP({
endpoint = endpoint,
method = "PUT",
body = util.TableToJSON(data),
success = onSuccess
})
end
function gmInte.delete(endpoint, onSuccess)
sendHTTP({
endpoint = endpoint,
method = "DELETE",
success = onSuccess
})
end
2023-08-09 05:04:43 +00:00
/*
// Fetch Example
gmInte.fetch(
// Endpoint
"",
// Parameters
{ request = "requ" },
// onSuccess
function( body, length, headers, code )
print(body)
end
)
// Post Example
gmInte.post(
// Endpoint
"",
// Data
{
data = "data"
},
// onSuccess
function( body, length, headers, code )
print(body)
end
)
*/