magpie/API/api.cpp

177 lines
6.9 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
2023-11-24 23:48:01 +00:00
#include "api.h"
#include <QDebug>
#include <QJsonDocument>
#include <QJsonObject>
2023-12-16 01:44:25 +00:00
#include <QUrlQuery>
2023-11-24 23:48:01 +00:00
2024-01-20 21:17:21 +00:00
#include "requests/test.h"
#include "requests/register.h"
#include "requests/login.h"
#include "requests/poll.h"
#include "requests/assets.h"
#include "requests/currencies.h"
2024-01-20 21:17:21 +00:00
#include "requests/addasset.h"
2024-01-21 19:22:56 +00:00
#include "requests/deleteasset.h"
2024-01-20 21:17:21 +00:00
API::API (Models::Magpie& magpie, QObject* parent):
2023-11-24 23:48:01 +00:00
QObject(parent),
2024-01-20 21:17:21 +00:00
idCounter(0),
magpie(magpie),
2023-12-16 01:44:25 +00:00
network(),
2024-01-20 21:17:21 +00:00
requests()
{}
2024-01-20 21:17:21 +00:00
void API::cancelRequest (RequestId id) {
requests.erase(id);
}
2024-01-20 21:17:21 +00:00
void API::onRequestDone (RequestId id) {
requests.erase(id);
}
2024-01-20 21:17:21 +00:00
API::RequestId API::test (const QString& path, const QJSValue& finished) {
2023-11-24 23:48:01 +00:00
qDebug() << "Testing" << path;
2024-01-20 21:17:21 +00:00
if (magpie.getState() == Models::Magpie::Offline)
callCallback(finished, "Need to be online to test"), 0;
2023-12-16 01:44:25 +00:00
2024-01-20 21:17:21 +00:00
std::unique_ptr<Request::Test> test = std::make_unique<Request::Test>(path);
connect(
test.get(),
&Request::Test::success,
this,
[this, &path, &finished] (const QVariantMap& data) {
callCallback(finished, QString(), {QJSValue(true)});
magpie.forceAddress(path);
}
2023-11-24 23:48:01 +00:00
);
2024-01-20 21:17:21 +00:00
connect(test.get(), &Request::Test::error, std::bind(&API::callCallback, this, finished, std::placeholders::_1, QJSValueList(false)));
return registerAndSend(std::move(test));
2023-11-24 23:48:01 +00:00
}
2024-01-20 21:17:21 +00:00
API::RequestId API::sendRegister (const QString& login, const QString& password, const QJSValue& finished) {
2023-12-16 01:44:25 +00:00
qDebug() << "Registering...";
2024-01-20 21:17:21 +00:00
if (magpie.getState() != Models::Magpie::NotAuthenticated)
return callCallback(finished, "Can not register in current state"), 0;
2023-12-16 01:44:25 +00:00
2024-01-20 21:17:21 +00:00
std::unique_ptr<Request::Register> reg = std::make_unique<Request::Register>(login, password, magpie.getAddress());
connect(reg.get(), &Request::Register::success, std::bind(&API::callCallback, this, finished, QString(), QJSValueList{QJSValue(true)}));
connect(reg.get(), &Request::Register::error, std::bind(&API::callCallback, this, finished, std::placeholders::_1, QJSValueList{QJSValue(false)}));
return registerAndSend(std::move(reg));
}
2024-01-20 21:17:21 +00:00
API::RequestId API::sendLogin (const QString& login, const QString& password, const QJSValue& finished) {
qDebug() << "Logging in...";
2024-01-20 21:17:21 +00:00
if (magpie.getState() != Models::Magpie::NotAuthenticated)
return callCallback(finished, "Can not register in current state"), 0;
2023-12-25 20:07:51 +00:00
2024-01-20 21:17:21 +00:00
std::unique_ptr<Request::Login> log = std::make_unique<Request::Login>(login, password, magpie.getAddress());
connect(
2024-01-20 21:17:21 +00:00
log.get(),
&Request::Login::success,
this,
[this, &finished] (const QVariantMap& data) {
callCallback(finished, QString(), {QJSValue(true)});
magpie.setTokens(data.value("accessToken").toString(), data.value("renewToken").toString());
}
2024-01-20 21:17:21 +00:00
);
connect(
log.get(),
&Request::Login::error,
this,
[this, &finished] (const QString& error) {
callCallback(finished, error, {QJSValue(false)});
magpie.setState(Models::Magpie::NotAuthenticated);
}
2024-01-20 21:17:21 +00:00
);
2024-01-20 21:17:21 +00:00
magpie.setState(Models::Magpie::Authenticating);
return registerAndSend(std::move(log));
}
2024-01-21 19:22:56 +00:00
API::RequestId API::requestAssets (const SuccessListHandler& success, const ErrorHandler& error) {
qDebug() << "Requesting assets...";
auto list = std::make_unique<Request::Assets>(magpie.getAddress());
2024-01-21 19:22:56 +00:00
list->setAuthorizationToken(magpie.getAccessToken());
connect(list.get(), &Request::Assets::success, success);
connect(list.get(), &Request::Assets::error, error);
return registerAndSend(std::move(list));
}
API::RequestId API::requestCurrencies (const SuccessListHandler& success, const ErrorHandler& error) {
qDebug() << "Requesting currencies...";
auto list = std::make_unique<Request::Currencies>(magpie.getAddress());
list->setAuthorizationToken(magpie.getAccessToken());
connect(list.get(), &Request::Currencies::success, success);
connect(list.get(), &Request::Currencies::error, error);
2024-01-21 19:22:56 +00:00
return registerAndSend(std::move(list));
}
2024-04-05 16:17:24 +00:00
API::RequestId API::addAsset (
const QString& title,
const QString& icon,
const QColor& color,
Models::Currency::ID currency,
const QJSValue& finished
) {
2024-01-20 21:17:21 +00:00
qDebug() << "Adding asset...";
if (magpie.getState() != Models::Magpie::Authenticated)
return callCallback(finished, "Can not add assets in current state"), 0;
2024-04-05 16:17:24 +00:00
auto add = std::make_unique<Request::AddAsset>(title, icon, color, currency, magpie.getAddress());
2024-01-20 21:17:21 +00:00
add->setAuthorizationToken(magpie.getAccessToken());
connect(add.get(), &Request::AddAsset::success, std::bind(&API::callCallback, this, finished, QString(), QJSValueList{QJSValue(true)}));
connect(add.get(), &Request::AddAsset::error, std::bind(&API::callCallback, this, finished, std::placeholders::_1, QJSValueList{QJSValue(false)}));
return registerAndSend(std::move(add));
2023-12-16 01:44:25 +00:00
}
API::RequestId API::updateAsset (
Models::Asset::ID id,
const QString& title,
const QString& icon,
const QColor& color,
Models::Currency::ID currency,
const QJSValue& finished
) {}
2024-01-21 19:22:56 +00:00
API::RequestId API::deleteAsset (unsigned int id, const QJSValue& finished) {
qDebug() << "Deleting asset...";
if (magpie.getState() != Models::Magpie::Authenticated)
return callCallback(finished, "Can not delete assets in current state"), 0;
2024-01-21 19:22:56 +00:00
auto del = std::make_unique<Request::DeleteAsset>(id, magpie.getAddress());
del->setAuthorizationToken(magpie.getAccessToken());
connect(del.get(), &Request::DeleteAsset::success, std::bind(&API::callCallback, this, finished, QString(), QJSValueList{QJSValue(true)}));
connect(del.get(), &Request::DeleteAsset::error, std::bind(&API::callCallback, this, finished, std::placeholders::_1, QJSValueList{QJSValue(false)}));
return registerAndSend(std::move(del));
}
2024-01-20 21:17:21 +00:00
API::RequestId API::poll (const SuccessMapHandler& success, const ErrorHandler& error, bool clear) {
auto poll = std::make_unique<Request::Poll>(magpie.getAddress(), clear);
poll->setAuthorizationToken(magpie.getAccessToken());
connect(poll.get(), &Request::Poll::success, success);
connect(poll.get(), &Request::Poll::error, error);
return registerAndSend(std::move(poll));
}
2024-01-20 21:17:21 +00:00
API::RequestId API::registerAndSend (std::unique_ptr<Request::Request> request) {
Request::Request* req = request.get();
requests.emplace(++idCounter, std::move(request));
connect(req, &Request::Request::done, std::bind(&API::onRequestDone, this, idCounter));
req->send(network);
return idCounter;
}
2024-01-20 21:17:21 +00:00
void API::callCallback (const QJSValue& callback, const QString& error, const QJSValueList& arguments) const {
2023-11-24 23:48:01 +00:00
if (callback.isCallable()) {
if (error.isEmpty())
callback.call(QJSValueList({QJSValue(QJSValue::NullValue)}) + arguments);
else
callback.call({QJSValue(error)});
}
}