Represents a JSON-RPC response.
More...
#include <response.hpp>
|
static auto | FromUserResponse (const nlohmann::json &response_json, std::optional< nlohmann::json > id) -> Response |
| Creates a Response object from a JSON object that represents a user response.
|
|
static auto | CreateResult (const nlohmann::json &result, const std::optional< nlohmann::json > &id) -> Response |
| Creates a successful Response object.
|
|
static auto | CreateLibError (LibErrorKind error_kind, const std::optional< nlohmann::json > &id=std::nullopt) -> Response |
| Creates a Response object for a library error.
|
|
static auto | CreateUserError (const nlohmann::json &error, const std::optional< nlohmann::json > &id) -> Response |
| Creates a Response object for a user error.
|
|
Represents a JSON-RPC response.
Definition at line 24 of file response.hpp.
◆ Response() [1/3]
jsonrpc::server::Response::Response |
( |
| ) |
|
|
delete |
◆ Response() [2/3]
jsonrpc::server::Response::Response |
( |
const Response & | | ) |
|
|
delete |
◆ Response() [3/3]
jsonrpc::server::Response::Response |
( |
Response && | other | ) |
|
|
noexcept |
Definition at line 14 of file response.cpp.
15 : response_(std::move(other.response_)) {
16}
◆ ~Response()
jsonrpc::server::Response::~Response |
( |
| ) |
|
|
default |
◆ CreateLibError()
auto jsonrpc::server::Response::CreateLibError |
( |
LibErrorKind | error_kind, |
|
|
const std::optional< nlohmann::json > & | id = std::nullopt ) -> Response |
|
static |
Creates a Response object for a library error.
- Parameters
-
errorKind | The kind of library error. |
id | The ID of the request. It can be a JSON object or null. |
- Returns
- A Response object indicating a library error.
Definition at line 67 of file response.cpp.
69 {
70 const auto &[code, message] = kErrorInfoMap.at(error_kind);
71 nlohmann::json error_response = CreateErrorResponse(message, code, id);
72 return Response{std::move(error_response)};
73}
Referenced by jsonrpc::server::Dispatcher::DispatchRequest().
◆ CreateResult()
auto jsonrpc::server::Response::CreateResult |
( |
const nlohmann::json & | result, |
|
|
const std::optional< nlohmann::json > & | id ) -> Response |
|
static |
Creates a successful Response object.
- Parameters
-
result | The result of the method call. |
id | The ID of the request. It can be a JSON object or null. |
- Returns
- A Response object indicating success.
Definition at line 39 of file response.cpp.
41 {
42 nlohmann::json response = {{"jsonrpc", "2.0"}, {"result", result}};
43 if (id.has_value()) {
44 response["id"] = id.value();
45 }
46 return Response{std::move(response)};
47}
◆ CreateUserError()
auto jsonrpc::server::Response::CreateUserError |
( |
const nlohmann::json & | error, |
|
|
const std::optional< nlohmann::json > & | id ) -> Response |
|
static |
Creates a Response object for a user error.
- Parameters
-
error | The JSON object representing the error. |
id | The ID of the request. It can be a JSON object or null. |
- Returns
- A Response object indicating a user error.
Definition at line 49 of file response.cpp.
51 {
52 nlohmann::json response = {{"jsonrpc", "2.0"}, {"error", error}};
53 if (id.has_value()) {
54 response["id"] = id.value();
55 }
56 return Response{std::move(response)};
57}
◆ FromUserResponse()
auto jsonrpc::server::Response::FromUserResponse |
( |
const nlohmann::json & | response_json, |
|
|
std::optional< nlohmann::json > | id ) -> Response |
|
static |
Creates a Response object from a JSON object that represents a user response.
- Parameters
-
responseJson | The JSON object representing the user response. |
id | The ID of the request. It can be a JSON object or null. |
- Returns
- A Response object.
Definition at line 26 of file response.cpp.
28 {
29 if (response_json.contains("result")) {
31 }
32 if (response_json.contains("error")) {
34 }
35 throw std::invalid_argument(
36 "Response JSON must contain either 'result' or 'error' field");
37}
static auto CreateUserError(const nlohmann::json &error, const std::optional< nlohmann::json > &id) -> Response
Creates a Response object for a user error.
static auto CreateResult(const nlohmann::json &result, const std::optional< nlohmann::json > &id) -> Response
Creates a successful Response object.
◆ operator=() [1/2]
auto jsonrpc::server::Response::operator= |
( |
const Response & | | ) |
-> Response &=delete |
|
delete |
◆ operator=() [2/2]
auto jsonrpc::server::Response::operator= |
( |
Response && | other | ) |
-> Response &=delete |
|
deletenoexcept |
◆ ToJson()
auto jsonrpc::server::Response::ToJson |
( |
| ) |
const -> nlohmann::json |
|
nodiscard |
Serializes the Response object to a JSON object.
- Returns
- The JSON representation of the response.
Definition at line 59 of file response.cpp.
59 {
60 return response_;
61}
◆ ToStr()
auto jsonrpc::server::Response::ToStr |
( |
| ) |
const -> std::string |
|
nodiscard |
Serializes the Response object to a string.
- Returns
- The string representation of the response.
Definition at line 63 of file response.cpp.
63 {
64 return response_.dump();
65}
The documentation for this class was generated from the following files: