magpie/API/requests/poll.cpp

47 lines
1.5 KiB
C++
Raw Normal View History

2024-01-20 21:17:21 +00:00
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#include "poll.h"
#include <QUrlQuery>
#include "API/codes.h"
const std::map<QString, QMetaType::Type> Request::Poll::updatesStructure({
{"data", QMetaType::QVariantMap},
});
Request::Poll::Poll (const QUrl& baseUrl, bool clear):
Request(createUrl(baseUrl, "/poll", clear))
{
request.setTransferTimeout(30000);
}
void Request::Poll::onSuccess (const QVariantMap& data) {
if (!validateResponse(data, resultStructure))
return Request::onError("Malformed response", std::nullopt);
Codes::Poll code = Codes::convertPoll(data.value("result").toInt());
if (code == Codes::Poll::success)
if (!validateResponse(data, updatesStructure))
return onError("Malformed response: received a poll that claimed to have some updates, but the \"data\" field is abscent", data);
Request::onSuccess(data);
}
void Request::Poll::onError (const QString& err, const std::optional<QVariantMap>& data) {
if (validateResponse(data, resultStructure))
Request::onError(err + ": " + Codes::description(Codes::convertPoll(data->value("result").toInt())), data);
else
Request::onError(err, data);
}
QUrl Request::Poll::createUrl (QUrl base, const QString& path, bool clear) {
QString startingPath = base.path();
base.setPath(startingPath + path);
if (clear)
base.setQuery(QUrlQuery({{"clearCache", "all"}}));
return base;
}