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#include <variant>
20
21namespace etl {
22class LoadBalancer;
23} // namespace etl
24namespace web {
25struct ConnectionBase;
26} // namespace web
27
28namespace rpc {
29
30class Counters;
31
36using MaybeError = std::expected<void, Status>;
37
45inline bool
46operator==(MaybeError const& lhs, MaybeError const& rhs)
47{
48 bool const lhsHasError = !static_cast<bool>(lhs);
49 bool const rhsHasError = !static_cast<bool>(rhs);
50 return lhsHasError == rhsHasError && (!lhsHasError || lhs.error() == rhs.error());
51}
52
56using Error = std::unexpected<Status>;
57
61template <typename OutputType>
62using HandlerReturnType = std::expected<OutputType, Status>;
63
67struct ReturnType {
74 ReturnType(std::expected<boost::json::value, Status> result, boost::json::array warnings = {})
75 : result{std::move(result)}, warnings(std::move(warnings))
76 {
77 }
78
82 operator bool() const
83 {
84 return result.has_value();
85 }
86
87 std::expected<boost::json::value, Status> result;
88 boost::json::array warnings;
89};
90
94struct VoidOutput {};
95
99struct Context {
100 boost::asio::yield_context yield;
101 web::SubscriptionContextPtr session = {}; // NOLINT(readability-redundant-member-init)
102 bool isAdmin = false;
103 std::string clientIp = {}; // NOLINT(readability-redundant-member-init)
104 uint32_t apiVersion = 0u; // invalid by default
105};
106
110struct Result {
116 explicit Result(ReturnType returnType)
117 {
118 if (returnType) {
119 response = std::move(returnType.result).value().as_object();
120 } else {
121 response = std::unexpected{std::move(returnType.result).error()};
122 }
123 warnings = std::move(returnType.warnings);
124 }
125
131 explicit Result(Status status) : response{std::unexpected{std::move(status)}}
132 {
133 }
134
140 explicit Result(boost::json::object response) : response{std::move(response)}
141 {
142 }
143
144 std::expected<boost::json::object, Status> response;
145 boost::json::array warnings;
146};
147
152 ripple::uint256 index;
153 std::uint32_t hint{};
154
160 std::string
161 toString() const
162 {
163 return ripple::strHex(index) + "," + std::to_string(hint);
164 }
165
171 bool
172 isNonZero() const
173 {
174 return index.isNonZero() || hint != 0;
175 }
176};
177
185inline void
186tag_invoke(boost::json::value_from_tag, boost::json::value& jv, VoidOutput const&)
187{
188 jv = boost::json::object{};
189}
190
191} // 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:18
std::expected< OutputType, Status > HandlerReturnType
Return type for each individual handler.
Definition Types.hpp:62
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:36
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:56
bool operator==(MaybeError const &lhs, MaybeError const &rhs)
Check if two MaybeError objects are equal.
Definition Types.hpp:46
This namespace implements the web server and related components.
Definition Types.hpp:24
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:151
std::string toString() const
Convert the cursor to a string.
Definition Types.hpp:161
bool isNonZero() const
Check if the cursor is non-zero.
Definition Types.hpp:172
Context of an RPC call.
Definition Types.hpp:99
Result(boost::json::object response)
Construct a new Result object from a response object.
Definition Types.hpp:140
Result(ReturnType returnType)
Construct a new Result object from ReturnType.
Definition Types.hpp:116
Result(Status status)
Construct a new Result object from Status.
Definition Types.hpp:131
The final return type out of RPC engine.
Definition Types.hpp:67
ReturnType(std::expected< boost::json::value, Status > result, boost::json::array warnings={})
Construct a new Return Type object.
Definition Types.hpp:74
A status returned from any RPC handler.
Definition Errors.hpp:65
An empty type used as Output for handlers than don't actually produce output.
Definition Types.hpp:94
Base class for all connections.
Definition ConnectionBase.hpp:25