diff --git a/CMakeLists.txt b/CMakeLists.txt index 892a27f..1ffd3fe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/README.md b/README.md index 02c7a2b..4fc13be 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/main.cpp b/main.cpp index 67d5afb..7dc3d3c 100644 --- a/main.cpp +++ b/main.cpp @@ -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(); } diff --git a/sdk/client.cpp b/sdk/client.cpp index af101b9..7bba9fd 100644 --- a/sdk/client.cpp +++ b/sdk/client.cpp @@ -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; diff --git a/sdk/client.h b/sdk/client.h index 0b30806..f8d7006 100644 --- a/sdk/client.h +++ b/sdk/client.h @@ -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 m_recordServices; }; diff --git a/sdk/services/_pending/settingsservice.cpp b/sdk/services/_pending/settingsservice.cpp deleted file mode 100644 index 3739332..0000000 --- a/sdk/services/_pending/settingsservice.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include "settingsservice.h" - -SettingsService::SettingsService(QObject *parent) - : QObject{parent} -{} diff --git a/sdk/services/_pending/settingsservice.h b/sdk/services/_pending/settingsservice.h deleted file mode 100644 index bae4758..0000000 --- a/sdk/services/_pending/settingsservice.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef SETTINGSSERVICE_H -#define SETTINGSSERVICE_H - -#include - -class SettingsService : public QObject -{ - Q_OBJECT -public: - explicit SettingsService(QObject *parent = nullptr); - -signals: -}; - -#endif // SETTINGSSERVICE_H diff --git a/sdk/services/healthservice.cpp b/sdk/services/healthservice.cpp index 04cfb89..8a6145a 100644 --- a/sdk/services/healthservice.cpp +++ b/sdk/services/healthservice.cpp @@ -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 ¶ms) { QJsonObject payload=params; diff --git a/sdk/services/settingsservice.cpp b/sdk/services/settingsservice.cpp new file mode 100644 index 0000000..dcf5ebc --- /dev/null +++ b/sdk/services/settingsservice.cpp @@ -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 ¶ms) { + QJsonObject payload = params; + payload["method"] = "GET"; + return client->send( "/api/settings", payload ); +} + +QJsonObject SettingsService::update(const QJsonObject ¶ms) { + 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); +} diff --git a/sdk/services/settingsservice.h b/sdk/services/settingsservice.h new file mode 100644 index 0000000..76fd029 --- /dev/null +++ b/sdk/services/settingsservice.h @@ -0,0 +1,40 @@ +#ifndef SETTINGSSERVICE_H +#define SETTINGSSERVICE_H + +#include +#include + +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