mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +00:00
feat: http response as table
This commit is contained in:
parent
5f37118f74
commit
ef3a5cf278
2 changed files with 42 additions and 10 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ struct ResponseEventDto {
|
|||
int status;
|
||||
bool binary;
|
||||
int requestId;
|
||||
std::vector<char> 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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue