Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
21 changes: 21 additions & 0 deletions examples/messaging/topics/createTopic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "Appwrite.hpp"
#include <iostream>

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<std::string> 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;
}
1 change: 1 addition & 0 deletions include/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Utils {
static int deleteRequest(const std::string &url, const std::vector<std::string> &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
14 changes: 8 additions & 6 deletions include/classes/Messaging.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
#ifndef MESSAGING_HPP
#define MESSAGING_HPP

#include <string>
#include "Utils.hpp"
#include "exceptions/AppwriteException.hpp"
#include "enums/HttpStatus.hpp"
#include "exceptions/AppwriteException.hpp"
#include <string>

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<std::string> &subscribe);

private:
private:
std::string projectId;
std::string apiKey;
};
Expand Down
2 changes: 2 additions & 0 deletions src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,6 @@ namespace Utils
}
return ss.str();
}

std::string boolToString(bool value) { return value ? "true" : "false"; };
}
63 changes: 54 additions & 9 deletions src/services/Messaging.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include <iostream>
#include "classes/Messaging.hpp"
#include "Validator.hpp"
#include <sstream>
#include "Utils.hpp"
#include "Validator.hpp"
#include "config/Config.hpp"
#include "enums/HttpStatus.hpp"
#include "exceptions/AppwriteException.hpp"
#include <iostream>
#include <sstream>

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'");
}
Expand All @@ -19,17 +19,62 @@ std::string Messaging::getTopic(const std::string& topicId) {

std::vector<std::string> headers = Config::getHeaders(projectId);
headers.push_back("X-Appwrite-Key: " + apiKey);

std::string response;

int statusCode = Utils::getRequest(url, headers, response);
std::cout << response;

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<std::string> &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<std::string> 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);
}
}
10 changes: 4 additions & 6 deletions src/services/Storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) + "\",";
Expand All @@ -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<std::string> headers = Config::getHeaders(projectId);
headers.push_back("X-Appwrite-Key: " + apiKey);
Expand Down