From ef3a5cf278393781aab693d5036a3f4f9b299b44 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 14 Sep 2026 12:41:12 +0300 Subject: [PATCH] feat: http response as table --- res/scripts/classes.lua | 24 +++++++++++++----- src/logic/scripting/lua/libs/libnetwork.cpp | 28 ++++++++++++++++++--- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/res/scripts/classes.lua b/res/scripts/classes.lua index b044aa478..a6348f23a 100644 --- a/res/scripts/classes.lua +++ b/res/scripts/classes.lua @@ -127,8 +127,12 @@ network.get = function(url, callback, errorCallback, headers) return request(url, { method = "GET", headers = headers, - on_response = callback, - on_error = errorCallback, + on_response = function(response) + callback(response.body) + end, + on_error = function(response) + errorCallback(response.status, response.body) + end, follow_location = true, }) end @@ -137,8 +141,12 @@ network.get_binary = function(url, callback, errorCallback, headers) return request(url, { method = "GET", headers = headers, - on_response = callback and (function (response) return callback(Bytearray(response)) end), - on_error = errorCallback, + on_response = callback and (function (response) + return callback(Bytearray(response.body)) + end), + on_error = function(response) + errorCallback(response.status, response.body) + end, follow_location = true, }) end @@ -150,8 +158,12 @@ network.post = function(url, body, callback, errorCallback, headers) "Content-Type: application/json" }, headers), body = body, - on_response = callback, - on_error = errorCallback, + on_response = function(response) + callback(response.body) + end, + on_error = function(response) + errorCallback(response.status, response.body) + end, }) end diff --git a/src/logic/scripting/lua/libs/libnetwork.cpp b/src/logic/scripting/lua/libs/libnetwork.cpp index 475158dea..2ccf25546 100644 --- a/src/logic/scripting/lua/libs/libnetwork.cpp +++ b/src/logic/scripting/lua/libs/libnetwork.cpp @@ -28,7 +28,7 @@ struct ResponseEventDto { int status; bool binary; int requestId; - std::vector bytes; + network::HttpResponse response; }; enum NetworkDatagramSide { @@ -122,7 +122,7 @@ static int l_request(lua::State* L, network::Network& network) { response.status, false, currentRequestId, - std::move(response.body)} + std::move(response)} )); }; @@ -493,11 +493,31 @@ static int l_pull_events(lua::State* L) { lua::pushinteger(L, dto.requestId); lua::rawseti(L, 3); + lua::createtable(L, 0, 2); + lua::pushinteger(L, dto.response.status); + lua::setfield(L, "status"); + if (dto.binary) { - lua::create_bytearray(L, dto.bytes.data(), dto.bytes.size()); + lua::create_bytearray( + L, dto.response.body.data(), dto.response.body.size() + ); } else { - lua::pushlstring(L, std::string_view(dto.bytes.data(), dto.bytes.size())); + lua::pushlstring( + L, + std::string_view( + dto.response.body.data(), dto.response.body.size() + ) + ); } + lua::setfield(L, "body"); + + lua::createtable(L, dto.response.headers.size(), 0); + for (int i = 0; i < dto.response.headers.size(); i++) { + lua::pushlstring(L, dto.response.headers[i]); + lua::rawseti(L, i + 1); + } + lua::setfield(L, "headers"); + lua::rawseti(L, 4); break; }