From 2972cd8870a5466a50b11c6fe65ee45c6d5299f5 Mon Sep 17 00:00:00 2001 From: lexara-prime-ai Date: Thu, 14 Nov 2024 06:49:26 +0000 Subject: [PATCH] Add service_manager --- librender_cdk/src/main.cpp | 50 ++++++++++++++-- librender_cdk/src/service_manager.cpp | 82 +++++++++++++++++++++++++++ librender_cdk/src/service_manager.h | 34 +++++++++++ 3 files changed, 162 insertions(+), 4 deletions(-) create mode 100644 librender_cdk/src/service_manager.cpp create mode 100644 librender_cdk/src/service_manager.h diff --git a/librender_cdk/src/main.cpp b/librender_cdk/src/main.cpp index 1a27337..0a1fcb9 100644 --- a/librender_cdk/src/main.cpp +++ b/librender_cdk/src/main.cpp @@ -1,8 +1,42 @@ -#include "authorization.h" #include "environment_manager.h" +#include "authorization.h" +#include "service_manager.h" #include -int main() { +int test_list_services() { + // Load environment configuration. + Config config = load_config(); + if (config.api_key.empty()) { + std::cerr << "API key is missing in the configuration." << std::endl; + return -1; + } + + // Initialize the ServiceManager with the loaded API key. + ServiceManager service_manager(config.api_key); + + // Get a list of services. + auto services = service_manager.list_services(); + + // Process the response and print output for debugging. + if (!services.empty()) { + std::cout << "Fetched Services: " << std::endl; + for (const auto &service : services) { + std::cout << "ID: " << service.id << ", Name: " << service.name + << ", Branch: " << service.branch + << ", Dashboard URL: " << service.dashboardUrl + << ", Type: " << service.type + << ", Repository: " << service.repo + << ", Created At: " << service.createdAt + << ", Updated At: " << service.updatedAt << std::endl; + } + } else { + std::cout << "No services found." << std::endl; + } + + return 0; +} + +int test_list_authorized_users() { // Load environment configuration. Config config = load_config(); @@ -33,7 +67,15 @@ int main() { return 0; } +int main() { + test_list_authorized_users(); + test_list_services(); +} + // Testing // g++ -I./librender_cdk/extern/dotenv-cpp/include src/main.cpp -// src/environment_manager.cpp src/authorization.cpp -o main_executable -lcurl -// -ljsoncpp +// src/environment_manager.cpp src/authorization.cpp src/service_manager.cpp -o +// main_executable -lcurl -ljsoncpp + + +// g++ -I./librender_cdk/extern/dotenv-cpp/include src/main.cpp src/environment_manager.cpp src/authorization.cpp src/service_manager.cpp -o main_executable -lcurl -ljsoncpp \ No newline at end of file diff --git a/librender_cdk/src/service_manager.cpp b/librender_cdk/src/service_manager.cpp new file mode 100644 index 0000000..6742bd7 --- /dev/null +++ b/librender_cdk/src/service_manager.cpp @@ -0,0 +1,82 @@ +#include "service_manager.h" +#include +#include + +ServiceManager::ServiceManager(const std::string &api_key) + : api_key_(api_key) {} + +// Define WriteCallback as a static member function. +size_t ServiceManager::WriteCallback(void *contents, size_t size, size_t nmemb, + std::string *response) { + response->append((char *)contents, size * nmemb); + return size * nmemb; +} + +std::vector ServiceManager::list_services() { + CURL *curl = curl_easy_init(); + std::vector services; + + if (curl) { + std::string response; + std::string api_url = + "https://api.render.com/v1/services?includePreviews=true&limit=20"; + + // Set up headers. + struct curl_slist *headers = nullptr; + headers = curl_slist_append(headers, "accept: application/json"); + headers = curl_slist_append(headers, + ("Authorization: Bearer " + api_key_).c_str()); + + // Configure CURL options. + curl_easy_setopt(curl, CURLOPT_URL, api_url.c_str()); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, + ServiceManager::WriteCallback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); + + // Perform the request. + CURLcode res = curl_easy_perform(curl); + + // Request validation. + if (res == CURLE_OK) { + long http_code = 0; + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); + if (http_code == 200) { + // JSON response parsing. + Json::Value jsonData; + Json::CharReaderBuilder reader; + std::string errs; + std::istringstream s(response); + + if (Json::parseFromStream(reader, s, &jsonData, &errs)) { + for (const auto &item : jsonData) { + const auto &serviceData = item["service"]; + Service service{serviceData["id"].asString(), + serviceData["name"].asString(), + serviceData["branch"].asString(), + serviceData["dashboardUrl"].asString(), + serviceData["type"].asString(), + serviceData["repo"].asString(), + serviceData["createdAt"].asString(), + serviceData["updatedAt"].asString()}; + services.push_back(service); + } + } else { + std::cerr << "Error parsing JSON response: " << errs << std::endl; + } + } else { + std::cerr << "Request failed with HTTP status code: " << http_code + << std::endl; + } + } else { + std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) + << std::endl; + } + + // Clean up. + curl_slist_free_all(headers); + curl_easy_cleanup(curl); + } + + return services; +} diff --git a/librender_cdk/src/service_manager.h b/librender_cdk/src/service_manager.h new file mode 100644 index 0000000..60f72f5 --- /dev/null +++ b/librender_cdk/src/service_manager.h @@ -0,0 +1,34 @@ +// service_manager.h +#ifndef SERVICE_MANAGER_H +#define SERVICE_MANAGER_H + +#include +#include +#include +#include + +struct Service { + std::string id; + std::string name; + std::string branch; + std::string dashboardUrl; + std::string type; + std::string repo; + std::string createdAt; + std::string updatedAt; +}; + +class ServiceManager { +public: + ServiceManager(const std::string &api_key); + std::vector list_services(); + +private: + std::string api_key_; + + // Declare WriteCallback as a static member function. + static size_t WriteCallback(void *contents, size_t size, size_t nmemb, + std::string *response); +}; + +#endif