File indexing completed on 2024-12-22 04:45:13
0001 /* 0002 SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "getbannersjob.h" 0008 #include "restapimethod.h" 0009 #include "rocketchatqtrestapi_debug.h" 0010 #include <QJsonDocument> 0011 #include <QJsonObject> 0012 #include <QNetworkReply> 0013 #include <QUrlQuery> 0014 0015 using namespace RocketChatRestApi; 0016 GetBannersJob::GetBannersJob(QObject *parent) 0017 : RestApiAbstractJob(parent) 0018 { 0019 } 0020 0021 GetBannersJob::~GetBannersJob() = default; 0022 0023 bool GetBannersJob::requireHttpAuthentication() const 0024 { 0025 return true; 0026 } 0027 0028 bool GetBannersJob::canStart() const 0029 { 0030 if (!RestApiAbstractJob::canStart()) { 0031 return false; 0032 } 0033 return true; 0034 } 0035 0036 bool GetBannersJob::start() 0037 { 0038 if (!canStart()) { 0039 qCWarning(ROCKETCHATQTRESTAPI_LOG) << "Impossible to start GetBannersJob job"; 0040 deleteLater(); 0041 return false; 0042 } 0043 submitGetRequest(); 0044 addStartRestApiInfo(QByteArrayLiteral("GetBannersJob: Ask about banners")); 0045 return true; 0046 } 0047 0048 void GetBannersJob::onGetRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson) 0049 { 0050 const QJsonObject replyObject = replyJson.object(); 0051 if (replyObject[QLatin1String("success")].toBool()) { 0052 addLoggerInfo(QByteArrayLiteral("GetBannersJob: success: ") + replyJson.toJson(QJsonDocument::Indented)); 0053 Q_EMIT getBannersDone(replyObject); 0054 } else { 0055 emitFailedMessage(replyErrorString, replyObject); 0056 addLoggerWarning(QByteArrayLiteral("GetBannersJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented)); 0057 } 0058 } 0059 0060 bool GetBannersJob::hasQueryParameterSupport() const 0061 { 0062 return false; 0063 } 0064 0065 QNetworkRequest GetBannersJob::request() const 0066 { 0067 QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::Banners); 0068 0069 QUrlQuery queryUrl; 0070 queryUrl.addQueryItem(QStringLiteral("platform"), QStringLiteral("web")); 0071 addQueryParameter(queryUrl); 0072 url.setQuery(queryUrl); 0073 0074 QNetworkRequest request(url); 0075 addAuthRawHeader(request); 0076 addRequestAttribute(request); 0077 return request; 0078 } 0079 0080 #include "moc_getbannersjob.cpp"