0xc3 1 月之前
父節點
當前提交
00ca2fe77a
共有 5 個文件被更改,包括 151 次插入20 次删除
  1. 8 0
      .zed/debug.json
  2. 107 3
      src/saura/cmd/sandbox/client/client.hpp
  3. 33 11
      src/saura/cmd/sandbox/server/server.cpp
  4. 1 5
      src/saura/cmd/sandbox/server/server.hpp
  5. 2 1
      vcpkg.json

+ 8 - 0
.zed/debug.json

@@ -0,0 +1,8 @@
+[
+  {
+    "label": "Debug native binary",
+    "program": "$ZED_WORKTREE_ROOT/build/Debug/sandbox",
+    "request": "launch",
+    "adapter": "CodeLLDB"
+  }
+]

+ 107 - 3
src/saura/cmd/sandbox/client/client.hpp

@@ -1,11 +1,115 @@
 #ifndef SANDBOX_CLIENT_HPP_
 #define SANDBOX_CLIENT_HPP_
 
+#include <cstdint>
+#include <fmt/core.h>
+#include <fmt/format.h>
+#include <map>
+#include <string>
+#include <vector>
+
+#include <nlohmann/json.hpp>
+
 namespace sandbox {
-class Client {
-private:
-public:
+
+struct Money {
+  int64_t amount;
+  std::string currency;
+
+  Money() = default;
+  Money(int64_t amount, std::string currency)
+      : amount(amount), currency(std::move(currency)) {}
+
+  static Money from_double(double value, std::string currency) {
+    return Money(static_cast<int64_t>(value * 100 + 0.5), std::move(currency));
+  }
+
+  double to_double() const { return static_cast<double>(amount) / 100.0; }
+};
+
+inline void to_json(nlohmann::json &j, const Money &money) {
+  j = nlohmann::json{{"amount", money.amount}, {"currency", money.currency}};
+}
+
+inline void from_json(const nlohmann::json &j, Money &money) {
+  j.at("amount").get_to(money.amount);
+  j.at("currency").get_to(money.currency);
+}
+
+struct ClientInfo {
+  std::string id;
+  std::string old_id;
+  bool contractor = false;
+  std::string full_name;
+  std::string type;
+  std::string email;
+  std::string legal_address;
+  std::string physical_address;
+  std::string note;
+
+  Money income;
+
+  std::vector<std::string> tags;
+  std::vector<std::string> phones;
+  std::map<std::string, std::string> metadata;
+
+  ClientInfo() = default;
+
+  static void to_json(nlohmann::json &j, const ClientInfo &client) {
+    j = nlohmann::json{
+        {"id", client.id},
+        {"contractor", client.contractor},
+        {"full_name", client.full_name},
+        {"type", client.type},
+        {"tags", client.tags},
+        {"phones", client.phones},
+        {"metadata", client.metadata},
+        {"old_id", client.old_id},
+        {"email", client.email},
+        {"legal_address", client.legal_address},
+        {"physical_address", client.physical_address},
+        {"note", client.note},
+        {"income", client.income},
+    };
+  }
+
+  static void from_json(const nlohmann::json &j, ClientInfo &client) {
+    j.at("id").get_to(client.id);
+    j.at("full_name").get_to(client.full_name);
+    j.at("type").get_to(client.type);
+    j.at("old_id").get_to(client.old_id);
+    j.at("email").get_to(client.email);
+    j.at("legal_address").get_to(client.legal_address);
+    j.at("physical_address").get_to(client.physical_address);
+    j.at("note").get_to(client.note);
+    j.at("contractor").get_to(client.contractor);
+    j.at("income").get_to(client.income);
+    j.at("tags").get_to(client.tags);
+    j.at("phones").get_to(client.phones);
+    j.at("metadata").get_to(client.metadata);
+  }
+
+  static ClientInfo from_json(const nlohmann::json &j) {
+    ClientInfo client;
+    ClientInfo::from_json(j, client);
+    return client;
+  }
+
+  nlohmann::json to_json() const {
+    nlohmann::json j;
+    ClientInfo::to_json(j, *this);
+    return j;
+  }
 };
+
+inline void to_json(nlohmann::json &j, const ClientInfo &client) {
+  ClientInfo::to_json(j, client);
+}
+
+inline void from_json(const nlohmann::json &j, ClientInfo &client) {
+  ClientInfo::from_json(j, client);
+}
+
 } // namespace sandbox
 
 #endif

+ 33 - 11
src/saura/cmd/sandbox/server/server.cpp

@@ -1,16 +1,20 @@
 #include "server.hpp"
+#include "saura/cmd/sandbox/client/client.hpp"
 #include <chrono>
 #include <cpr/api.h>
 #include <cpr/cprtypes.h>
 #include <cpr/timeout.h>
+#include <fmt/ranges.h>
 
+#include <nlohmann/json.hpp>
+#include <nlohmann/json_fwd.hpp>
 #include <spdlog/spdlog.h>
 
 #include <thread>
 
 namespace saura {
 ServerAPI::ServerAPI() {
-  worker = std::thread([this]() { check_is_live(); });
+  worker = std::thread([this]() { run(); });
 }
 
 ServerAPI::~ServerAPI() {
@@ -18,21 +22,39 @@ ServerAPI::~ServerAPI() {
     worker.join();
 }
 
-void ServerAPI::check_is_live() {
+void ServerAPI::run() {
   while (true) {
-    auto res = cpr::Get(cpr::Url{"http://api.localhost:8090/ping"},
-                        cpr::Timeout{1000});
-    if (res.status_code == 200) {
-      is_live = true;
-    } else {
-      is_live = false;
+    {
+      auto res = cpr::Get(
+          cpr::Url{
+              "http://api.localhost:8090/v1/example/clients?limit=10&offset=0"},
+          cpr::Timeout{1000});
+      if (res.status_code == 200) {
+        auto res_json = nlohmann::json::parse(res.text);
+        spdlog::info("Response: {}", res_json.dump(2));
+
+        if (!res_json.is_array()) {
+          throw std::runtime_error("Expected JSON array but got " +
+                                   std::string(res_json.type_name()));
+        }
+        for (size_t i = 0; i < res_json.size(); ++i) {
+          const auto &client_json = res_json[i];
+
+          sandbox::ClientInfo client =
+              sandbox::ClientInfo::from_json(client_json);
+
+          spdlog::info("✅ Client #{} parsed: {}", i, client.full_name);
+          spdlog::info("   ID: {}", client.id);
+          spdlog::info("   Tags: {}", fmt::join(client.tags, ", "));
+          spdlog::info("   Phones: {}", fmt::join(client.phones, ", "));
+          spdlog::info("   Income: {} {}", client.income.amount,
+                       client.income.currency);
+        }
+      }
     }
 
-    spdlog::info("server.status_code: {0}", res.status_code);
-
     std::this_thread::sleep_for(std::chrono::seconds(1));
   }
 }
 
-bool ServerAPI::get_is_live() { return is_live; }
 } // namespace saura

+ 1 - 5
src/saura/cmd/sandbox/server/server.hpp

@@ -1,22 +1,18 @@
 #ifndef SAURA_SERVER_HPP_
 #define SAURA_SERVER_HPP_
 
-#include <atomic>
 #include <thread>
 
 namespace saura {
 class ServerAPI {
 private:
-  std::atomic<bool> is_live;
   std::thread worker;
 
 public:
   ServerAPI();
   ~ServerAPI();
 
-  void check_is_live();
-
-  bool get_is_live();
+  void run();
 };
 }; // namespace saura
 

+ 2 - 1
vcpkg.json

@@ -10,6 +10,7 @@
     "spdlog",
     "sdl3",
     "glm",
-    "cpr"
+    "cpr",
+    "nlohmann-json"
   ]
 }