magpie/API/requests/register.cpp

30 lines
1.1 KiB
C++

//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#include "register.h"
#include <QUrlQuery>
#include "API/codes.h"
Request::Register::Register (const QString& login, const QString& password, const QUrl& baseUrl):
Post(createUrl(baseUrl, "/register"), {{"login", login}, {"password", password}}) {}
void Request::Register::onSuccess (const QVariantMap& data) {
if (!validateResponse(data, resultStructure))
return Request::onError("Malformed response", std::nullopt);
Codes::Register code = Codes::convertRegister(data.value("result").toInt());
if (code != Codes::Register::success)
return Request::onError("Failed to register: " + Codes::description(code), data);
Request::onSuccess(data);
}
void Request::Register::onError (const QString& err, const std::optional<QVariantMap>& data) {
if (validateResponse(data, resultStructure))
Request::onError(err + ": " + Codes::description(Codes::convertRegister(data->value("result").toInt())), data);
else
Request::onError(err, data);
}