diff --git a/Makefile b/Makefile index 922a742..acfec3e 100644 --- a/Makefile +++ b/Makefile @@ -235,5 +235,8 @@ getQueueMigrations: $(SRCS) $(EXAMPLES_DIR)/health/params/getQueueMigrations.cpp # Messaging getTopic: $(SRCS) $(EXAMPLES_DIR)/messaging/topics/getTopic.cpp - @mkdir -p ./$(TESTS_DIR) + @mkdir -p ./$(TESTS_DIR) $(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/getTopic $(SRCS) $(EXAMPLES_DIR)/messaging/topics/getTopic.cpp $(LDFLAGS) +createTopic: $(SRCS) $(EXAMPLES_DIR)/messaging/topics/createTopic.cpp + @mkdir -p ./$(TESTS_DIR) + $(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/createTopic $(SRCS) $(EXAMPLES_DIR)/messaging/topics/createTopic.cpp $(LDFLAGS) diff --git a/examples/messaging/topics/createTopic.cpp b/examples/messaging/topics/createTopic.cpp new file mode 100644 index 0000000..92eb29d --- /dev/null +++ b/examples/messaging/topics/createTopic.cpp @@ -0,0 +1,21 @@ +#include "Appwrite.hpp" +#include + +int main() { + std::string projectId = "66fbb5a100070a3a1d19"; + std::string apiKey = ""; + Appwrite appwrite(projectId, apiKey); + + std::string topicId = "test_123"; + std::string name = "pingu_123"; + std::vector subscribe = {"users"}; + + try { + std::string response = appwrite.getMessaging().createTopic(topicId, name, subscribe); + std::cout << "Topic Created! \nResponse: " << response << std::endl; + } catch (const AppwriteException& ex) { + std::cerr << "Exception: " << ex.what() << std::endl; + } + + return 0; +} diff --git a/include/Utils.hpp b/include/Utils.hpp index 3e1a1fc..4e8ca45 100644 --- a/include/Utils.hpp +++ b/include/Utils.hpp @@ -13,6 +13,7 @@ class Utils { static int deleteRequest(const std::string &url, const std::vector &headers, std::string &response); static std::string urlEncode(const std::string& value); static std::string escapeJsonString(const std::string &input); + static std::string boolToString (bool value); }; #endif diff --git a/include/classes/Messaging.hpp b/include/classes/Messaging.hpp index 6546b6f..614e6a4 100644 --- a/include/classes/Messaging.hpp +++ b/include/classes/Messaging.hpp @@ -1,18 +1,20 @@ #ifndef MESSAGING_HPP #define MESSAGING_HPP -#include #include "Utils.hpp" -#include "exceptions/AppwriteException.hpp" #include "enums/HttpStatus.hpp" +#include "exceptions/AppwriteException.hpp" +#include class Messaging { -public: - Messaging(const std::string& projectId, const std::string& apiKey); + public: + Messaging(const std::string &projectId, const std::string &apiKey); - std::string getTopic(const std::string& topicId); + std::string getTopic(const std::string &topicId); + std::string createTopic(const std::string &topicId, const std::string &name, + const std::vector &subscribe); -private: + private: std::string projectId; std::string apiKey; }; diff --git a/src/Utils.cpp b/src/Utils.cpp index fe84918..fae4bfe 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -288,4 +288,6 @@ namespace Utils } return ss.str(); } + + std::string boolToString(bool value) { return value ? "true" : "false"; }; } diff --git a/src/services/Messaging.cpp b/src/services/Messaging.cpp index d41883a..c6b46b0 100644 --- a/src/services/Messaging.cpp +++ b/src/services/Messaging.cpp @@ -1,16 +1,16 @@ -#include #include "classes/Messaging.hpp" -#include "Validator.hpp" -#include #include "Utils.hpp" +#include "Validator.hpp" #include "config/Config.hpp" #include "enums/HttpStatus.hpp" #include "exceptions/AppwriteException.hpp" +#include +#include -Messaging::Messaging(const std::string& projectId, const std::string& apiKey) +Messaging::Messaging(const std::string &projectId, const std::string &apiKey) : projectId(projectId), apiKey(apiKey) {} -std::string Messaging::getTopic(const std::string& topicId) { +std::string Messaging::getTopic(const std::string &topicId) { if (topicId.empty()) { throw AppwriteException("Missing required parameter: 'topicId'"); } @@ -19,7 +19,7 @@ std::string Messaging::getTopic(const std::string& topicId) { std::vector headers = Config::getHeaders(projectId); headers.push_back("X-Appwrite-Key: " + apiKey); - + std::string response; int statusCode = Utils::getRequest(url, headers, response); @@ -27,9 +27,54 @@ std::string Messaging::getTopic(const std::string& topicId) { if (statusCode == HttpStatus::OK) { return response; - } - else { - throw AppwriteException("Error fetching health. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response); + } else { + throw AppwriteException("Error fetching health. Status code: " + + std::to_string(statusCode) + + "\n\nResponse: " + response); } } +std::string Messaging::createTopic(const std::string &topicId, + const std::string &name, + const std::vector &subscribe) { + if (topicId.empty()) { + throw AppwriteException("Missing required parameter: 'topicId'"); + } + + if (name.empty()) { + throw AppwriteException("Missing required parameter: 'name'"); + } + + std::string url = Config::API_BASE_URL + "/messaging/topics/"; + + std::string subscribeStr = "["; + for (const auto &ext : subscribe) { + subscribeStr += "\"" + Utils::escapeJsonString(ext) + "\","; + } + + if (!subscribe.empty()) + subscribeStr.pop_back(); + subscribeStr += "]"; + + std::string payload = R"({"topicId":")" + Utils::escapeJsonString(topicId) + + R"(",)" + R"("name":")" + + Utils::escapeJsonString(name) + "," + + R"("subscribe":)" + subscribeStr + R"("})"; + + std::cout << payload; + + std::vector headers = Config::getHeaders(projectId); + headers.push_back("X-Appwrite-Key: " + apiKey); + + std::string response; + + int statusCode = Utils::postRequest(url, payload, headers, response); + + if (statusCode == HttpStatus::CREATED) { + return response; + } else { + throw AppwriteException( + "Error Creating Topic. Status code: " + std::to_string(statusCode) + + "\n\nResponse: " + response); + } +} diff --git a/src/services/Storage.cpp b/src/services/Storage.cpp index fb1c725..4dd571c 100644 --- a/src/services/Storage.cpp +++ b/src/services/Storage.cpp @@ -61,8 +61,6 @@ Storage::updateBucket(const std::string &bucketId, const std::string &name, std::string url = Config::API_BASE_URL + "/storage/buckets/" + bucketId; - auto boolToString = [](bool value) { return value ? "true" : "false"; }; - std::string permissionsStr = "["; for (const auto &perm : permissions) { permissionsStr += "\"" + Utils::escapeJsonString(perm) + "\","; @@ -84,13 +82,13 @@ Storage::updateBucket(const std::string &bucketId, const std::string &name, std::string payload = R"({"name":")" + Utils::escapeJsonString(name) + R"(",)" + R"("permissions":)" + permissionsStr + "," + R"("fileSecurity":)" + - boolToString(fileSecurity) + "," + R"("enabled":)" + - boolToString(enabled) + "," + R"("maximumFileSize":)" + + Utils::boolToString(fileSecurity) + "," + R"("enabled":)" + + Utils::boolToString(enabled) + "," + R"("maximumFileSize":)" + std::to_string(maximumFileSize) + "," + R"("allowedFileExtensions":)" + extensionsStr + "," + R"("compression":")" + Utils::escapeJsonString(compression) + R"(",)" + R"("encryption":)" + - boolToString(encryption) + "," + R"("antivirus":)" + - boolToString(antivirus) + "}"; + Utils::boolToString(encryption) + "," + R"("antivirus":)" + + Utils::boolToString(antivirus) + "}"; std::vector headers = Config::getHeaders(projectId); headers.push_back("X-Appwrite-Key: " + apiKey);