feat: add some mod api calls

- Get Project: Already existed but required a specific caller type. This
  is more general.
- Get Projects: A single call to multiple of the above

Both providers support these calls.

Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
flow 2022-06-19 14:29:21 -03:00
parent fd6755c93f
commit 52c45c2d32
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
7 changed files with 71 additions and 14 deletions

View File

@ -40,6 +40,7 @@
#include <list>
#include "Version.h"
#include "net/NetJob.h"
namespace ModPlatform {
class ListModel;
@ -74,6 +75,9 @@ class ModAPI {
virtual void searchMods(CallerType* caller, SearchArgs&& args) const = 0;
virtual void getModInfo(CallerType* caller, ModPlatform::IndexedPack& pack) = 0;
virtual auto getProject(QString addonId, QByteArray* response) const -> NetJob::Ptr = 0;
virtual auto getProjects(QStringList addonIds, QByteArray* response) const -> NetJob::Ptr = 0;
struct VersionSearchArgs {
QString addonId;

View File

@ -123,3 +123,26 @@ auto FlameAPI::getLatestVersion(VersionSearchArgs&& args) -> ModPlatform::Indexe
return ver;
}
auto FlameAPI::getProjects(QStringList addonIds, QByteArray* response) const -> NetJob::Ptr
{
auto* netJob = new NetJob(QString("Flame::GetProjects"), APPLICATION->network());
QJsonObject body_obj;
QJsonArray addons_arr;
for (auto& addonId : addonIds) {
addons_arr.append(addonId);
}
body_obj["modIds"] = addons_arr;
QJsonDocument body(body_obj);
auto body_raw = body.toJson();
netJob->addNetAction(Net::Upload::makeByteArray(QString("https://api.curseforge.com/v1/mods"), response, body_raw));
QObject::connect(netJob, &NetJob::finished, [response] { delete response; });
QObject::connect(netJob, &NetJob::failed, [body_raw] { qDebug() << body_raw; });
return netJob;
}

View File

@ -3,8 +3,6 @@
#include "modplatform/ModIndex.h"
#include "modplatform/helpers/NetworkModAPI.h"
#include "net/NetJob.h"
class FlameAPI : public NetworkModAPI {
public:
auto matchFingerprints(const std::list<uint>& fingerprints, QByteArray* response) -> NetJob::Ptr;
@ -12,6 +10,8 @@ class FlameAPI : public NetworkModAPI {
auto getLatestVersion(VersionSearchArgs&& args) -> ModPlatform::IndexedVersion;
auto getProjects(QStringList addonIds, QByteArray* response) const -> NetJob::Ptr override;
private:
inline auto getSortFieldInt(QString sortString) const -> int
{

View File

@ -33,18 +33,14 @@ void NetworkModAPI::searchMods(CallerType* caller, SearchArgs&& args) const
void NetworkModAPI::getModInfo(CallerType* caller, ModPlatform::IndexedPack& pack)
{
auto id_str = pack.addonId.toString();
auto netJob = new NetJob(QString("%1::ModInfo").arg(id_str), APPLICATION->network());
auto searchUrl = getModInfoURL(id_str);
auto response = new QByteArray();
netJob->addNetAction(Net::Download::makeByteArray(QUrl(searchUrl), response));
auto job = getProject(pack.addonId.toString(), response);
QObject::connect(netJob, &NetJob::succeeded, [response, &pack, caller] {
QObject::connect(job.get(), &NetJob::succeeded, caller, [caller, &pack, response] {
QJsonParseError parse_error{};
auto doc = QJsonDocument::fromJson(*response, &parse_error);
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
if (parse_error.error != QJsonParseError::NoError) {
qWarning() << "Error while parsing JSON response for " << pack.name << " at " << parse_error.offset
qWarning() << "Error while parsing JSON response from " << caller->debugName() << " at " << parse_error.offset
<< " reason: " << parse_error.errorString();
qWarning() << *response;
return;
@ -53,7 +49,7 @@ void NetworkModAPI::getModInfo(CallerType* caller, ModPlatform::IndexedPack& pac
caller->infoRequestFinished(doc, pack);
});
netJob->start();
job->start();
}
void NetworkModAPI::getVersions(CallerType* caller, VersionSearchArgs&& args) const
@ -83,3 +79,18 @@ void NetworkModAPI::getVersions(CallerType* caller, VersionSearchArgs&& args) co
netJob->start();
}
auto NetworkModAPI::getProject(QString addonId, QByteArray* response) const -> NetJob::Ptr
{
auto netJob = new NetJob(QString("%1::GetProject").arg(addonId), APPLICATION->network());
auto searchUrl = getModInfoURL(addonId);
netJob->addNetAction(Net::Download::makeByteArray(QUrl(searchUrl), response));
QObject::connect(netJob, &NetJob::finished, [response, netJob] {
netJob->deleteLater();
delete response;
});
return netJob;
}

View File

@ -8,6 +8,8 @@ class NetworkModAPI : public ModAPI {
void getModInfo(CallerType* caller, ModPlatform::IndexedPack& pack) override;
void getVersions(CallerType* caller, VersionSearchArgs&& args) const override;
auto getProject(QString addonId, QByteArray* response) const -> NetJob::Ptr override;
protected:
virtual auto getModSearchURL(SearchArgs& args) const -> QString = 0;
virtual auto getModInfoURL(QString& id) const -> QString = 0;

View File

@ -88,8 +88,19 @@ auto ModrinthAPI::latestVersions(const QStringList& hashes,
QJsonDocument body(body_obj);
auto body_raw = body.toJson();
netJob->addNetAction(Net::Upload::makeByteArray(
QString(BuildConfig.MODRINTH_PROD_URL + "/version_files/update"), response, body_raw));
netJob->addNetAction(Net::Upload::makeByteArray(QString(BuildConfig.MODRINTH_PROD_URL + "/version_files/update"), response, body_raw));
QObject::connect(netJob, &NetJob::finished, [response] { delete response; });
return netJob;
}
auto ModrinthAPI::getProjects(QStringList addonIds, QByteArray* response) const -> NetJob::Ptr
{
auto netJob = new NetJob(QString("Modrinth::GetProjects"), APPLICATION->network());
auto searchUrl = getMultipleModInfoURL(addonIds);
netJob->addNetAction(Net::Download::makeByteArray(QUrl(searchUrl), response));
QObject::connect(netJob, &NetJob::finished, [response] { delete response; });

View File

@ -22,7 +22,6 @@
#include "modplatform/ModAPI.h"
#include "modplatform/ModIndex.h"
#include "modplatform/helpers/NetworkModAPI.h"
#include "net/NetJob.h"
#include <QDebug>
@ -48,6 +47,8 @@ class ModrinthAPI : public NetworkModAPI {
ModLoaderTypes loaders,
QByteArray* response) -> NetJob::Ptr;
auto getProjects(QStringList addonIds, QByteArray* response) const -> NetJob::Ptr override;
public:
inline auto getAuthorURL(const QString& name) const -> QString { return "https://modrinth.com/user/" + name; };
@ -103,6 +104,11 @@ class ModrinthAPI : public NetworkModAPI {
return BuildConfig.MODRINTH_PROD_URL + "/project/" + id;
};
inline auto getMultipleModInfoURL(QStringList ids) const -> QString
{
return BuildConfig.MODRINTH_PROD_URL + QString("/projects?ids=[\"%1\"]").arg(ids.join("\",\""));
};
inline auto getVersionsURL(VersionSearchArgs& args) const -> QString override
{
return QString(BuildConfig.MODRINTH_PROD_URL +