Skip to content

Commit

Permalink
Add service_manager
Browse files Browse the repository at this point in the history
  • Loading branch information
irfanghat committed Nov 14, 2024
1 parent 101cbf4 commit 2972cd8
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 4 deletions.
50 changes: 46 additions & 4 deletions librender_cdk/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
#include "authorization.h"
#include "environment_manager.h"
#include "authorization.h"
#include "service_manager.h"
#include <iostream>

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();

Expand Down Expand Up @@ -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
82 changes: 82 additions & 0 deletions librender_cdk/src/service_manager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "service_manager.h"
#include <iostream>
#include <sstream>

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<Service> ServiceManager::list_services() {
CURL *curl = curl_easy_init();
std::vector<Service> 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;
}
34 changes: 34 additions & 0 deletions librender_cdk/src/service_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// service_manager.h
#ifndef SERVICE_MANAGER_H
#define SERVICE_MANAGER_H

#include <curl/curl.h>
#include <jsoncpp/json/json.h>
#include <string>
#include <vector>

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<Service> 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

0 comments on commit 2972cd8

Please sign in to comment.