add network.http_get, network.http_get_binary

This commit is contained in:
MihailRis
2024-11-22 07:30:38 +03:00
parent 186078a8d5
commit b23318a06c
6 changed files with 62 additions and 2 deletions
@@ -0,0 +1,43 @@
#include "api_lua.hpp"
#include "engine.hpp"
#include "network/Network.hpp"
using namespace scripting;
static int l_http_get(lua::State* L) {
std::string url(lua::require_lstring(L, 1));
lua::pushvalue(L, 2);
auto onResponse = lua::create_lambda(L);
engine->getNetwork().httpGet(url, [onResponse](std::vector<char> bytes) {
engine->postRunnable([=]() {
onResponse({std::string(bytes.data(), bytes.size())});
});
});
return 0;
}
static int l_http_get_binary(lua::State* L) {
std::string url(lua::require_lstring(L, 1));
lua::pushvalue(L, 2);
auto onResponse = lua::create_lambda(L);
engine->getNetwork().httpGet(url, [onResponse](std::vector<char> bytes) {
auto buffer = std::make_shared<util::Buffer<ubyte>>(
reinterpret_cast<const ubyte*>(bytes.data()), bytes.size()
);
engine->postRunnable([=]() {
onResponse({buffer});
});
});
return 0;
}
const luaL_Reg networklib[] = {
{"http_get", lua::wrap<l_http_get>},
{"http_get_binary", lua::wrap<l_http_get_binary>},
{NULL, NULL}
};