magpie/API/api.h

93 lines
2.7 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>
2023-11-24 23:48:01 +00:00
#include <QObject>
#include <QString>
#include <QUrl>
#include <QJSValue>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QTimer>
2023-11-24 23:48:01 +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);
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());
void sendLogin(const QString& login, const QString& password, const QJSValue& finished = QJSValue());
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;
void onLoginFinished(QNetworkReply* reply, const QJSValue& finished);
void onAssetAdded(QNetworkReply* reply, const QJSValue& finished);
void onPollFinished();
void onFirstPollSuccess();
2023-11-24 23:48:01 +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);
void setState(State newState);
QUrl createUrl(const QString& path) const;
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;
QTimer firstPoll;
std::unique_ptr<QNetworkReply> pollReply;
public:
Models::Assets assets;
2023-11-24 23:48:01 +00:00
};