From 1e66dcc6be4b1a05d3743fddb1450338b10c6776 Mon Sep 17 00:00:00 2001 From: JohnDoe6345789 Date: Fri, 26 Dec 2025 06:11:23 +0000 Subject: [PATCH] code: cpp,frontends,cli (4 files) --- dbal/cpp/tests/unit/client_test.cpp | 4 +- .../cli/src/commands/command_dispatch.cpp | 1 + frontends/cli/src/main.cpp | 26 +++++++++++ frontends/cli/src/utils/http_client.cpp | 43 +++++++++++++++++++ 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 frontends/cli/src/main.cpp create mode 100644 frontends/cli/src/utils/http_client.cpp diff --git a/dbal/cpp/tests/unit/client_test.cpp b/dbal/cpp/tests/unit/client_test.cpp index 70087d078..7e5183748 100644 --- a/dbal/cpp/tests/unit/client_test.cpp +++ b/dbal/cpp/tests/unit/client_test.cpp @@ -1230,6 +1230,8 @@ int main() { test_create_user(); test_user_validation(); test_user_conflicts(); + test_user_search(); + test_user_count(); test_credential_crud(); test_credential_validation(); test_get_user(); @@ -1254,7 +1256,7 @@ int main() { std::cout << std::endl; std::cout << "==================================================" << std::endl; - std::cout << "✅ All 26 test suites passed!" << std::endl; + std::cout << "✅ All 28 test suites passed!" << std::endl; std::cout << "==================================================" << std::endl; return 0; } catch (const std::exception& e) { diff --git a/frontends/cli/src/commands/command_dispatch.cpp b/frontends/cli/src/commands/command_dispatch.cpp index 73903a2e6..fdbf54a65 100644 --- a/frontends/cli/src/commands/command_dispatch.cpp +++ b/frontends/cli/src/commands/command_dispatch.cpp @@ -2,6 +2,7 @@ #include #include +#include namespace { diff --git a/frontends/cli/src/main.cpp b/frontends/cli/src/main.cpp new file mode 100644 index 000000000..c9fba0c5d --- /dev/null +++ b/frontends/cli/src/main.cpp @@ -0,0 +1,26 @@ +#include "commands/command_dispatch.h" +#include "utils/http_client.h" + +#include +#include +#include +#include + +int main(int argc, char **argv) { + std::vector args; + args.reserve(std::max(0, argc - 1)); + for (int i = 1; i < argc; ++i) { + args.emplace_back(argv[i]); + } + + const char *env_base = std::getenv("METABUILDER_BASE_URL"); + const std::string base_url = env_base ? env_base : "http://localhost:3000"; + + try { + HttpClient client(base_url); + return commands::dispatch(client, args); + } catch (const std::exception &e) { + std::cerr << "failed to create HTTP client: " << e.what() << '\n'; + return 1; + } +} diff --git a/frontends/cli/src/utils/http_client.cpp b/frontends/cli/src/utils/http_client.cpp new file mode 100644 index 000000000..930278ac3 --- /dev/null +++ b/frontends/cli/src/utils/http_client.cpp @@ -0,0 +1,43 @@ +#include "utils/http_client.h" + +#include + +namespace { + +std::string build_url(const std::string &base, const std::string &path) { + std::string result = base; + if (!result.empty() && result.back() == '/') { + result.pop_back(); + } + + if (!path.empty()) { + if (path.front() != '/') { + result.push_back('/'); + } + result.append(path); + } + + return result; +} + +} // namespace + +HttpClient::HttpClient(std::string base_url) : base_url_(std::move(base_url)) { + if (base_url_.empty()) { + throw std::invalid_argument("base URL cannot be empty"); + } +} + +cpr::Response HttpClient::get(const std::string &path) const { + return cpr::Get(cpr::Url{build_url(base_url_, path)}); +} + +cpr::Response HttpClient::post(const std::string &path, + const std::string &body, + const std::string &content_type) const { + return cpr::Post(cpr::Url{build_url(base_url_, path)}, + cpr::Body{body}, + cpr::Header{{"Content-Type", content_type}}); +} + +const std::string &HttpClient::base_url() const noexcept { return base_url_; }