Axivion: Refactor fetching serializable data

Change-Id: I5a5c07238abf7440c4f8686a1a06b96da6125ef8
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Jarek Kobus
2024-01-19 19:46:40 +01:00
parent 948352d937
commit 0a7dc1ac1e

View File

@@ -190,52 +190,36 @@ static QUrl urlForProject(const QString &projectName)
static constexpr int httpStatusCodeOk = 200; static constexpr int httpStatusCodeOk = 200;
static const QLatin1String jsonContentType{ "application/json" }; static const QLatin1String jsonContentType{ "application/json" };
static void deserialize(QPromise<Dto::ProjectInfoDto> &promise, const QByteArray &input) template<typename SerializableType>
static Group fetchDataRecipe(const QUrl &url,
const std::function<void(const SerializableType &result)> &handler)
{ {
promise.addResult(Dto::ProjectInfoDto::deserialize(input));
}
void AxivionPluginPrivate::fetchProjectInfo(const QString &projectName)
{
if (m_taskTreeRunner.isRunning()) { // TODO: cache in queue and run when task tree finished
QTimer::singleShot(3000, this, [this, projectName] { fetchProjectInfo(projectName); });
return;
}
clearAllMarks();
if (projectName.isEmpty()) {
m_currentProjectInfo = {};
m_axivionOutputPane.updateDashboard();
return;
}
const QUrl url = urlForProject(projectName);
struct StorageData struct StorageData
{ {
QByteArray credential; QByteArray credentials;
QByteArray projectInfoData; QByteArray serializableData;
}; };
const Storage<StorageData> storage; const Storage<StorageData> storage;
const auto onCredentialSetup = [storage] { const auto onCredentialSetup = [storage] {
storage->credential = QByteArrayLiteral("AxToken ") storage->credentials = QByteArrayLiteral("AxToken ")
+ settings().server.token.toUtf8(); + settings().server.token.toUtf8();
}; };
const auto onQuerySetup = [this, storage, url](NetworkQuery &query) { const auto onQuerySetup = [storage, url](NetworkQuery &query) {
QNetworkRequest request(url); QNetworkRequest request(url);
request.setRawHeader(QByteArrayLiteral("Accept"), request.setRawHeader(QByteArrayLiteral("Accept"),
QByteArray(jsonContentType.data(), jsonContentType.size())); QByteArray(jsonContentType.data(), jsonContentType.size()));
request.setRawHeader(QByteArrayLiteral("Authorization"), request.setRawHeader(QByteArrayLiteral("Authorization"),
storage->credential); storage->credentials);
const QByteArray ua = QByteArrayLiteral("Axivion") const QByteArray ua = QByteArrayLiteral("Axivion")
+ QCoreApplication::applicationName().toUtf8() + QCoreApplication::applicationName().toUtf8()
+ QByteArrayLiteral("Plugin/") + QByteArrayLiteral("Plugin/")
+ QCoreApplication::applicationVersion().toUtf8(); + QCoreApplication::applicationVersion().toUtf8();
request.setRawHeader(QByteArrayLiteral("X-Axivion-User-Agent"), ua); request.setRawHeader(QByteArrayLiteral("X-Axivion-User-Agent"), ua);
query.setRequest(request); query.setRequest(request);
query.setNetworkAccessManager(&m_networkAccessManager); query.setNetworkAccessManager(&dd->m_networkAccessManager);
}; };
const auto onQueryDone = [storage, url](const NetworkQuery &query, DoneWith doneWith) { const auto onQueryDone = [storage, url](const NetworkQuery &query, DoneWith doneWith) {
@@ -250,7 +234,7 @@ void AxivionPluginPrivate::fetchProjectInfo(const QString &projectName)
.toLower(); .toLower();
if (doneWith == DoneWith::Success && statusCode == httpStatusCodeOk if (doneWith == DoneWith::Success && statusCode == httpStatusCodeOk
&& contentType == jsonContentType) { && contentType == jsonContentType) {
storage->projectInfoData = reply->readAll(); storage->serializableData = reply->readAll();
return DoneResult::Success; return DoneResult::Success;
} }
@@ -277,15 +261,47 @@ void AxivionPluginPrivate::fetchProjectInfo(const QString &projectName)
return DoneResult::Error; return DoneResult::Error;
}; };
const auto onDeserializeSetup = [storage](Async<Dto::ProjectInfoDto> &task) { const auto onDeserializeSetup = [storage](Async<SerializableType> &task) {
const auto deserialize = [](QPromise<SerializableType> &promise, const QByteArray &input) {
promise.addResult(SerializableType::deserialize(input));
};
task.setFutureSynchronizer(ExtensionSystem::PluginManager::futureSynchronizer()); task.setFutureSynchronizer(ExtensionSystem::PluginManager::futureSynchronizer());
task.setConcurrentCallData(deserialize, storage->projectInfoData); task.setConcurrentCallData(deserialize, storage->serializableData);
}; };
const auto onDeserializeDone = [this, url](const Async<Dto::ProjectInfoDto> &task, const auto onDeserializeDone = [handler](const Async<SerializableType> &task,
DoneWith doneWith) { DoneWith doneWith) {
if (doneWith == DoneWith::Success) { if (doneWith == DoneWith::Success)
m_currentProjectInfo = task.future().result(); handler(task.future().result());
};
const Group recipe {
storage,
Sync(onCredentialSetup),
NetworkQueryTask(onQuerySetup, onQueryDone),
AsyncTask<SerializableType>(onDeserializeSetup, onDeserializeDone)
};
return recipe;
}
void AxivionPluginPrivate::fetchProjectInfo(const QString &projectName)
{
if (m_taskTreeRunner.isRunning()) { // TODO: cache in queue and run when task tree finished
QTimer::singleShot(3000, this, [this, projectName] { fetchProjectInfo(projectName); });
return;
}
clearAllMarks();
if (projectName.isEmpty()) {
m_currentProjectInfo = {};
m_axivionOutputPane.updateDashboard();
return;
}
const QUrl url = urlForProject(projectName);
const auto handler = [this](const Dto::ProjectInfoDto &data) {
m_currentProjectInfo = data;
m_axivionOutputPane.updateDashboard(); m_axivionOutputPane.updateDashboard();
// handle already opened documents // handle already opened documents
if (auto buildSystem = ProjectExplorer::ProjectManager::startupBuildSystem(); if (auto buildSystem = ProjectExplorer::ProjectManager::startupBuildSystem();
@@ -296,17 +312,9 @@ void AxivionPluginPrivate::fetchProjectInfo(const QString &projectName)
&ProjectExplorer::ProjectManager::projectFinishedParsing, &ProjectExplorer::ProjectManager::projectFinishedParsing,
this, &AxivionPluginPrivate::handleOpenedDocs); this, &AxivionPluginPrivate::handleOpenedDocs);
} }
}
}; };
const Group recipe { m_taskTreeRunner.start(fetchDataRecipe<Dto::ProjectInfoDto>(url, handler));
storage,
Sync(onCredentialSetup),
NetworkQueryTask(onQuerySetup, onQueryDone),
AsyncTask<Dto::ProjectInfoDto>(onDeserializeSetup, onDeserializeDone)
};
m_taskTreeRunner.start(recipe);
} }
void AxivionPluginPrivate::fetchRuleInfo(const QString &id) void AxivionPluginPrivate::fetchRuleInfo(const QString &id)