Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Types.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2023, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "rpc/Errors.hpp"
23#include "web/SubscriptionContextInterface.hpp"
24
25#include <boost/asio/spawn.hpp>
26#include <boost/json/array.hpp>
27#include <boost/json/conversion.hpp>
28#include <boost/json/object.hpp>
29#include <boost/json/value.hpp>
30#include <boost/json/value_from.hpp>
31#include <xrpl/basics/base_uint.h>
32#include <xrpl/basics/strHex.h>
33
34#include <cstdint>
35#include <expected>
36#include <string>
37#include <utility>
38#include <variant>
39
40namespace etl {
41class LoadBalancer;
42} // namespace etl
43namespace web {
44struct ConnectionBase;
45} // namespace web
46
47namespace rpc {
48
49class Counters;
50
55using MaybeError = std::expected<void, Status>;
56
64inline bool
65operator==(MaybeError const& lhs, MaybeError const& rhs)
66{
67 bool const lhsHasError = !static_cast<bool>(lhs);
68 bool const rhsHasError = !static_cast<bool>(rhs);
69 return lhsHasError == rhsHasError && (!lhsHasError || lhs.error() == rhs.error());
70}
71
75using Error = std::unexpected<Status>;
76
80template <typename OutputType>
81using HandlerReturnType = std::expected<OutputType, Status>;
82
86struct ReturnType {
93 ReturnType(std::expected<boost::json::value, Status> result, boost::json::array warnings = {})
94 : result{std::move(result)}, warnings(std::move(warnings))
95 {
96 }
97
101 operator bool() const
102 {
103 return result.operator bool();
104 }
105
106 std::expected<boost::json::value, Status> result;
107 boost::json::array warnings;
108};
109
113struct VoidOutput {};
114
118struct Context {
119 boost::asio::yield_context yield;
120 web::SubscriptionContextPtr session = {}; // NOLINT(readability-redundant-member-init)
121 bool isAdmin = false;
122 std::string clientIp = {}; // NOLINT(readability-redundant-member-init)
123 uint32_t apiVersion = 0u; // invalid by default
124};
125
129struct Result {
135 explicit Result(ReturnType returnType)
136 {
137 if (returnType) {
138 response = std::move(returnType.result).value().as_object();
139 } else {
140 response = std::move(returnType.result).error();
141 }
142 warnings = std::move(returnType.warnings);
143 }
144
150 explicit Result(Status status) : response{std::move(status)}
151 {
152 }
153
159 explicit Result(boost::json::object response) : response{std::move(response)}
160 {
161 }
162
163 std::variant<Status, boost::json::object> response;
164 boost::json::array warnings;
165};
166
171 ripple::uint256 index;
172 std::uint32_t hint{};
173
179 std::string
180 toString() const
181 {
182 return ripple::strHex(index) + "," + std::to_string(hint);
183 }
184
190 bool
191 isNonZero() const
192 {
193 return index.isNonZero() || hint != 0;
194 }
195};
196
204inline void
205tag_invoke(boost::json::value_from_tag, boost::json::value& jv, VoidOutput const&)
206{
207 jv = boost::json::object{};
208}
209
210} // namespace rpc
This namespace contains everything to do with the ETL and ETL sources.
Definition CacheLoader.hpp:36
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:36
std::expected< OutputType, Status > HandlerReturnType
Return type for each individual handler.
Definition Types.hpp:81
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:55
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:241
std::unexpected< Status > Error
The type that represents just the error part of MaybeError.
Definition Types.hpp:75
bool operator==(MaybeError const &lhs, MaybeError const &rhs)
Check if two MaybeError objects are equal.
Definition Types.hpp:65
This namespace implements the web server and related components.
Definition Types.hpp:43
std::shared_ptr< SubscriptionContextInterface > SubscriptionContextPtr
An alias for shared pointer to a SubscriptionContextInterface.
Definition SubscriptionContextInterface.hpp:86
A cursor object used to traverse nodes owned by an account.
Definition Types.hpp:170
std::string toString() const
Convert the cursor to a string.
Definition Types.hpp:180
bool isNonZero() const
Check if the cursor is non-zero.
Definition Types.hpp:191
Context of an RPC call.
Definition Types.hpp:118
Result type used to return responses or error statuses to the Webserver subsystem.
Definition Types.hpp:129
Result(boost::json::object response)
Construct a new Result object from a response object.
Definition Types.hpp:159
Result(ReturnType returnType)
Construct a new Result object from ReturnType.
Definition Types.hpp:135
Result(Status status)
Construct a new Result object from Status.
Definition Types.hpp:150
The final return type out of RPC engine.
Definition Types.hpp:86
ReturnType(std::expected< boost::json::value, Status > result, boost::json::array warnings={})
Construct a new Return Type object.
Definition Types.hpp:93
A status returned from any RPC handler.
Definition Errors.hpp:82
An empty type used as Output for handlers than don't actually produce output.
Definition Types.hpp:113
Base class for all connections.
Definition ConnectionBase.hpp:44