Skip to content

Commit

Permalink
Format JSON <response>
Browse files Browse the repository at this point in the history
  • Loading branch information
irfanghat committed Nov 15, 2024
1 parent 6987219 commit b0a05b3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ clean: cpp-clean

# deps:
# sudo apt -y install nlohmann-json3-dev
#
# 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
Binary file modified librender_cdk/main_executable
Binary file not shown.
24 changes: 19 additions & 5 deletions librender_cdk/src/authorization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
#include <curl/curl.h>
#include <iostream>
#include <jsoncpp/json/json.h>
#include <nlohmann/json.hpp>
#include <sstream>

// 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<OwnerResponse>
AuthorizationManager::list_authorized_users(const std::string &email,
Expand Down Expand Up @@ -45,13 +45,26 @@ 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;
std::string errs;
std::istringstream s(response);

if (Json::parseFromStream(reader, s, &jsonData, &errs)) {

// Pretty print JSON.
try {
nlohmann::json prettyJson = nlohmann::json::parse(response);
std::cout << "<response> -> \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(),
Expand Down Expand Up @@ -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
// g++ -I./librender_cdk/extern/dotenv-cpp/include src/main.cpp
// src/environment_manager.cpp src/authorization.cpp -o main_executable -lcurl
// -ljsoncpp
2 changes: 1 addition & 1 deletion librender_cdk/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
// 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
5 changes: 4 additions & 1 deletion render_cdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<render>@<email>.com
```
Expand All @@ -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/):
Expand Down

0 comments on commit b0a05b3

Please sign in to comment.