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()(
34 HandlerType const& handler,
35 boost::json::value const& value,
36 Context const& ctx
37 ) const
38 {
39 using boost::json::value_from;
40 using boost::json::value_to;
42 // first we run validation against specified API version
43
44 auto const spec = handler.spec(ctx.apiVersion);
45 auto warnings = spec.check(value);
46 auto input = value; // copy here, spec require mutable data
47
48 if (auto const ret = spec.process(input); not ret)
49 return ReturnType{Error{ret.error()}, std::move(warnings)}; // forward Status
50
51 auto const inData = value_to<typename HandlerType::Input>(input);
52 auto ret = handler.process(inData, ctx);
53
54 // real handler is given expected Input, not json
55 if (!ret) {
56 return ReturnType{
57 Error{std::move(ret).error()}, std::move(warnings)
58 }; // forward Status
59 }
60 return ReturnType{value_from(std::move(ret).value()), std::move(warnings)};
61 } else if constexpr (SomeHandlerWithoutInput<HandlerType>) {
62 // no input to pass, ignore the value
63 auto const ret = handler.process(ctx);
64 if (not ret) {
65 return ReturnType{Error{ret.error()}}; // forward Status
66 }
67 return ReturnType{value_from(ret.value())};
68 } else {
69 // when concept SomeHandlerWithInput and SomeHandlerWithoutInput not cover all Handler
70 // case
71 static_assert(util::Unsupported<HandlerType>);
72 }
73 }
74};
75
76} // namespace rpc::impl
Specifies what a Handler with Input must provide.
Definition Concepts.hpp:89
Specifies what a Handler without Input must provide.
Definition Concepts.hpp:97
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