Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Processors.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/common/Concepts.hpp"
23#include "rpc/common/Types.hpp"
24#include "util/UnsupportedType.hpp"
25
26#include <boost/json/value.hpp>
27
28namespace rpc::impl {
29
30template <SomeHandler HandlerType>
31struct DefaultProcessor final {
32 [[nodiscard]] ReturnType
33 operator()(HandlerType const& handler, boost::json::value const& value, Context const& ctx) const
34 {
35 using boost::json::value_from;
36 using boost::json::value_to;
38 // first we run validation against specified API version
39
40 auto const spec = handler.spec(ctx.apiVersion);
41 auto warnings = spec.check(value);
42 auto input = value; // copy here, spec require mutable data
43
44 if (auto const ret = spec.process(input); not ret)
45 return ReturnType{Error{ret.error()}, std::move(warnings)}; // forward Status
46
47 auto const inData = value_to<typename HandlerType::Input>(input);
48 auto ret = handler.process(inData, ctx);
49
50 // real handler is given expected Input, not json
51 if (!ret) {
52 return ReturnType{Error{std::move(ret).error()}, std::move(warnings)}; // forward Status
53 }
54 return ReturnType{value_from(std::move(ret).value()), std::move(warnings)};
55 } else if constexpr (SomeHandlerWithoutInput<HandlerType>) {
56 // no input to pass, ignore the value
57 auto const ret = handler.process(ctx);
58 if (not ret) {
59 return ReturnType{Error{ret.error()}}; // forward Status
60 }
61 return ReturnType{value_from(ret.value())};
62 } else {
63 // when concept SomeHandlerWithInput and SomeHandlerWithoutInput not cover all Handler case
64 static_assert(util::Unsupported<HandlerType>);
65 }
66 }
67};
68
69} // namespace rpc::impl
Specifies what a Handler with Input must provide.
Definition Concepts.hpp:88
Specifies what a Handler without Input must provide.
Definition Concepts.hpp:96
std::unexpected< Status > Error
The type that represents just the error part of MaybeError.
Definition Types.hpp:75
static constexpr bool Unsupported
used for compile time checking of unsupported types
Definition UnsupportedType.hpp:26
Context of an RPC call.
Definition Types.hpp:118
The final return type out of RPC engine.
Definition Types.hpp:86
Definition Processors.hpp:31