Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Types.hpp
1#pragma once
2
3#include "rpc/Errors.hpp"
4#include "web/SubscriptionContextInterface.hpp"
5
6#include <boost/asio/spawn.hpp>
7#include <boost/json/array.hpp>
8#include <boost/json/conversion.hpp>
9#include <boost/json/object.hpp>
10#include <boost/json/value.hpp>
11#include <boost/json/value_from.hpp>
12#include <xrpl/basics/base_uint.h>
13#include <xrpl/basics/strHex.h>
14
15#include <cstdint>
16#include <expected>
17#include <string>
18#include <utility>
19
20namespace etl {
21class LoadBalancer;
22} // namespace etl
23namespace web {
24struct ConnectionBase;
25} // namespace web
26
27namespace rpc {
28
29class Counters;
30
35using MaybeError = std::expected<void, Status>;
36
44inline bool
45operator==(MaybeError const& lhs, MaybeError const& rhs)
46{
47 bool const lhsHasError = !static_cast<bool>(lhs);
48 bool const rhsHasError = !static_cast<bool>(rhs);
49 return lhsHasError == rhsHasError && (!lhsHasError || lhs.error() == rhs.error());
50}
51
55using Error = std::unexpected<Status>;
56
60template <typename OutputType>
61using HandlerReturnType = std::expected<OutputType, Status>;
62
66struct ReturnType {
73 ReturnType(std::expected<boost::json::value, Status> result, boost::json::array warnings = {})
74 : result{std::move(result)}, warnings(std::move(warnings))
75 {
76 }
77
81 operator bool() const
82 {
83 return result.has_value();
84 }
85
86 std::expected<boost::json::value, Status> result;
87 boost::json::array warnings;
88};
89
93struct VoidOutput {};
94
98struct Context {
99 boost::asio::yield_context yield;
100 web::SubscriptionContextPtr session = {}; // NOLINT(readability-redundant-member-init)
101 bool isAdmin = false;
102 std::string clientIp = {}; // NOLINT(readability-redundant-member-init)
103 uint32_t apiVersion = 0u; // invalid by default
104};
105
109struct Result {
115 explicit Result(ReturnType returnType)
116 {
117 if (returnType) {
118 response = std::move(returnType.result).value().as_object();
119 } else {
120 response = std::unexpected{std::move(returnType.result).error()};
121 }
122 warnings = std::move(returnType.warnings);
123 }
124
130 explicit Result(Status status) : response{std::unexpected{std::move(status)}}
131 {
132 }
133
139 explicit Result(boost::json::object response) : response{std::move(response)}
140 {
141 }
142
143 std::expected<boost::json::object, Status> response;
144 boost::json::array warnings;
145};
146
151 xrpl::uint256 index;
152 std::uint32_t hint{};
153
159 [[nodiscard]] std::string
160 toString() const
161 {
162 return xrpl::strHex(index) + "," + std::to_string(hint);
163 }
164
170 [[nodiscard]] bool
171 isNonZero() const
172 {
173 return index.isNonZero() || hint != 0;
174 }
175};
176
184inline void
185tag_invoke(boost::json::value_from_tag, boost::json::value& jv, VoidOutput const&)
186{
187 jv = boost::json::object{};
188}
189
190} // namespace rpc
This class is used to manage connections to transaction processing processes.
Definition LoadBalancer.hpp:59
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:17
std::expected< OutputType, Status > HandlerReturnType
Return type for each individual handler.
Definition Types.hpp:61
std::expected< void, Status > MaybeError
Return type used for Validators that can return error but don't have specific value to return.
Definition Types.hpp:35
void tag_invoke(boost::json::value_from_tag, boost::json::value &jv, BookChange const &change)
Implementation of value_from for BookChange type.
Definition BookChangesHelper.hpp:233
std::unexpected< Status > Error
The type that represents just the error part of MaybeError.
Definition Types.hpp:55
bool operator==(MaybeError const &lhs, MaybeError const &rhs)
Check if two MaybeError objects are equal.
Definition Types.hpp:45
This namespace implements the web server and related components.
Definition Types.hpp:23
std::shared_ptr< SubscriptionContextInterface > SubscriptionContextPtr
An alias for shared pointer to a SubscriptionContextInterface.
Definition SubscriptionContextInterface.hpp:64
A cursor object used to traverse nodes owned by an account.
Definition Types.hpp:150
std::string toString() const
Convert the cursor to a string.
Definition Types.hpp:160
bool isNonZero() const
Check if the cursor is non-zero.
Definition Types.hpp:171
Context of an RPC call.
Definition Types.hpp:98
Result(boost::json::object response)
Construct a new Result object from a response object.
Definition Types.hpp:139
Result(ReturnType returnType)
Construct a new Result object from ReturnType.
Definition Types.hpp:115
Result(Status status)
Construct a new Result object from Status.
Definition Types.hpp:130
The final return type out of RPC engine.
Definition Types.hpp:66
ReturnType(std::expected< boost::json::value, Status > result, boost::json::array warnings={})
Construct a new Return Type object.
Definition Types.hpp:73
An empty type used as Output for handlers than don't actually produce output.
Definition Types.hpp:93
Base class for all connections.
Definition ConnectionBase.hpp:25