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

Represents a JSON-RPC request. More...

#include <request.hpp>

Collaboration diagram for jsonrpc::client::Request:

Public Member Functions

 Request (std::string method, std::optional< nlohmann::json > params, bool is_notification, const std::function< int()> &id_generator)
 Constructs a new Request object.
 
auto RequiresResponse () const -> bool
 Checks if the request requires a response.
 
auto GetKey () const -> int
 Returns the unique key (ID) for the request.
 
auto Dump () const -> std::string
 Serializes the request to a JSON string.
 

Detailed Description

Represents a JSON-RPC request.

This class handles the creation and management of JSON-RPC requests, including notifications (requests that do not expect a response).

Definition at line 17 of file request.hpp.

Constructor & Destructor Documentation

◆ Request()

jsonrpc::client::Request::Request ( std::string method,
std::optional< nlohmann::json > params,
bool is_notification,
const std::function< int()> & id_generator )

Constructs a new Request object.

Parameters
methodThe name of the method to be invoked.
paramsOptional parameters to be passed with the request.
isNotificationTrue if this is a notification (no response expected).
idGeneratorA function to generate unique request IDs.

Definition at line 5 of file request.cpp.

8 : method_(std::move(method)),
9 params_(std::move(params)),
10 is_notification_(is_notification),
11 id_(0) {
12 if (!is_notification_) {
13 id_ = id_generator();
14 }
15}

Member Function Documentation

◆ Dump()

auto jsonrpc::client::Request::Dump ( ) const -> std::string
nodiscard

Serializes the request to a JSON string.

Definition at line 25 of file request.cpp.

25 {
26 nlohmann::json json_request;
27 json_request["jsonrpc"] = "2.0";
28 json_request["method"] = method_;
29 if (params_) {
30 json_request["params"] = *params_;
31 }
32 if (!is_notification_) {
33 json_request["id"] = id_;
34 }
35 return json_request.dump();
36}

◆ GetKey()

auto jsonrpc::client::Request::GetKey ( ) const -> int
nodiscard

Returns the unique key (ID) for the request.

Definition at line 21 of file request.cpp.

21 {
22 return id_;
23}

◆ RequiresResponse()

auto jsonrpc::client::Request::RequiresResponse ( ) const -> bool
nodiscard

Checks if the request requires a response.

Definition at line 17 of file request.cpp.

17 {
18 return !is_notification_;
19}

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