Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple http endpoints #1137

Merged
merged 14 commits into from May 15, 2023
4 changes: 2 additions & 2 deletions libraries/chain/include/eosio/chain/types.hpp
Expand Up @@ -370,15 +370,15 @@ namespace eosio::chain {
}

template<typename E, typename F>
static inline auto has_field( F flags, E field )
static constexpr auto has_field( F flags, E field )
-> std::enable_if_t< std::is_integral<F>::value && std::is_unsigned<F>::value &&
std::is_enum<E>::value && std::is_same< F, std::underlying_type_t<E> >::value, bool>
{
return ( (flags & static_cast<F>(field)) != 0 );
}

template<typename E, typename F>
static inline auto set_field( F flags, E field, bool value = true )
static constexpr auto set_field( F flags, E field, bool value = true )
-> std::enable_if_t< std::is_integral<F>::value && std::is_unsigned<F>::value &&
std::is_enum<E>::value && std::is_same< F, std::underlying_type_t<E> >::value, F >
{
Expand Down
18 changes: 10 additions & 8 deletions plugins/chain_api_plugin/chain_api_plugin.cpp
Expand Up @@ -41,8 +41,9 @@ parse_params<chain_apis::read_only::get_transaction_status_params, http_params_t
}
}

#define CALL_WITH_400(api_name, api_handle, api_namespace, call_name, http_response_code, params_type) \
#define CALL_WITH_400(api_name, category, api_handle, api_namespace, call_name, http_response_code, params_type) \
heifner marked this conversation as resolved.
Show resolved Hide resolved
{std::string("/v1/" #api_name "/" #call_name), \
api_category::category,\
[api_handle](string&&, string&& body, url_response_callback&& cb) mutable { \
auto deadline = api_handle.start(); \
try { \
Expand All @@ -55,13 +56,13 @@ parse_params<chain_apis::read_only::get_transaction_status_params, http_params_t
} \
}}

#define CHAIN_RO_CALL(call_name, http_response_code, params_type) CALL_WITH_400(chain, ro_api, chain_apis::read_only, call_name, http_response_code, params_type)
#define CHAIN_RW_CALL(call_name, http_response_code, params_type) CALL_WITH_400(chain, rw_api, chain_apis::read_write, call_name, http_response_code, params_type)
#define CHAIN_RO_CALL_POST(call_name, call_result, http_response_code, params_type) CALL_WITH_400_POST(chain, ro_api, chain_apis::read_only, call_name, call_result, http_response_code, params_type)
#define CHAIN_RO_CALL_ASYNC(call_name, call_result, http_response_code, params_type) CALL_ASYNC_WITH_400(chain, ro_api, chain_apis::read_only, call_name, call_result, http_response_code, params_type)
#define CHAIN_RW_CALL_ASYNC(call_name, call_result, http_response_code, params_type) CALL_ASYNC_WITH_400(chain, rw_api, chain_apis::read_write, call_name, call_result, http_response_code, params_type)
#define CHAIN_RO_CALL(call_name, http_response_code, params_type) CALL_WITH_400(chain, chain_ro, ro_api, chain_apis::read_only, call_name, http_response_code, params_type)
#define CHAIN_RW_CALL(call_name, http_response_code, params_type) CALL_WITH_400(chain, chain_rw, rw_api, chain_apis::read_write, call_name, http_response_code, params_type)
#define CHAIN_RO_CALL_POST(call_name, call_result, http_response_code, params_type) CALL_WITH_400_POST(chain, chain_ro, ro_api, chain_apis::read_only, call_name, call_result, http_response_code, params_type)
#define CHAIN_RO_CALL_ASYNC(call_name, call_result, http_response_code, params_type) CALL_ASYNC_WITH_400(chain, chain_ro, ro_api, chain_apis::read_only, call_name, call_result, http_response_code, params_type)
#define CHAIN_RW_CALL_ASYNC(call_name, call_result, http_response_code, params_type) CALL_ASYNC_WITH_400(chain, chain_rw, rw_api, chain_apis::read_write, call_name, call_result, http_response_code, params_type)

#define CHAIN_RO_CALL_WITH_400(call_name, http_response_code, params_type) CALL_WITH_400(chain, ro_api, chain_apis::read_only, call_name, http_response_code, params_type)
#define CHAIN_RO_CALL_WITH_400(call_name, http_response_code, params_type) CALL_WITH_400(chain, chain_ro, ro_api, chain_apis::read_only, call_name, http_response_code, params_type)

void chain_api_plugin::plugin_startup() {
ilog( "starting chain_api_plugin" );
Expand All @@ -76,7 +77,8 @@ void chain_api_plugin::plugin_startup() {
ro_api.set_shorten_abi_errors( !http_plugin::verbose_errors() );

_http_plugin.add_api( {
CHAIN_RO_CALL(get_info, 200, http_params_types::no_params)}, appbase::exec_queue::read_only, appbase::priority::medium_high);
CALL_WITH_400(chain, node, ro_api, chain_apis::read_only, get_info, 200, http_params_types::no_params)
}, appbase::exec_queue::read_only, appbase::priority::medium_high);
_http_plugin.add_api({
CHAIN_RO_CALL(get_activated_protocol_features, 200, http_params_types::possible_no_params),
CHAIN_RO_CALL_POST(get_block, fc::variant, 200, http_params_types::params_required), // _POST because get_block() returns a lambda to be executed on the http thread pool
Expand Down
1 change: 1 addition & 0 deletions plugins/db_size_api_plugin/db_size_api_plugin.cpp
Expand Up @@ -11,6 +11,7 @@ using namespace eosio;

#define CALL_WITH_400(api_name, api_handle, call_name, INVOKE, http_response_code) \
{std::string("/v1/" #api_name "/" #call_name), \
api_category::db_size, \
[api_handle](string&&, string&& body, url_response_callback&& cb) mutable { \
try { \
body = parse_params<std::string, http_params_types::no_params>(body); \
Expand Down