File indexing completed on 2024-12-22 04:45:18

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