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 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
{
QByteArray credential;
QByteArray projectInfoData;
QByteArray credentials;
QByteArray serializableData;
};
const Storage<StorageData> storage;
const auto onCredentialSetup = [storage] {
storage->credential = QByteArrayLiteral("AxToken ")
storage->credentials = QByteArrayLiteral("AxToken ")
+ settings().server.token.toUtf8();
};
const auto onQuerySetup = [this, storage, url](NetworkQuery &query) {
const auto onQuerySetup = [storage, url](NetworkQuery &query) {
QNetworkRequest request(url);
request.setRawHeader(QByteArrayLiteral("Accept"),
QByteArray(jsonContentType.data(), jsonContentType.size()));
request.setRawHeader(QByteArrayLiteral("Authorization"),
storage->credential);
storage->credentials);
const QByteArray ua = QByteArrayLiteral("Axivion")
+ QCoreApplication::applicationName().toUtf8()
+ QByteArrayLiteral("Plugin/")
+ QCoreApplication::applicationVersion().toUtf8();
request.setRawHeader(QByteArrayLiteral("X-Axivion-User-Agent"), ua);
query.setRequest(request);
query.setNetworkAccessManager(&m_networkAccessManager);
query.setNetworkAccessManager(&dd->m_networkAccessManager);
};
const auto onQueryDone = [storage, url](const NetworkQuery &query, DoneWith doneWith) {
@@ -250,7 +234,7 @@ void AxivionPluginPrivate::fetchProjectInfo(const QString &projectName)
.toLower();
if (doneWith == DoneWith::Success && statusCode == httpStatusCodeOk
&& contentType == jsonContentType) {
storage->projectInfoData = reply->readAll();
storage->serializableData = reply->readAll();
return DoneResult::Success;
}
@@ -277,15 +261,47 @@ void AxivionPluginPrivate::fetchProjectInfo(const QString &projectName)
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.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) {
if (doneWith == DoneWith::Success) {
m_currentProjectInfo = task.future().result();
if (doneWith == DoneWith::Success)
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();
// handle already opened documents
if (auto buildSystem = ProjectExplorer::ProjectManager::startupBuildSystem();
@@ -296,17 +312,9 @@ void AxivionPluginPrivate::fetchProjectInfo(const QString &projectName)
&ProjectExplorer::ProjectManager::projectFinishedParsing,
this, &AxivionPluginPrivate::handleOpenedDocs);
}
}
};
const Group recipe {
storage,
Sync(onCredentialSetup),
NetworkQueryTask(onQuerySetup, onQueryDone),
AsyncTask<Dto::ProjectInfoDto>(onDeserializeSetup, onDeserializeDone)
};
m_taskTreeRunner.start(recipe);
m_taskTreeRunner.start(fetchDataRecipe<Dto::ProjectInfoDto>(url, handler));
}
void AxivionPluginPrivate::fetchRuleInfo(const QString &id)