2023-12-27 20:59:22 +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
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <memory>
|
2023-12-25 20:07:51 +00:00
|
|
|
#include <optional>
|
|
|
|
#include <map>
|
2023-11-24 23:48:01 +00:00
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QString>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QJSValue>
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkReply>
|
2023-12-27 20:59:22 +00:00
|
|
|
#include <QTimer>
|
2023-11-24 23:48:01 +00:00
|
|
|
|
2024-01-16 21:12:41 +00:00
|
|
|
#include "models/assets.h"
|
|
|
|
|
2023-12-17 00:06:04 +00:00
|
|
|
class Root;
|
2023-11-24 23:48:01 +00:00
|
|
|
class API : public QObject {
|
2023-12-17 00:06:04 +00:00
|
|
|
friend class Root;
|
2023-11-24 23:48:01 +00:00
|
|
|
Q_OBJECT
|
2023-12-16 01:44:25 +00:00
|
|
|
public:
|
2023-12-25 20:07:51 +00:00
|
|
|
enum State {
|
|
|
|
Offline,
|
|
|
|
NoServer,
|
|
|
|
NotAuthenticated,
|
|
|
|
Authenticating,
|
|
|
|
Authenticated
|
|
|
|
};
|
2023-12-16 01:44:25 +00:00
|
|
|
Q_ENUM(State)
|
|
|
|
|
|
|
|
private:
|
2023-12-17 00:06:04 +00:00
|
|
|
Q_PROPERTY(QUrl address READ getAddress NOTIFY addressChanged)
|
2023-12-16 01:44:25 +00:00
|
|
|
Q_PROPERTY(State state READ getState NOTIFY stateChanged)
|
|
|
|
|
2023-11-24 23:48:01 +00:00
|
|
|
public:
|
|
|
|
explicit API(const QUrl& path = QString(), QObject* parent = nullptr);
|
|
|
|
|
2023-12-16 01:44:25 +00:00
|
|
|
QUrl getAddress() const;
|
|
|
|
State getState() const;
|
2023-12-25 20:07:51 +00:00
|
|
|
void setTokens(const QString access, const QString& renew);
|
2023-12-27 20:59:22 +00:00
|
|
|
void startPolling();
|
2023-12-16 01:44:25 +00:00
|
|
|
|
|
|
|
signals:
|
|
|
|
void addressChanged(const QUrl& path);
|
|
|
|
void stateChanged(State state);
|
|
|
|
|
2023-12-25 20:07:51 +00:00
|
|
|
void storeTokens(const QString& access, const QString& renew);
|
|
|
|
|
2023-12-16 01:44:25 +00:00
|
|
|
public slots:
|
|
|
|
void test(const QString& path, const QJSValue& finished = QJSValue());
|
|
|
|
void sendRegister(const QString& login, const QString& password, const QJSValue& finished = QJSValue());
|
2023-12-21 18:03:44 +00:00
|
|
|
void sendLogin(const QString& login, const QString& password, const QJSValue& finished = QJSValue());
|
2024-01-16 21:12:41 +00:00
|
|
|
void addAsset(const QString& title, const QString& icon, const QJSValue& finished = QJSValue());
|
2023-11-24 23:48:01 +00:00
|
|
|
|
|
|
|
private slots:
|
2023-12-17 00:06:04 +00:00
|
|
|
void onTestFinished(QNetworkReply* reply, const QUrl& addr, const QJSValue& finished);
|
2023-12-16 01:44:25 +00:00
|
|
|
void onRegisterFinished(QNetworkReply* reply, const QJSValue& finished) const;
|
2023-12-21 18:03:44 +00:00
|
|
|
void onLoginFinished(QNetworkReply* reply, const QJSValue& finished);
|
2024-01-16 21:12:41 +00:00
|
|
|
void onAssetAdded(QNetworkReply* reply, const QJSValue& finished);
|
2024-01-10 19:27:03 +00:00
|
|
|
void onPollFinished();
|
2023-12-27 20:59:22 +00:00
|
|
|
|
|
|
|
void onFirstPollSuccess();
|
2023-11-24 23:48:01 +00:00
|
|
|
|
2024-01-16 21:12:41 +00:00
|
|
|
void requestAssets();
|
|
|
|
void responseAssets(QNetworkReply* reply);
|
|
|
|
|
2023-11-24 23:48:01 +00:00
|
|
|
private:
|
|
|
|
void callCallback(const QJSValue& callback, const QString& error = QString(), const QJSValueList& arguments = QJSValueList()) const;
|
2023-12-17 00:06:04 +00:00
|
|
|
void setAddress(const QUrl& path);
|
2023-12-25 20:07:51 +00:00
|
|
|
static std::optional<QVariantMap> readResult(QNetworkReply* reply);
|
|
|
|
static bool validateResponse(const std::optional<QVariantMap>& data, const std::map<QString, QMetaType::Type>& structure);
|
2023-12-26 23:31:55 +00:00
|
|
|
void setState(State newState);
|
2023-12-27 20:59:22 +00:00
|
|
|
QUrl createUrl(const QString& path) const;
|
2024-01-16 21:12:41 +00:00
|
|
|
void sendPoll(bool clear = false);
|
|
|
|
bool handleChanges(const QVariantMap& changes);
|
|
|
|
void resetAllModels();
|
2023-11-24 23:48:01 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
QUrl address;
|
|
|
|
QNetworkAccessManager network;
|
2023-12-16 01:44:25 +00:00
|
|
|
State state;
|
2023-12-25 20:07:51 +00:00
|
|
|
QString accessToken;
|
|
|
|
QString renewToken;
|
2023-12-27 20:59:22 +00:00
|
|
|
QTimer firstPoll;
|
2024-01-10 19:27:03 +00:00
|
|
|
std::unique_ptr<QNetworkReply> pollReply;
|
2024-01-16 21:12:41 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
Models::Assets assets;
|
2023-11-24 23:48:01 +00:00
|
|
|
};
|