2023-01-23 14:03:55 +00:00
|
|
|
// SPDX-FileCopyrightText: 2023 flowln <flowlnlnln@gmail.com>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
|
2022-11-25 12:23:46 +00:00
|
|
|
#include "NetworkResourceAPI.h"
|
|
|
|
|
|
|
|
#include "Application.h"
|
|
|
|
#include "net/NetJob.h"
|
|
|
|
|
|
|
|
#include "modplatform/ModIndex.h"
|
|
|
|
|
2023-01-03 16:58:27 +00:00
|
|
|
Task::Ptr NetworkResourceAPI::searchProjects(SearchArgs&& args, SearchCallbacks&& callbacks) const
|
2022-11-25 12:23:46 +00:00
|
|
|
{
|
|
|
|
auto search_url_optional = getSearchURL(args);
|
|
|
|
if (!search_url_optional.has_value()) {
|
|
|
|
callbacks.on_fail("Failed to create search URL", -1);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto search_url = search_url_optional.value();
|
|
|
|
|
|
|
|
auto response = new QByteArray();
|
2023-01-24 19:52:09 +00:00
|
|
|
auto netJob = makeShared<NetJob>(QString("%1::Search").arg(debugName()), APPLICATION->network());
|
2022-11-25 12:23:46 +00:00
|
|
|
|
|
|
|
netJob->addNetAction(Net::Download::makeByteArray(QUrl(search_url), response));
|
|
|
|
|
2023-04-14 16:59:31 +01:00
|
|
|
QObject::connect(netJob.get(), &NetJob::succeeded, [=] {
|
2022-11-25 12:23:46 +00:00
|
|
|
QJsonParseError parse_error{};
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
|
|
|
qWarning() << "Error while parsing JSON response from " << debugName() << " at " << parse_error.offset
|
|
|
|
<< " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << *response;
|
|
|
|
|
|
|
|
callbacks.on_fail(parse_error.errorString(), -1);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callbacks.on_succeed(doc);
|
|
|
|
});
|
|
|
|
|
2023-04-14 16:59:31 +01:00
|
|
|
QObject::connect(netJob.get(), &NetJob::failed, [=](QString reason) {
|
2022-11-25 12:23:46 +00:00
|
|
|
int network_error_code = -1;
|
|
|
|
if (auto* failed_action = netJob->getFailedActions().at(0); failed_action && failed_action->m_reply)
|
|
|
|
network_error_code = failed_action->m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
|
|
|
2023-04-14 16:59:31 +01:00
|
|
|
callbacks.on_fail(reason, network_error_code);
|
2022-11-25 12:23:46 +00:00
|
|
|
});
|
2023-04-14 16:59:31 +01:00
|
|
|
QObject::connect(netJob.get(), &NetJob::aborted, [=] { callbacks.on_abort(); });
|
2022-11-25 12:23:46 +00:00
|
|
|
|
|
|
|
return netJob;
|
|
|
|
}
|
|
|
|
|
2023-01-03 16:58:27 +00:00
|
|
|
Task::Ptr NetworkResourceAPI::getProjectInfo(ProjectInfoArgs&& args, ProjectInfoCallbacks&& callbacks) const
|
2022-11-25 12:23:46 +00:00
|
|
|
{
|
|
|
|
auto response = new QByteArray();
|
|
|
|
auto job = getProject(args.pack.addonId.toString(), response);
|
|
|
|
|
|
|
|
QObject::connect(job.get(), &NetJob::succeeded, [response, callbacks, args] {
|
|
|
|
QJsonParseError parse_error{};
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
|
|
|
qWarning() << "Error while parsing JSON response for mod info at " << parse_error.offset
|
|
|
|
<< " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << *response;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callbacks.on_succeed(doc, args.pack);
|
|
|
|
});
|
|
|
|
|
|
|
|
return job;
|
|
|
|
}
|
|
|
|
|
2023-01-03 16:58:27 +00:00
|
|
|
Task::Ptr NetworkResourceAPI::getProjectVersions(VersionSearchArgs&& args, VersionSearchCallbacks&& callbacks) const
|
2022-11-25 12:23:46 +00:00
|
|
|
{
|
|
|
|
auto versions_url_optional = getVersionsURL(args);
|
|
|
|
if (!versions_url_optional.has_value())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
auto versions_url = versions_url_optional.value();
|
|
|
|
|
2023-01-24 19:52:09 +00:00
|
|
|
auto netJob = makeShared<NetJob>(QString("%1::Versions").arg(args.pack.name), APPLICATION->network());
|
2022-11-25 12:23:46 +00:00
|
|
|
auto response = new QByteArray();
|
|
|
|
|
|
|
|
netJob->addNetAction(Net::Download::makeByteArray(versions_url, response));
|
|
|
|
|
2023-01-24 19:52:09 +00:00
|
|
|
QObject::connect(netJob.get(), &NetJob::succeeded, [=] {
|
2022-11-25 12:23:46 +00:00
|
|
|
QJsonParseError parse_error{};
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
|
|
|
qWarning() << "Error while parsing JSON response for getting versions at " << parse_error.offset
|
|
|
|
<< " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << *response;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-18 18:41:46 +00:00
|
|
|
callbacks.on_succeed(doc, args.pack);
|
2022-11-25 12:23:46 +00:00
|
|
|
});
|
|
|
|
|
2023-04-14 16:59:31 +01:00
|
|
|
QObject::connect(netJob.get(), &NetJob::finished, [response] { delete response; });
|
2022-11-25 12:23:46 +00:00
|
|
|
|
|
|
|
return netJob;
|
|
|
|
}
|
|
|
|
|
2023-01-03 16:58:27 +00:00
|
|
|
Task::Ptr NetworkResourceAPI::getProject(QString addonId, QByteArray* response) const
|
2022-11-25 12:23:46 +00:00
|
|
|
{
|
|
|
|
auto project_url_optional = getInfoURL(addonId);
|
|
|
|
if (!project_url_optional.has_value())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
auto project_url = project_url_optional.value();
|
|
|
|
|
2023-01-24 19:52:09 +00:00
|
|
|
auto netJob = makeShared<NetJob>(QString("%1::GetProject").arg(addonId), APPLICATION->network());
|
2022-11-25 12:23:46 +00:00
|
|
|
|
|
|
|
netJob->addNetAction(Net::Download::makeByteArray(QUrl(project_url), response));
|
|
|
|
|
2023-04-14 16:59:31 +01:00
|
|
|
QObject::connect(netJob.get(), &NetJob::finished, [response] { delete response; });
|
2022-11-25 12:23:46 +00:00
|
|
|
|
|
|
|
return netJob;
|
|
|
|
}
|
2023-04-14 16:59:31 +01:00
|
|
|
|
|
|
|
Task::Ptr NetworkResourceAPI::getDependencyVersion(DependencySearchArgs&& args, DependencySearchCallbacks&& callbacks) const
|
|
|
|
{
|
2023-04-21 16:41:40 +01:00
|
|
|
auto versions_url_optional = getDependencyURL(args);
|
2023-04-14 16:59:31 +01:00
|
|
|
if (!versions_url_optional.has_value())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
auto versions_url = versions_url_optional.value();
|
|
|
|
|
2023-04-21 16:41:40 +01:00
|
|
|
auto netJob = makeShared<NetJob>(QString("%1::Dependency").arg(args.dependency.addonId.toString()), APPLICATION->network());
|
2023-04-14 16:59:31 +01:00
|
|
|
auto response = new QByteArray();
|
|
|
|
|
|
|
|
netJob->addNetAction(Net::Download::makeByteArray(versions_url, response));
|
|
|
|
|
|
|
|
QObject::connect(netJob.get(), &NetJob::succeeded, [=] {
|
|
|
|
QJsonParseError parse_error{};
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
|
|
|
qWarning() << "Error while parsing JSON response for getting versions at " << parse_error.offset
|
|
|
|
<< " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << *response;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callbacks.on_succeed(doc, args.dependency);
|
|
|
|
});
|
|
|
|
|
|
|
|
QObject::connect(netJob.get(), &NetJob::finished, [response] { delete response; });
|
|
|
|
|
|
|
|
return netJob;
|
|
|
|
};
|