Axivion: Parameterize recipe for re-use

The recipe which fetches the issue properties works
in general equally to the one needed to fetch source
code for a given analysis version.
This patch provides this function but does not make
any use of it yet.

Change-Id: I5bd63eb470e9cb7038aaae5e4c79fa2d570d45db
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
Christian Stenger
2024-07-25 15:02:10 +02:00
parent dd1125e7af
commit 1b23330ec3

View File

@@ -376,6 +376,7 @@ static QUrl constructUrl(const QString &projectName, const QString &subPath, con
static constexpr int httpStatusCodeOk = 200; static constexpr int httpStatusCodeOk = 200;
constexpr char s_htmlContentType[] = "text/html"; constexpr char s_htmlContentType[] = "text/html";
constexpr char s_plaintextContentType[] = "text/plain";
constexpr char s_jsonContentType[] = "application/json"; constexpr char s_jsonContentType[] = "application/json";
static bool isServerAccessEstablished() static bool isServerAccessEstablished()
@@ -384,15 +385,17 @@ static bool isServerAccessEstablished()
|| (dd->m_serverAccess == ServerAccess::WithAuthorization && dd->m_apiToken); || (dd->m_serverAccess == ServerAccess::WithAuthorization && dd->m_apiToken);
} }
static Group fetchHtmlRecipe(const QUrl &url, const std::function<void(const QByteArray &)> &handler) static Group fetchSimpleRecipe(const QUrl &url,
const QByteArray &expectedContentType,
const std::function<void(const QByteArray &)> &handler)
{ {
// TODO: Refactor so that it's a common code with fetchDataRecipe(). // TODO: Refactor so that it's a common code with fetchDataRecipe().
const auto onQuerySetup = [url](NetworkQuery &query) { const auto onQuerySetup = [url, expectedContentType](NetworkQuery &query) {
if (!isServerAccessEstablished()) if (!isServerAccessEstablished())
return SetupResult::StopWithError; // TODO: start authorizationRecipe()? return SetupResult::StopWithError; // TODO: start authorizationRecipe()?
QNetworkRequest request(url); QNetworkRequest request(url);
request.setRawHeader("Accept", s_htmlContentType); request.setRawHeader("Accept", expectedContentType);
if (dd->m_serverAccess == ServerAccess::WithAuthorization && dd->m_apiToken) if (dd->m_serverAccess == ServerAccess::WithAuthorization && dd->m_apiToken)
request.setRawHeader("Authorization", "AxToken " + *dd->m_apiToken); request.setRawHeader("Authorization", "AxToken " + *dd->m_apiToken);
const QByteArray ua = "Axivion" + QCoreApplication::applicationName().toUtf8() + const QByteArray ua = "Axivion" + QCoreApplication::applicationName().toUtf8() +
@@ -402,7 +405,7 @@ static Group fetchHtmlRecipe(const QUrl &url, const std::function<void(const QBy
query.setNetworkAccessManager(&dd->m_networkAccessManager); query.setNetworkAccessManager(&dd->m_networkAccessManager);
return SetupResult::Continue; return SetupResult::Continue;
}; };
const auto onQueryDone = [url, handler](const NetworkQuery &query, DoneWith doneWith) { const auto onQueryDone = [url, expectedContentType, handler](const NetworkQuery &query, DoneWith doneWith) {
QNetworkReply *reply = query.reply(); QNetworkReply *reply = query.reply();
const int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); const int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader) const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader)
@@ -412,7 +415,7 @@ static Group fetchHtmlRecipe(const QUrl &url, const std::function<void(const QBy
.trimmed() .trimmed()
.toLower(); .toLower();
if (doneWith == DoneWith::Success && statusCode == httpStatusCodeOk if (doneWith == DoneWith::Success && statusCode == httpStatusCodeOk
&& contentType == s_htmlContentType) { && contentType == QString::fromUtf8(expectedContentType)) {
handler(reply->readAll()); handler(reply->readAll());
return DoneResult::Success; return DoneResult::Success;
} }
@@ -421,6 +424,16 @@ static Group fetchHtmlRecipe(const QUrl &url, const std::function<void(const QBy
return {NetworkQueryTask(onQuerySetup, onQueryDone)}; return {NetworkQueryTask(onQuerySetup, onQueryDone)};
} }
static Group fetchHtmlRecipe(const QUrl &url, const std::function<void(const QByteArray &)> &handler)
{
return fetchSimpleRecipe(url, s_htmlContentType, handler);
}
static Group fetchPlainTextRecipe(const QUrl &url, const std::function<void(const QByteArray &)> &handler)
{
return fetchSimpleRecipe(url, s_plaintextContentType, handler);
}
template <typename DtoType, template <typename> typename DtoStorageType> template <typename DtoType, template <typename> typename DtoStorageType>
static Group dtoRecipe(const Storage<DtoStorageType<DtoType>> &dtoStorage) static Group dtoRecipe(const Storage<DtoStorageType<DtoType>> &dtoStorage)
{ {
@@ -801,6 +814,18 @@ Group lineMarkerRecipe(const FilePath &filePath, const LineMarkerHandler &handle
return fetchDataRecipe<Dto::FileViewDto>(url, handler); return fetchDataRecipe<Dto::FileViewDto>(url, handler);
} }
Group fileSourceRecipe(const FilePath &filePath, const std::function<void(const QByteArray &)> &handler)
{
QTC_ASSERT(dd->m_currentProjectInfo, return {}); // TODO: Call handler with unexpected
QTC_ASSERT(!filePath.isEmpty(), return {}); // TODO: Call handler with unexpected
QTC_ASSERT(dd->m_analysisVersion, return {}); // TODO: Call handler with unexpected
const QString fileName = QString::fromUtf8(QUrl::toPercentEncoding(filePath.path()));
const QUrlQuery query({{"filename", fileName}, {"version", *dd->m_analysisVersion}});
const QUrl url = constructUrl(dd->m_currentProjectInfo.value().name, "sourcecode", query);
return fetchPlainTextRecipe(url, handler);
}
Group issueHtmlRecipe(const QString &issueId, const HtmlHandler &handler) Group issueHtmlRecipe(const QString &issueId, const HtmlHandler &handler)
{ {
QTC_ASSERT(dd->m_currentProjectInfo, return {}); // TODO: Call handler with unexpected? QTC_ASSERT(dd->m_currentProjectInfo, return {}); // TODO: Call handler with unexpected?