Kagome
Polkadot Runtime Engine in C++17
jrpc_method.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CORE_API_JRPC_JRPC_METHOD_HPP
7 #define KAGOME_CORE_API_JRPC_JRPC_METHOD_HPP
8 
9 #include <jsonrpc-lean/dispatcher.h>
10 #include <jsonrpc-lean/request.h>
11 
12 #include <memory>
13 #include <type_traits>
14 
16 
17 namespace kagome::api {
18 
19  template <typename RequestType, typename Api>
20  class Method {
21  private:
22  std::weak_ptr<Api> api_;
23 
24  public:
25  explicit Method(const std::shared_ptr<Api> &api) : api_(api) {}
26 
27  jsonrpc::Value operator()(const jsonrpc::Request::Parameters &params) {
28  auto api = api_.lock();
29  if (not api) {
30  throw jsonrpc::Fault("API not available");
31  }
32 
33  RequestType request(api);
34 
35  // Init request
36  if (auto &&init_result = request.init(params); not init_result) {
37  throw jsonrpc::Fault(init_result.error().message());
38  }
39 
40  // Execute request
41  auto &&result = request.execute();
42 
43  // Handle of failure
44  if (not result) {
45  throw jsonrpc::Fault(result.error().message());
46  }
47 
48  if constexpr (std::is_same_v<decltype(result.value()), void>) {
49  return {};
50  } else {
51  return makeValue(result.value());
52  }
53  }
54  };
55 } // namespace kagome::api
56 
57 #endif // KAGOME_CORE_API_JRPC_JRPC_METHOD_HPP
jsonrpc::Value operator()(const jsonrpc::Request::Parameters &params)
Definition: jrpc_method.hpp:27
jsonrpc::Value makeValue(const uint32_t &)
Method(const std::shared_ptr< Api > &api)
Definition: jrpc_method.hpp:25
std::weak_ptr< Api > api_
Definition: jrpc_method.hpp:22