mirror of
https://github.com/allankoechke/pocketbase-cpp.git
synced 2026-12-01 17:29:03 +00:00
Added SSEClient, Event and EventLoop
This commit is contained in:
@@ -31,6 +31,10 @@ add_executable(pocketbase-cpp
|
||||
sdk/dtos/recordmodel.h sdk/dtos/collectionmodel.h
|
||||
sdk/dtos/AdminModel.h
|
||||
sdk/dtos/logsmodel.h
|
||||
sdk/services/realtimeservice.h sdk/services/realtimeservice.cpp
|
||||
sdk/services/sseclient.h sdk/services/sseclient.cpp
|
||||
sdk/services/eventloop.h sdk/services/eventloop.cpp
|
||||
sdk/services/event.h sdk/services/event.cpp
|
||||
)
|
||||
target_link_libraries(pocketbase-cpp Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Network)
|
||||
|
||||
@@ -41,13 +45,10 @@ install(TARGETS pocketbase-cpp
|
||||
)
|
||||
|
||||
|
||||
|
||||
# sdk/localauthstore.h sdk/localauthstore.cpp
|
||||
# sdk/models/utils/schemafield.h sdk/models/utils/schemafield.cpp
|
||||
# sdk/utils/sseclient.h sdk/utils/sseclient.cpp
|
||||
# sdk/models/schemafield.h sdk/models/schemafield.cpp
|
||||
# sdk/fileservice.h sdk/fileservice.cpp
|
||||
# sdk/realtimeservice.h sdk/realtimeservice.cpp
|
||||
# sdk/backupservice.h sdk/backupservice.cpp
|
||||
# sdk/models/backupmodel.hpp
|
||||
# sdk/models/fileuploadmodel.hpp
|
||||
# sdk/utils/eventloop.h sdk/utils/eventloop.cpp
|
||||
# sdk/event.h sdk/event.cpp
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
#ifndef ADMINSERVICE_H
|
||||
#define ADMINSERVICE_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <memory>
|
||||
#include <QString>
|
||||
#include <QJsonObject>
|
||||
|
||||
#include "client.h"
|
||||
#include "utils/crudservice.h"
|
||||
#include "models/AdminModel.h"
|
||||
#include "baseauthstore.h"
|
||||
#include "recordservice.h"
|
||||
|
||||
class AdminAuthResponse {
|
||||
public:
|
||||
AdminAuthResponse(const QString& token, std::shared_ptr<AdminModel> admin);
|
||||
|
||||
QString getToken() const;
|
||||
std::shared_ptr<AdminModel> getAdmin() const;
|
||||
|
||||
private:
|
||||
QString token;
|
||||
std::shared_ptr<AdminModel> admin;
|
||||
};
|
||||
|
||||
class AdminService : public CrudService {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit AdminService(QPocketBase* client, QObject* parent = nullptr);
|
||||
|
||||
std::shared_ptr<BaseModel> decode(const QJsonObject& data) override;
|
||||
QString baseCrudPath() const override;
|
||||
|
||||
std::shared_ptr<BaseModel> update(const QString& id, const QJsonObject& bodyParams, const QJsonObject& queryParams = QJsonObject());
|
||||
bool deleteItem(const QString& id, const QJsonObject& queryParams = QJsonObject());
|
||||
|
||||
AdminAuthResponse authResponse(const QJsonObject& responseData);
|
||||
QJsonObject authWithPassword(const QString& email, const QString& password, const QJsonObject& bodyParams = QJsonObject(), const QJsonObject& queryParams = QJsonObject());
|
||||
AdminAuthResponse authRefresh(const QJsonObject& bodyParams = QJsonObject(), const QJsonObject& queryParams = QJsonObject());
|
||||
bool requestPasswordReset(const QString& email, const QJsonObject& bodyParams = QJsonObject(), const QJsonObject& queryParams = QJsonObject());
|
||||
bool confirmPasswordReset(const QString& passwordResetToken, const QString& password, const QString& passwordConfirm, const QJsonObject& bodyParams = QJsonObject(), const QJsonObject& queryParams = QJsonObject());
|
||||
};
|
||||
|
||||
|
||||
#endif // ADMINSERVICE_H
|
||||
@@ -1,5 +0,0 @@
|
||||
#include "collectionservice.h"
|
||||
|
||||
CollectionService::CollectionService(QObject *parent)
|
||||
: QObject{parent}
|
||||
{}
|
||||
@@ -1,15 +0,0 @@
|
||||
#ifndef COLLECTIONSERVICE_H
|
||||
#define COLLECTIONSERVICE_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class CollectionService : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit CollectionService(QObject *parent = nullptr);
|
||||
|
||||
signals:
|
||||
};
|
||||
|
||||
#endif // COLLECTIONSERVICE_H
|
||||
@@ -1,76 +0,0 @@
|
||||
#include "LocalAuthStore.h"
|
||||
#include "RecordService.h"
|
||||
#include "AdminService.h"
|
||||
|
||||
#include <QVariant>
|
||||
#include <QVariantMap>
|
||||
#include <QFile>
|
||||
#include <QDataStream>
|
||||
#include <QFileInfo>
|
||||
|
||||
LocalAuthStore::LocalAuthStore(const QString& filename, const QString& filepath, const QString& baseToken, std::shared_ptr<RecordService> baseModel, QObject* parent)
|
||||
: BaseAuthStore(baseToken, baseModel, parent), filename(filename), filepath(filepath) {
|
||||
completeFilepath = QDir(filepath).filePath(filename);
|
||||
}
|
||||
|
||||
QString LocalAuthStore::token() const {
|
||||
QVariantMap data = storageGet(completeFilepath);
|
||||
if (data.isEmpty() || !data.contains("token")) {
|
||||
return QString();
|
||||
}
|
||||
return data["token"].toString();
|
||||
}
|
||||
|
||||
std::shared_ptr<RecordService> LocalAuthStore::model() const {
|
||||
QVariantMap data = storageGet(completeFilepath);
|
||||
if (data.isEmpty() || !data.contains("model")) {
|
||||
return nullptr;
|
||||
}
|
||||
// Assuming Record has a method to deserialize from QVariant
|
||||
// return std::make_shared<RecordService>(data["model"].toMap());
|
||||
return std::make_shared<RecordService>(nullptr, "");
|
||||
}
|
||||
|
||||
void LocalAuthStore::save(const QString& token, std::shared_ptr<RecordService> model) {
|
||||
QVariantMap data;
|
||||
data["token"] = token;
|
||||
if (model) {
|
||||
// Assuming Record has a method to serialize to QVariant
|
||||
// data["model"] = QVariant::fromValue(model->toVariantMap());
|
||||
}
|
||||
|
||||
storageSet(completeFilepath, data);
|
||||
BaseAuthStore::save(token, model);
|
||||
}
|
||||
|
||||
void LocalAuthStore::clear() {
|
||||
storageRemove(completeFilepath);
|
||||
BaseAuthStore::clear();
|
||||
}
|
||||
|
||||
void LocalAuthStore::storageSet(const QString& key, const QVariantMap& value) const {
|
||||
QFile file(key);
|
||||
if (file.open(QIODevice::WriteOnly)) {
|
||||
QDataStream out(&file);
|
||||
out << value;
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
QVariantMap LocalAuthStore::storageGet(const QString& key) const {
|
||||
QVariantMap value;
|
||||
QFile file(key);
|
||||
if (file.exists() && file.open(QIODevice::ReadOnly)) {
|
||||
QDataStream in(&file);
|
||||
in >> value;
|
||||
file.close();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
void LocalAuthStore::storageRemove(const QString& key) const {
|
||||
QFile file(key);
|
||||
if (file.exists()) {
|
||||
file.remove();
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
#ifndef LOCALAUTHSTORE_H
|
||||
#define LOCALAUTHSTORE_H
|
||||
|
||||
#include "BaseAuthStore.h"
|
||||
#include <QString>
|
||||
#include <QFile>
|
||||
#include <QDataStream>
|
||||
#include <QDir>
|
||||
#include <memory>
|
||||
|
||||
class RecordService;
|
||||
class AdminService;
|
||||
|
||||
class LocalAuthStore : public BaseAuthStore {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit LocalAuthStore(const QString& filename = "pocketbase_auth.data", const QString& filepath = "", const QString& baseToken = "", std::shared_ptr<RecordService> baseModel = nullptr, QObject* parent = nullptr);
|
||||
|
||||
QString token() const;
|
||||
std::shared_ptr<RecordService> model() const;
|
||||
|
||||
void save(const QString& token = "", std::shared_ptr<RecordService> model = nullptr);
|
||||
void clear();
|
||||
|
||||
private:
|
||||
QString filename;
|
||||
QString filepath;
|
||||
QString completeFilepath;
|
||||
|
||||
void storageSet(const QString& key, const QVariantMap& value) const;
|
||||
QVariantMap storageGet(const QString& key) const;
|
||||
void storageRemove(const QString& key) const;
|
||||
};
|
||||
|
||||
#endif // LOCALAUTHSTORE_H
|
||||
|
||||
@@ -5,7 +5,10 @@
|
||||
|
||||
class Event {
|
||||
public:
|
||||
Event(const QString& id = "", const QString& event = "message", const QString& data = "", int retry = -1);
|
||||
Event(const QString& id = "",
|
||||
const QString& event = "message",
|
||||
const QString& data = "",
|
||||
int retry = -1);
|
||||
|
||||
QString getId() const;
|
||||
QString getEvent() const;
|
||||
@@ -1,7 +1,13 @@
|
||||
#include "SSEClient.h"
|
||||
|
||||
Event::Event(const QString& id, const QString& event, const QString& data, int retry)
|
||||
: id(id), event(event), data(data), retry(retry) {}
|
||||
Event::Event(const QString& id,
|
||||
const QString& event,
|
||||
const QString& data,
|
||||
int retry)
|
||||
: id(id),
|
||||
event(event),
|
||||
data(data),
|
||||
retry(retry) {}
|
||||
|
||||
QString Event::getId() const { return id; }
|
||||
|
||||
@@ -11,7 +17,12 @@ QString Event::getData() const { return data; }
|
||||
|
||||
int Event::getRetry() const { return retry; }
|
||||
|
||||
EventLoop::EventLoop(const QString& url, const QString& method, const QMap<QString, QString>& headers, const QByteArray& payload, const QString& encoding, QObject* parent)
|
||||
EventLoop::EventLoop(const QString& url,
|
||||
const QString& method,
|
||||
const QMap<QString, QString>& headers,
|
||||
const QByteArray& payload,
|
||||
const QString& encoding,
|
||||
QObject* parent)
|
||||
: QThread(parent),
|
||||
url(url),
|
||||
method(method),
|
||||
@@ -23,7 +34,8 @@ EventLoop::EventLoop(const QString& url, const QString& method, const QMap<QStri
|
||||
networkManager = new QNetworkAccessManager(this);
|
||||
}
|
||||
|
||||
void EventLoop::addListener(const QString& event, std::function<void(Event)> listener) {
|
||||
void EventLoop::addListener(const QString& event,
|
||||
std::function<void(Event)> listener) {
|
||||
listeners[event] = listener;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,10 @@ class Event : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit Event(const QString& id = "", const QString& event = "message", const QString& data = "", int retry = -1);
|
||||
explicit Event(const QString& id = "",
|
||||
const QString& event = "message",
|
||||
const QString& data = "",
|
||||
int retry = -1);
|
||||
|
||||
QString getId() const;
|
||||
void setId(const QString& nId) { id = nId; };
|
||||
@@ -38,7 +41,12 @@ class EventLoop : public QThread {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit EventLoop(const QString& url, const QString& method = "GET", const QMap<QString, QString>& headers = {}, const QByteArray& payload = {}, const QString& encoding = "utf-8", QObject* parent = nullptr);
|
||||
explicit EventLoop(const QString& url,
|
||||
const QString& method = "GET",
|
||||
const QMap<QString, QString>& headers = {},
|
||||
const QByteArray& payload = {},
|
||||
const QString& encoding = "utf-8",
|
||||
QObject* parent = nullptr);
|
||||
void addListener(const QString& event, std::function<void(Event)> listener);
|
||||
void removeListener(const QString& event);
|
||||
void run() override;
|
||||
@@ -69,8 +77,14 @@ class SSEClient : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit SSEClient(const QString& url, const QString& method = "GET", const QMap<QString, QString>& headers = {}, const QByteArray& payload = QByteArray(), const QString& encoding = "utf-8", QObject* parent = nullptr);
|
||||
void addEventListener(const QString& event, std::function<void(const Event&)> callback);
|
||||
explicit SSEClient(const QString& url,
|
||||
const QString& method = "GET",
|
||||
const QMap<QString, QString>& headers = {},
|
||||
const QByteArray& payload = QByteArray(),
|
||||
const QString& encoding = "utf-8",
|
||||
QObject* parent = nullptr);
|
||||
void addEventListener(const QString& event,
|
||||
std::function<void(const Event&)> callback);
|
||||
void removeEventListener(const QString& event);
|
||||
void close();
|
||||
|
||||
Reference in New Issue
Block a user