SettingsService added

This commit is contained in:
2024-06-28 22:23:05 +03:00
parent 1383dcc56a
commit dc70f3fac4
10 changed files with 125 additions and 33 deletions

View File

@@ -24,6 +24,7 @@ add_executable(pocketbase-cpp
sdk/services/collectionservice.h sdk/services/collectionservice.cpp
sdk/services/adminservice.h sdk/services/adminservice.cpp
sdk/services/healthservice.h sdk/services/healthservice.cpp
sdk/services/settingsservice.h sdk/services/settingsservice.cpp
sdk/dtos/basemodel.h sdk/dtos/basemodel.cpp
sdk/dtos/listresult.h sdk/dtos/listresult.cpp
sdk/dtos/recordmodel.h sdk/dtos/collectionmodel.h
@@ -43,7 +44,6 @@ install(TARGETS pocketbase-cpp
# sdk/utils/sseclient.h sdk/utils/sseclient.cpp
# sdk/fileservice.h sdk/fileservice.cpp
# sdk/realtimeservice.h sdk/realtimeservice.cpp
# sdk/settingsservice.h sdk/settingsservice.cpp
# sdk/logservice.h sdk/logservice.cpp
# sdk/backupservice.h sdk/backupservice.cpp
# sdk/models/backupmodel.hpp

View File

@@ -32,6 +32,11 @@ Work still in progress, many breaking changes without notice as we try to match
- [ ] Realtime (pending)
- [X] Collections Service
- [ ] File Service
- [X] Health Service
- [ ] Backup Service
- [ ] Log Service
- [ ] Realtime Service
- [ ] Settings Service
## Installation

View File

@@ -7,6 +7,7 @@
#include "sdk/services/collectionservice.h"
#include "sdk/dtos/adminmodel.h"
#include "sdk/services/healthservice.h"
#include "sdk/services/settingsservice.h"
int main(int argc, char *argv[])
{
@@ -15,12 +16,12 @@ int main(int argc, char *argv[])
PocketBase client{"http://127.0.0.1:5740/"};
// ADMIN
// try {
// auto user = client.admins()->authWithPassword("admin@admin.com", "12345678901");
// qDebug() << "Admin User: " << user.getToken() << "\t: " << user.getAdmin()->data();
// } catch (ClientResponseError e) {
// qDebug() << "Error thrown! -> " << e.what() << "\t" << e.status();
// }
try {
auto user = client.admins()->authWithPassword("admin@admin.com", "12345678901");
qDebug() << "Admin User: " << user.getToken() << "\t: " << user.getAdmin()->data();
} catch (ClientResponseError e) {
qDebug() << "Error thrown! -> " << e.what() << "\t" << e.status();
}
// try {
// QJsonObject body;
@@ -111,10 +112,18 @@ int main(int argc, char *argv[])
qDebug() << "Error thrown! -> " << e.what() << "\t" << e.status();
}
// Health check
// try {
// auto health = client.health()->check();
// qDebug() << "New Admin User: " << health;
// } catch (ClientResponseError e) {
// qDebug() << "Error thrown! -> " << e.what() << "\t" << e.status();
// }
// Health check
try {
auto health = client.health()->check();
qDebug() << "New Admin User: " << health;
auto health = client.settings()->getAll();
qDebug() << "Settings: " << health;
} catch (ClientResponseError e) {
qDebug() << "Error thrown! -> " << e.what() << "\t" << e.status();
}

View File

@@ -5,6 +5,7 @@
#include "services/collectionservice.h"
#include "services/adminservice.h"
#include "services/healthservice.h"
#include "services/settingsservice.h"
PocketBase::PocketBase(const QString& baseUrl, const QString& lang, int timeout, QObject* parent)
: QObject(parent),
@@ -15,7 +16,8 @@ PocketBase::PocketBase(const QString& baseUrl, const QString& lang, int timeout,
m_authStore( new BaseAuthStore("", nullptr, this) ),
m_collectionService( new CollectionService(this, this) ),
m_adminService( new AdminService(this, this) ),
m_healthService( new HealthService(this, this) ) {}
m_healthService( new HealthService(this, this) ),
m_settingsService( new SettingsService(this, this)) {}
AdminService *PocketBase::admins() const { return m_adminService; }
@@ -48,6 +50,8 @@ BaseAuthStore *PocketBase::authStore() const { return m_authStore; }
HealthService *PocketBase::health() const { return m_healthService; }
SettingsService *PocketBase::settings() const { return m_settingsService; }
QJsonObject PocketBase::send(const QString& path, const QJsonObject params) {
QUrl url = buildUrl(path);
QNetworkRequest request;

View File

@@ -20,6 +20,7 @@ class RecordService;
class BaseAuthStore;
class CollectionService;
class HealthService;
class SettingsService;
class PocketBase : public QObject
{
@@ -35,7 +36,7 @@ public:
CollectionService* collections() const;
BaseAuthStore* authStore() const;
HealthService* health() const;
SettingsService* settings() const;
QJsonObject send(const QString& path, const QJsonObject params);
@@ -62,6 +63,7 @@ private:
CollectionService* m_collectionService;
AdminService* m_adminService;
HealthService* m_healthService;
SettingsService* m_settingsService;
QMap<QString, RecordService*> m_recordServices;
};

View File

@@ -1,5 +0,0 @@
#include "settingsservice.h"
SettingsService::SettingsService(QObject *parent)
: QObject{parent}
{}

View File

@@ -1,15 +0,0 @@
#ifndef SETTINGSSERVICE_H
#define SETTINGSSERVICE_H
#include <QObject>
class SettingsService : public QObject
{
Q_OBJECT
public:
explicit SettingsService(QObject *parent = nullptr);
signals:
};
#endif // SETTINGSSERVICE_H

View File

@@ -2,8 +2,7 @@
#include "../client.h"
HealthService::HealthService(PocketBase* client, QObject *parent)
: QObject{parent}, client(client)
{}
: QObject{parent}, client(client) {}
QJsonObject HealthService::check(const QJsonObject &params) {
QJsonObject payload=params;

View File

@@ -0,0 +1,53 @@
#include "settingsservice.h"
#include "../client.h"
SettingsService::SettingsService(PocketBase* client, QObject *parent)
: QObject{parent}, client(client)
{}
QJsonObject SettingsService::getAll(const QJsonObject &params) {
QJsonObject payload = params;
payload["method"] = "GET";
return client->send( "/api/settings", payload );
}
QJsonObject SettingsService::update(const QJsonObject &params) {
QJsonObject payload = params;
payload["method"] = "PATCH";
return client->send("/api/settings", payload);
}
QJsonObject SettingsService::testS3(QString filesystem) {
QJsonObject payload, body;
payload["method"] = "POST";
body["filesystem"] = filesystem;
return client->send("/api/settings/test/s3", payload);
}
bool SettingsService::testEmail(QString toEmail, QString emailTemplate) {
// verification, password-reset or email-change
QJsonObject payload, body;
body["email"] = toEmail;
body["template"] = emailTemplate;
payload["body"] = body;
payload["method"] = "POST";
auto responseJSON = client->send("/api/settings/test/email", payload);
return responseJSON.value("statusCode").toInt() == 204;
}
QJsonObject SettingsService::generateAppleClientSecret(
QString clientId, QString teamId, QString keyId, QString privateKey, int duration) {
QJsonObject payload, body;
body["clientId"] = clientId;
body["teamId"] = teamId;
body["keyId"] = keyId;
body["privateKey"] = privateKey;
body["duration"] = duration;
payload["method"] = "POST";
payload["body"] = body;
return client->send("/api/settings/apple/generate-client-secret", payload);
}

View File

@@ -0,0 +1,40 @@
#ifndef SETTINGSSERVICE_H
#define SETTINGSSERVICE_H
#include <QObject>
#include <QJsonObject>
class PocketBase;
class SettingsService : public QObject
{
Q_OBJECT
public:
explicit SettingsService(PocketBase* client, QObject *parent = nullptr);
/// Fetch all available app settings.
QJsonObject getAll( const QJsonObject& params = {} );
/// Bulk updates app settings.
QJsonObject update(const QJsonObject& params);
/// Performs a S3 storage connection test.
QJsonObject testS3(QString filesystem = "storage");
/// Sends a test email.
///
/// The possible `template` values are:
/// - verification
/// - password-reset
/// - email-change
bool testEmail(QString toEmail, QString emailTemplate="verification");
/// Generates a new Apple OAuth2 client secret.
QJsonObject generateAppleClientSecret( QString clientId, QString teamId, QString keyId,
QString privateKey, int duration);
private:
PocketBase* client;
};
#endif // SETTINGSSERVICE_H