Support api_version (#695)

Fixes #64
This commit is contained in:
Alex Kremer
2023-06-16 12:14:30 +01:00
committed by GitHub
parent 871d43c85f
commit a960471ef4
58 changed files with 734 additions and 299 deletions

View File

@@ -18,21 +18,25 @@
//==============================================================================
#include <rpc/Factories.h>
#include <rpc/common/Types.h>
using namespace std;
using namespace clio;
namespace RPC {
optional<Web::Context>
util::Expected<Web::Context, Status>
make_WsContext(
boost::asio::yield_context& yc,
boost::json::object const& request,
shared_ptr<Server::ConnectionBase> const& session,
util::TagDecoratorFactory const& tagFactory,
Backend::LedgerRange const& range,
string const& clientIp)
string const& clientIp,
std::reference_wrapper<APIVersionParser const> apiVersionParser)
{
using Error = util::Unexpected<Status>;
boost::json::value commandValue = nullptr;
if (!request.contains("command") && request.contains("method"))
commandValue = request.at("method");
@@ -40,40 +44,48 @@ make_WsContext(
commandValue = request.at("command");
if (!commandValue.is_string())
return {};
return Error{{RippledError::rpcBAD_SYNTAX, "Method/Command is not specified or is not a string."}};
auto const apiVersion = apiVersionParser.get().parse(request);
if (!apiVersion)
return Error{{ClioError::rpcINVALID_API_VERSION, apiVersion.error()}};
string command = commandValue.as_string().c_str();
return make_optional<Web::Context>(yc, command, 1, request, session, tagFactory, range, clientIp);
return Web::Context(yc, command, *apiVersion, request, session, tagFactory, range, clientIp);
}
optional<Web::Context>
util::Expected<Web::Context, Status>
make_HttpContext(
boost::asio::yield_context& yc,
boost::json::object const& request,
util::TagDecoratorFactory const& tagFactory,
Backend::LedgerRange const& range,
string const& clientIp)
string const& clientIp,
std::reference_wrapper<APIVersionParser const> apiVersionParser)
{
using Error = util::Unexpected<Status>;
if (!request.contains("method") || !request.at("method").is_string())
return {};
return Error{{RippledError::rpcBAD_SYNTAX, "Method is not specified or is not a string."}};
string const& command = request.at("method").as_string().c_str();
if (command == "subscribe" || command == "unsubscribe")
return {};
return Error{{RippledError::rpcBAD_SYNTAX, "Subscribe and unsubscribe are only allowed or websocket."}};
if (!request.at("params").is_array())
return {};
return Error{{RippledError::rpcBAD_SYNTAX, "Missing params array."}};
boost::json::array const& array = request.at("params").as_array();
if (array.size() != 1)
return {};
if (array.size() != 1 || !array.at(0).is_object())
return Error{{RippledError::rpcBAD_SYNTAX, "Params must be an array holding exactly one object."}};
if (!array.at(0).is_object())
return {};
auto const apiVersion = apiVersionParser.get().parse(request.at("params").as_array().at(0).as_object());
if (!apiVersion)
return Error{{ClioError::rpcINVALID_API_VERSION, apiVersion.error()}};
return make_optional<Web::Context>(yc, command, 1, array.at(0).as_object(), nullptr, tagFactory, range, clientIp);
return Web::Context(yc, command, *apiVersion, array.at(0).as_object(), nullptr, tagFactory, range, clientIp);
}
} // namespace RPC