Size: 1306 bytes.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// cs/net/rpc/client.hh
#ifndef CS_NET_RPC_CLIENT_HH
#define CS_NET_RPC_CLIENT_HH

#include <map>
#include <string>

#include "cs/net/http/client.hh"
#include "cs/net/http/response.hh"
#include "cs/net/rpc/rpc.hh"
#include "cs/result.hh"

namespace cs::net::rpc {

template <typename RpcType>
class RPCClient {
 public:
  explicit RPCClient(const std::string& base_url)
      : base_url_(base_url) {}

  using RequestType = typename RpcType::RequestType;
  using ResponseType = typename RpcType::ResponseType;

  ResultOr<ResponseType> Call(const std::string& endpoint,
                              const RequestType& request) {
    // Build full URL from base URL + endpoint
    SET_OR_RET(
        auto http_response,
        cs::FetchResponse(
            base_url_ + endpoint, "POST",
            {{"Content-Type",
              cs::net::http::kContentTypeApplicationJson}},
            request.Serialize()));

    // Check HTTP status code
    if (http_response.status().code != 200) {
      return cs::Error(
          "HTTP " +
          std::to_string(http_response.status().code) +
          " " + http_response.status().name);
    }

    return ResponseType().Parse(http_response.body());
  }

 private:
  std::string base_url_;
};

}  // namespace cs::net::rpc

#endif  // CS_NET_RPC_CLIENT_HH
v0 (commit) © 2025 @p13i.io | Load balancer proxied to: cs-code-viewer-1:8080 in 4ms.