magpie/API/api.h

59 lines
2.1 KiB
C
Raw Normal View History

// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
2023-11-24 23:48:01 +00:00
#pragma once
#include <memory>
2023-12-25 20:07:51 +00:00
#include <optional>
#include <map>
2024-01-20 21:17:21 +00:00
#include <functional>
2023-11-24 23:48:01 +00:00
#include <QObject>
#include <QString>
#include <QUrl>
#include <QJSValue>
#include <QNetworkAccessManager>
#include <QNetworkReply>
2024-01-20 21:17:21 +00:00
#include "models/magpie.h"
#include "requests/request.h"
2023-11-24 23:48:01 +00:00
class API : public QObject {
Q_OBJECT
2023-12-16 01:44:25 +00:00
2023-11-24 23:48:01 +00:00
public:
2024-01-20 21:17:21 +00:00
using SuccessMapHandler = std::function<void(const QVariantMap&)>;
using SuccessListHandler = std::function<void(const QVariantList&)>;
using ErrorHandler = std::function<void(const QString&, const std::optional<QVariantMap>&)>;
using RequestId = unsigned int;
explicit API(Models::Magpie& magpie, QObject* parent = nullptr);
2023-11-24 23:48:01 +00:00
2024-01-20 21:17:21 +00:00
RequestId requestAssets(const SuccessListHandler& success, const ErrorHandler& error);
RequestId requestCurrencies(const SuccessListHandler& success, const ErrorHandler& error);
2024-01-20 21:17:21 +00:00
RequestId poll(const SuccessMapHandler& success, const ErrorHandler& error, bool clear = false);
2023-12-16 01:44:25 +00:00
2024-01-20 21:17:21 +00:00
static const RequestId none = 0;
2023-12-25 20:07:51 +00:00
2023-12-16 01:44:25 +00:00
public slots:
2024-01-20 21:17:21 +00:00
void cancelRequest(RequestId id);
RequestId test(const QString& path, const QJSValue& finished = QJSValue());
RequestId sendRegister(const QString& login, const QString& password, const QJSValue& finished = QJSValue());
RequestId sendLogin(const QString& login, const QString& password, const QJSValue& finished = QJSValue());
2024-01-21 19:22:56 +00:00
RequestId addAsset(const QString& title, const QString& icon, const QColor& color, const QJSValue& finished = QJSValue());
RequestId deleteAsset(unsigned int id, const QJSValue& finished = QJSValue());
2023-11-24 23:48:01 +00:00
private slots:
2024-01-20 21:17:21 +00:00
void onRequestDone(RequestId id);
2023-11-24 23:48:01 +00:00
private:
void callCallback(const QJSValue& callback, const QString& error = QString(), const QJSValueList& arguments = QJSValueList()) const;
2024-01-20 21:17:21 +00:00
RequestId registerAndSend(std::unique_ptr<Request::Request> request);
2023-11-24 23:48:01 +00:00
private:
2024-01-20 21:17:21 +00:00
RequestId idCounter;
Models::Magpie& magpie;
2023-11-24 23:48:01 +00:00
QNetworkAccessManager network;
2024-01-20 21:17:21 +00:00
std::map<RequestId, std::unique_ptr<Request::Request>> requests;
2023-11-24 23:48:01 +00:00
};