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:
parent
fd6755c93f
commit
52c45c2d32
@ -40,6 +40,7 @@
|
|||||||
#include <list>
|
#include <list>
|
||||||
|
|
||||||
#include "Version.h"
|
#include "Version.h"
|
||||||
|
#include "net/NetJob.h"
|
||||||
|
|
||||||
namespace ModPlatform {
|
namespace ModPlatform {
|
||||||
class ListModel;
|
class ListModel;
|
||||||
@ -74,6 +75,9 @@ class ModAPI {
|
|||||||
virtual void searchMods(CallerType* caller, SearchArgs&& args) const = 0;
|
virtual void searchMods(CallerType* caller, SearchArgs&& args) const = 0;
|
||||||
virtual void getModInfo(CallerType* caller, ModPlatform::IndexedPack& pack) = 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 {
|
struct VersionSearchArgs {
|
||||||
QString addonId;
|
QString addonId;
|
||||||
|
@ -123,3 +123,26 @@ auto FlameAPI::getLatestVersion(VersionSearchArgs&& args) -> ModPlatform::Indexe
|
|||||||
|
|
||||||
return ver;
|
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;
|
||||||
|
}
|
||||||
|
@ -3,8 +3,6 @@
|
|||||||
#include "modplatform/ModIndex.h"
|
#include "modplatform/ModIndex.h"
|
||||||
#include "modplatform/helpers/NetworkModAPI.h"
|
#include "modplatform/helpers/NetworkModAPI.h"
|
||||||
|
|
||||||
#include "net/NetJob.h"
|
|
||||||
|
|
||||||
class FlameAPI : public NetworkModAPI {
|
class FlameAPI : public NetworkModAPI {
|
||||||
public:
|
public:
|
||||||
auto matchFingerprints(const std::list<uint>& fingerprints, QByteArray* response) -> NetJob::Ptr;
|
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 getLatestVersion(VersionSearchArgs&& args) -> ModPlatform::IndexedVersion;
|
||||||
|
|
||||||
|
auto getProjects(QStringList addonIds, QByteArray* response) const -> NetJob::Ptr override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
inline auto getSortFieldInt(QString sortString) const -> int
|
inline auto getSortFieldInt(QString sortString) const -> int
|
||||||
{
|
{
|
||||||
|
@ -33,18 +33,14 @@ void NetworkModAPI::searchMods(CallerType* caller, SearchArgs&& args) const
|
|||||||
|
|
||||||
void NetworkModAPI::getModInfo(CallerType* caller, ModPlatform::IndexedPack& pack)
|
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();
|
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{};
|
QJsonParseError parse_error{};
|
||||||
auto doc = QJsonDocument::fromJson(*response, &parse_error);
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
||||||
if (parse_error.error != QJsonParseError::NoError) {
|
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();
|
<< " reason: " << parse_error.errorString();
|
||||||
qWarning() << *response;
|
qWarning() << *response;
|
||||||
return;
|
return;
|
||||||
@ -53,7 +49,7 @@ void NetworkModAPI::getModInfo(CallerType* caller, ModPlatform::IndexedPack& pac
|
|||||||
caller->infoRequestFinished(doc, pack);
|
caller->infoRequestFinished(doc, pack);
|
||||||
});
|
});
|
||||||
|
|
||||||
netJob->start();
|
job->start();
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkModAPI::getVersions(CallerType* caller, VersionSearchArgs&& args) const
|
void NetworkModAPI::getVersions(CallerType* caller, VersionSearchArgs&& args) const
|
||||||
@ -83,3 +79,18 @@ void NetworkModAPI::getVersions(CallerType* caller, VersionSearchArgs&& args) co
|
|||||||
|
|
||||||
netJob->start();
|
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;
|
||||||
|
}
|
||||||
|
@ -8,6 +8,8 @@ class NetworkModAPI : public ModAPI {
|
|||||||
void getModInfo(CallerType* caller, ModPlatform::IndexedPack& pack) override;
|
void getModInfo(CallerType* caller, ModPlatform::IndexedPack& pack) override;
|
||||||
void getVersions(CallerType* caller, VersionSearchArgs&& args) const override;
|
void getVersions(CallerType* caller, VersionSearchArgs&& args) const override;
|
||||||
|
|
||||||
|
auto getProject(QString addonId, QByteArray* response) const -> NetJob::Ptr override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual auto getModSearchURL(SearchArgs& args) const -> QString = 0;
|
virtual auto getModSearchURL(SearchArgs& args) const -> QString = 0;
|
||||||
virtual auto getModInfoURL(QString& id) const -> QString = 0;
|
virtual auto getModInfoURL(QString& id) const -> QString = 0;
|
||||||
|
@ -88,8 +88,19 @@ auto ModrinthAPI::latestVersions(const QStringList& hashes,
|
|||||||
QJsonDocument body(body_obj);
|
QJsonDocument body(body_obj);
|
||||||
auto body_raw = body.toJson();
|
auto body_raw = body.toJson();
|
||||||
|
|
||||||
netJob->addNetAction(Net::Upload::makeByteArray(
|
netJob->addNetAction(Net::Upload::makeByteArray(QString(BuildConfig.MODRINTH_PROD_URL + "/version_files/update"), response, body_raw));
|
||||||
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; });
|
QObject::connect(netJob, &NetJob::finished, [response] { delete response; });
|
||||||
|
|
||||||
|
@ -22,7 +22,6 @@
|
|||||||
#include "modplatform/ModAPI.h"
|
#include "modplatform/ModAPI.h"
|
||||||
#include "modplatform/ModIndex.h"
|
#include "modplatform/ModIndex.h"
|
||||||
#include "modplatform/helpers/NetworkModAPI.h"
|
#include "modplatform/helpers/NetworkModAPI.h"
|
||||||
#include "net/NetJob.h"
|
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
@ -48,6 +47,8 @@ class ModrinthAPI : public NetworkModAPI {
|
|||||||
ModLoaderTypes loaders,
|
ModLoaderTypes loaders,
|
||||||
QByteArray* response) -> NetJob::Ptr;
|
QByteArray* response) -> NetJob::Ptr;
|
||||||
|
|
||||||
|
auto getProjects(QStringList addonIds, QByteArray* response) const -> NetJob::Ptr override;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
inline auto getAuthorURL(const QString& name) const -> QString { return "https://modrinth.com/user/" + name; };
|
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;
|
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
|
inline auto getVersionsURL(VersionSearchArgs& args) const -> QString override
|
||||||
{
|
{
|
||||||
return QString(BuildConfig.MODRINTH_PROD_URL +
|
return QString(BuildConfig.MODRINTH_PROD_URL +
|
||||||
|
Loading…
Reference in New Issue
Block a user