magpie/API/requests/login.cpp

35 lines
1.3 KiB
C++

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