JSON-RPC 2.0
JSON-RPC 2.0 Modern C++ Library
Loading...
Searching...
No Matches
jsonrpc::endpoint::Response Class Reference

#include <response.hpp>

Collaboration diagram for jsonrpc::endpoint::Response:

Public Member Functions

 Response ()=default
 
 Response (const Response &)=default
 
 Response (Response &&other)=default
 
auto operator= (const Response &) -> Response &=default
 
auto operator= (Response &&other) noexcept -> Response &=default
 
 ~Response ()=default
 
auto IsSuccess () const -> bool
 
auto GetResult () const -> const nlohmann::json &
 
auto GetError () const -> const nlohmann::json &
 
auto GetId () const -> std::optional< RequestId >
 
auto ToJson () const -> nlohmann::json
 

Static Public Member Functions

static auto FromJson (const nlohmann::json &json) -> std::expected< Response, error::RpcError >
 
static auto CreateSuccess (const nlohmann::json &result, const std::optional< RequestId > &id) -> Response
 
static auto CreateError (RpcErrorCode code, const std::optional< RequestId > &id=std::nullopt) -> Response
 
static auto CreateError (const RpcError &error, const std::optional< RequestId > &id=std::nullopt) -> Response
 
static auto CreateError (const nlohmann::json &error, const std::optional< RequestId > &id) -> Response
 

Detailed Description

Definition at line 16 of file response.hpp.

Constructor & Destructor Documentation

◆ Response() [1/3]

jsonrpc::endpoint::Response::Response ( )
default

◆ Response() [2/3]

jsonrpc::endpoint::Response::Response ( const Response & )
default

◆ Response() [3/3]

jsonrpc::endpoint::Response::Response ( Response && other)
default

◆ ~Response()

jsonrpc::endpoint::Response::~Response ( )
default

Member Function Documentation

◆ CreateError() [1/3]

auto jsonrpc::endpoint::Response::CreateError ( const nlohmann::json & error,
const std::optional< RequestId > & id ) -> Response
static

Definition at line 49 of file response.cpp.

51 {
52 nlohmann::json response = {{"jsonrpc", kJsonRpcVersion}, {"error", error}};
53 if (id) {
54 std::visit([&response](const auto& v) { response["id"] = v; }, *id);
55 }
56 return Response{std::move(response)};
57}
constexpr std::string_view kJsonRpcVersion
Definition types.hpp:13

References jsonrpc::endpoint::kJsonRpcVersion.

◆ CreateError() [2/3]

auto jsonrpc::endpoint::Response::CreateError ( const RpcError & error,
const std::optional< RequestId > & id = std::nullopt ) -> Response
static

Definition at line 43 of file response.cpp.

44 {
45 nlohmann::json error_json = error.to_json();
46 return CreateError(error_json, id);
47}
static auto CreateError(RpcErrorCode code, const std::optional< RequestId > &id=std::nullopt) -> Response
Definition response.cpp:28

◆ CreateError() [3/3]

auto jsonrpc::endpoint::Response::CreateError ( RpcErrorCode code,
const std::optional< RequestId > & id = std::nullopt ) -> Response
static

Definition at line 28 of file response.cpp.

29 {
30 RpcError err = RpcError::FromCode(code);
31
32 nlohmann::json error = {
33 {"code", static_cast<int>(err.Code())}, {"message", err.Message()}};
34 nlohmann::json response = {{"jsonrpc", kJsonRpcVersion}, {"error", error}};
35 if (id) {
36 std::visit([&response](const auto& v) { response["id"] = v; }, *id);
37 } else {
38 response["id"] = nullptr;
39 }
40 return Response{std::move(response)};
41}
static auto FromCode(RpcErrorCode code, std::string message="") -> RpcError
Definition error.hpp:93

References jsonrpc::error::RpcError::Code(), jsonrpc::error::RpcError::FromCode(), jsonrpc::endpoint::kJsonRpcVersion, and jsonrpc::error::RpcError::Message().

Referenced by jsonrpc::endpoint::Dispatcher::DispatchRequest().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CreateSuccess()

auto jsonrpc::endpoint::Response::CreateSuccess ( const nlohmann::json & result,
const std::optional< RequestId > & id ) -> Response
static

Definition at line 18 of file response.cpp.

20 {
21 nlohmann::json response = {{"jsonrpc", kJsonRpcVersion}, {"result", result}};
22 if (id) {
23 std::visit([&response](const auto& v) { response["id"] = v; }, *id);
24 }
25 return Response{std::move(response)};
26}

References jsonrpc::endpoint::kJsonRpcVersion.

◆ FromJson()

auto jsonrpc::endpoint::Response::FromJson ( const nlohmann::json & json) -> std::expected<Response, error::RpcError>
static

Definition at line 9 of file response.cpp.

10 {
11 Response r{json};
12 if (auto result = r.ValidateResponse(); !result) {
13 return std::unexpected(result.error());
14 }
15 return r;
16}

◆ GetError()

auto jsonrpc::endpoint::Response::GetError ( ) const -> const nlohmann::json&
nodiscard

Definition at line 70 of file response.cpp.

70 {
71 if (IsSuccess()) {
72 throw std::runtime_error("Response is not an error response");
73 }
74 return response_["error"];
75}
auto IsSuccess() const -> bool
Definition response.cpp:59

References IsSuccess().

Here is the call graph for this function:

◆ GetId()

auto jsonrpc::endpoint::Response::GetId ( ) const -> std::optional<RequestId>
nodiscard

Definition at line 77 of file response.cpp.

77 {
78 if (!response_.contains("id") || response_["id"].is_null()) {
79 return std::nullopt;
80 }
81
82 const auto& id = response_["id"];
83 if (id.is_string()) {
84 return id.get<std::string>();
85 }
86 return id.get<int64_t>();
87}

◆ GetResult()

auto jsonrpc::endpoint::Response::GetResult ( ) const -> const nlohmann::json&
nodiscard

Definition at line 63 of file response.cpp.

63 {
64 if (!IsSuccess()) {
65 throw std::runtime_error("Response is not a success response");
66 }
67 return response_["result"];
68}

References IsSuccess().

Here is the call graph for this function:

◆ IsSuccess()

auto jsonrpc::endpoint::Response::IsSuccess ( ) const -> bool
nodiscard

Definition at line 59 of file response.cpp.

59 {
60 return response_.contains("result");
61}

Referenced by GetError(), and GetResult().

Here is the caller graph for this function:

◆ operator=() [1/2]

auto jsonrpc::endpoint::Response::operator= ( const Response & ) -> Response &=default
default

◆ operator=() [2/2]

auto jsonrpc::endpoint::Response::operator= ( Response && other) -> Response &=default
defaultnoexcept

◆ ToJson()

auto jsonrpc::endpoint::Response::ToJson ( ) const -> nlohmann::json
nodiscard

Definition at line 89 of file response.cpp.

89 {
90 return response_;
91}

Referenced by nlohmann::adl_serializer< jsonrpc::endpoint::Response >::to_json().

Here is the caller graph for this function:

The documentation for this class was generated from the following files: