diff --git a/Makefile b/Makefile index 1d1c75d..c750b90 100644 --- a/Makefile +++ b/Makefile @@ -81,4 +81,4 @@ clean: cpp-clean # deps: # sudo apt -y install nlohmann-json3-dev -# \ No newline at end of file +# Quick build command: g++ -I./librender_cdk/extern/dotenv-cpp/include -I./librender_cdk/extern/nlohmann-json/include src/main.cpp src/environment_manager.cpp src/authorization.cpp src/service_manager.cpp -o main_executable -lcurl -ljsoncpp diff --git a/librender_cdk/main_executable b/librender_cdk/main_executable index 79da8db..5d56b30 100755 Binary files a/librender_cdk/main_executable and b/librender_cdk/main_executable differ diff --git a/librender_cdk/src/authorization.cpp b/librender_cdk/src/authorization.cpp index 60fe070..58b8b93 100644 --- a/librender_cdk/src/authorization.cpp +++ b/librender_cdk/src/authorization.cpp @@ -2,18 +2,18 @@ #include #include #include +#include #include +// Define an anonymous namespace for the WriteCallback function. namespace { - // Callback function for handling the data received from `libcurl`. size_t WriteCallback(void *contents, size_t size, size_t nmemb, std::string *response) { response->append((char *)contents, size * nmemb); return size * nmemb; } - -} // anonymous namespace. +} // namespace std::vector AuthorizationManager::list_authorized_users(const std::string &email, @@ -45,6 +45,7 @@ AuthorizationManager::list_authorized_users(const std::string &email, 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; @@ -52,6 +53,18 @@ AuthorizationManager::list_authorized_users(const std::string &email, std::istringstream s(response); if (Json::parseFromStream(reader, s, &jsonData, &errs)) { + + // Pretty print JSON. + try { + nlohmann::json prettyJson = nlohmann::json::parse(response); + std::cout << " -> \n" + << prettyJson.dump(4) + << std::endl; // 4 spaces for indentation + } catch (const nlohmann::json::parse_error &e) { + std::cerr << "Failed to parse JSON with nlohmann::json: " + << e.what() << std::endl; + } + for (const auto &item : jsonData) { Owner owner{item["owner"]["id"].asString(), item["owner"]["name"].asString(), @@ -86,6 +99,7 @@ AuthorizationManager::list_authorized_users(const std::string &email, return ownerResponses; } - // Quick compilation: -// g++ -I./librender_cdk/extern/dotenv-cpp/include src/main.cpp src/environment_manager.cpp src/authorization.cpp -o main_executable -lcurl -ljsoncpp \ No newline at end of file +// g++ -I./librender_cdk/extern/dotenv-cpp/include src/main.cpp +// src/environment_manager.cpp src/authorization.cpp -o main_executable -lcurl +// -ljsoncpp \ No newline at end of file diff --git a/librender_cdk/src/main.cpp b/librender_cdk/src/main.cpp index 2467333..b5bc03c 100644 --- a/librender_cdk/src/main.cpp +++ b/librender_cdk/src/main.cpp @@ -78,4 +78,4 @@ int main() { // main_executable -lcurl -ljsoncpp // sudo apt-get install libjsoncpp-dev -// 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 +// g++ -I./librender_cdk/extern/dotenv-cpp/include -I./librender_cdk/extern/nlohmann-json/include src/main.cpp src/environment_manager.cpp src/authorization.cpp src/service_manager.cpp -o main_executable -lcurl -ljsoncpp diff --git a/render_cdk/README.md b/render_cdk/README.md index 0131c6a..264a361 100644 --- a/render_cdk/README.md +++ b/render_cdk/README.md @@ -5,7 +5,7 @@ To configure the environment variables for use with `render_cdk`, you need to set the `API_KEY` and `OWNER_CREDENTIALS` environment variables. You can do this by creating a `.env` file in the root of your project with the following content: -```.env +```toml API_KEY=rnd_xxxxXXXXxxxxXXXXxxxXX OWNER_CREDENTIALS=@.com ``` @@ -23,6 +23,9 @@ render_cdk = "0.0.21" * Alternatively, running `cargo add render_cdk` at the root of your project will also add it to your project. +## Building from source +To build both the `rust` crate and `cpp` library, simply clone the repository, and run `make release-build`. + ### Usage Examples Here are basic examples of how to use the `render_cdk` crate to interact with [Render Cloud](https://render.com/):