Lua: Change fetch module access to async

Change-Id: I32bd760e347e2bd4465bd028e31fb3c4ff220aa5
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Marcus Tillmanns
2024-05-02 11:41:54 +02:00
parent d76082da59
commit 6ecd0f4023

View File

@@ -36,7 +36,11 @@ static QString opToString(QNetworkAccessManager::Operation op)
void addFetchModule()
{
LuaEngine::registerProvider("__fetch", [](sol::state_view lua) -> sol::object {
LuaEngine::registerProvider(
"Fetch", [](sol::state_view lua) -> sol::object {
sol::table async = lua.script("return require('async')", "_fetch_").get<sol::table>();
sol::function wrap = async["wrap"];
sol::table fetch = lua.create_table();
auto networkReplyType = lua.new_usertype<QNetworkReply>(
@@ -53,20 +57,22 @@ void addFetchModule()
.arg(r->error());
});
fetch["fetch_cb"] = [](const sol::table &options, const sol::function &callback, const sol::this_state &thisState) {
fetch["fetch_cb"] = [](const sol::table &options,
const sol::function &callback,
const sol::this_state &thisState) {
auto url = options.get<QString>("url");
auto method = (options.get_or<QString>("method", "GET")).toLower();
auto headers = options.get_or<sol::table>("headers", {});
auto data = options.get_or<QString>("body", {});
bool convertToTable = options.get<std::optional<bool>>("convertToTable").value_or(false);
bool convertToTable
= options.get<std::optional<bool>>("convertToTable").value_or(false);
QNetworkRequest request((QUrl(url)));
if (headers && !headers.empty()) {
for (const auto &[k, v] : headers) {
for (const auto &[k, v] : headers)
request.setRawHeader(k.as<QString>().toUtf8(), v.as<QString>().toUtf8());
}
}
QNetworkReply *reply = nullptr;
if (method == "get")
@@ -77,11 +83,16 @@ void addFetchModule()
throw std::runtime_error("Unknown method: " + method.toStdString());
if (convertToTable) {
QObject::connect(reply, &QNetworkReply::finished, reply, [reply, thisState, callback]() {
QObject::connect(
reply,
&QNetworkReply::finished,
&LuaEngine::instance(),
[reply, thisState, callback]() {
reply->deleteLater();
if (reply->error() != QNetworkReply::NoError) {
callback(QString("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
callback(
QString("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
return;
}
@@ -103,7 +114,8 @@ void addFetchModule()
});
} else {
QObject::connect(reply, &QNetworkReply::finished, reply, [reply, callback]() {
QObject::connect(
reply, &QNetworkReply::finished, &LuaEngine::instance(), [reply, callback]() {
// We don't want the network reply to be deleted by the manager, but
// by the Lua GC
reply->setParent(nullptr);
@@ -112,24 +124,10 @@ void addFetchModule()
}
};
fetch["fetch"] = wrap(fetch["fetch_cb"]);
return fetch;
});
LuaEngine::registerProvider("Fetch", [](sol::state_view lua) -> sol::object {
return lua
.script(
R"(
local f = require("__fetch")
local a = require("async")
return {
fetch_cb = f.fetch_cb,
fetch = a.wrap(f.fetch_cb)
}
)",
"_fetch_")
.get<sol::table>();
});
}
} // namespace Lua::Internal