Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
fc::rpc::http_api_connection Class Reference

#include <http_api.hpp>

Inheritance diagram for fc::rpc::http_api_connection:
Collaboration diagram for fc::rpc::http_api_connection:

Public Member Functions

 http_api_connection ()
 
 ~http_api_connection ()
 
virtual variant send_call (api_id_type api_id, string method_name, variants args=variants()) override
 
virtual variant send_callback (uint64_t callback_id, variants args=variants()) override
 
virtual void send_notice (uint64_t callback_id, variants args=variants()) override
 
void on_request (const fc::http::request &req, const fc::http::server::response &resp)
 
- Public Member Functions inherited from fc::api_connection
 api_connection ()
 
virtual ~api_connection ()
 
template<typename T >
api< Tget_remote_api (api_id_type api_id=0)
 
variant receive_call (api_id_type api_id, const string &method_name, const variants &args=variants()) const
 
variant receive_callback (uint64_t callback_id, const variants &args=variants()) const
 
void receive_notice (uint64_t callback_id, const variants &args=variants()) const
 
template<typename Interface >
api_id_type register_api (const Interface &a)
 
template<typename Signature >
uint64_t register_callback (const std::function< Signature > &cb)
 
std::vector< std::string > get_method_names (api_id_type local_api_id=0) const
 

Public Attributes

fc::rpc::state _rpc_state
 
- Public Attributes inherited from fc::api_connection
fc::signal< void()> closed
 

Detailed Description

Definition at line 11 of file http_api.hpp.

Constructor & Destructor Documentation

◆ http_api_connection()

fc::rpc::http_api_connection::http_api_connection ( )

Definition at line 10 of file http_api.cpp.

11{
12 _rpc_state.add_method( "call", [this]( const variants& args ) -> variant
13 {
14 // TODO: This logic is duplicated between http_api_connection and websocket_api_connection
15 // it should be consolidated into one place instead of copy-pasted
16 FC_ASSERT( args.size() == 3 && args[2].is_array() );
17 api_id_type api_id;
18 if( args[0].is_string() )
19 {
20 variants subargs;
21 subargs.push_back( args[0] );
22 variant subresult = this->receive_call( 1, "get_api_by_name", subargs );
23 api_id = subresult.as_uint64();
24 }
25 else
26 api_id = args[0].as_uint64();
27
28 return this->receive_call(
29 api_id,
30 args[1].as_string(),
31 args[2].get_array() );
32 } );
33
34 _rpc_state.add_method( "notice", [this]( const variants& args ) -> variant
35 {
36 FC_ASSERT( args.size() == 2 && args[1].is_array() );
37 this->receive_notice(
38 args[0].as_uint64(),
39 args[1].get_array() );
40 return variant();
41 } );
42
43 _rpc_state.add_method( "callback", [this]( const variants& args ) -> variant
44 {
45 FC_ASSERT( args.size() == 2 && args[1].is_array() );
46 this->receive_callback(
47 args[0].as_uint64(),
48 args[1].get_array() );
49 return variant();
50 } );
51
52 _rpc_state.on_unhandled( [&]( const std::string& method_name, const variants& args )
53 {
54 return this->receive_call( 0, method_name, args );
55 } );
56}
variant receive_callback(uint64_t callback_id, const variants &args=variants()) const
void receive_notice(uint64_t callback_id, const variants &args=variants()) const
variant receive_call(api_id_type api_id, const string &method_name, const variants &args=variants()) const
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
uint32_t api_id_type
Definition api.hpp:46
std::vector< fc::variant > variants
Definition variant.hpp:173
Here is the call graph for this function:

◆ ~http_api_connection()

fc::rpc::http_api_connection::~http_api_connection ( )

Definition at line 6 of file http_api.cpp.

7{
8}

Member Function Documentation

◆ on_request()

void fc::rpc::http_api_connection::on_request ( const fc::http::request & req,
const fc::http::server::response & resp )

Definition at line 83 of file http_api.cpp.

84{
85 // this must be called by outside HTTP server's on_request method
86 std::string resp_body;
87 http::reply::status_code resp_status;
88
89 auto handle_error = [&](const auto& e)
90 {
91 resp_status = http::reply::InternalServerError;
92 resp_body = "";
93 wdump((e.to_detail_string()));
94 };
95
96 try
97 {
98 resp.add_header( "Content-Type", "application/json" );
99 std::string req_body( req.body.begin(), req.body.end() );
100 auto var = fc::json::from_string( req_body );
101 const auto& var_obj = var.get_object();
102
103 if( var_obj.contains( "method" ) )
104 {
105 auto call = var.as<fc::rpc::request>();
106 auto handle_error_inner = [&](const auto& e)
107 {
108 resp_body = fc::json::to_string( fc::rpc::response( *call.id, error_object{ 1, e.to_detail_string(), fc::variant(e)} ) );
109 resp_status = http::reply::InternalServerError;}
110 };
111
112 try
113 {
114 auto result = _rpc_state.local_call( call.method, call.params );
115 resp_body = fc::json::to_string( fc::rpc::response( *call.id, result ) );
116 resp_status = http::reply::OK;
117 }
118 catch ( const std::bad_alloc& )
119 {
120 throw;
121 }
122 catch ( const boost::interprocess::bad_alloc& )
123 {
124 throw;
125 }
126 catch ( const fc::exception& e )
127 {
128 handle_error_inner(e);
129 }
130 catch ( const std::exception& e )
131 {
133 }
134 }
135 else
136 {
137 resp_status = http::reply::BadRequest;
138 resp_body = "";
139 }
140 }
Used to generate a useful error report when an exception is thrown.
Definition exception.hpp:58
static string to_string(const variant &v, const yield_function_t &yield, const output_formatting format=output_formatting::stringify_large_ints_and_doubles)
Definition json.cpp:674
static variant from_string(const string &utf8_str, const parse_type ptype=parse_type::legacy_parser, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition json.cpp:442
static std_exception_wrapper from_current_exception(const std::exception &e)
T as() const
Definition variant.hpp:327
#define wdump(SEQ)
Definition logger.hpp:160
fc::variant call(const std::string &url, const std::string &path, const T &v)
Definition main.cpp:258
Here is the call graph for this function:

◆ send_call()

variant fc::rpc::http_api_connection::send_call ( api_id_type api_id,
string method_name,
variants args = variants() )
overridevirtual

makes calls to the remote server

Implements fc::api_connection.

Definition at line 58 of file http_api.cpp.

62{
63 // HTTP has no way to do this, so do nothing
64 return variant();
65}

◆ send_callback()

variant fc::rpc::http_api_connection::send_callback ( uint64_t callback_id,
variants args = variants() )
overridevirtual

Implements fc::api_connection.

Definition at line 67 of file http_api.cpp.

70{
71 // HTTP has no way to do this, so do nothing
72 return variant();
73}

◆ send_notice()

void fc::rpc::http_api_connection::send_notice ( uint64_t callback_id,
variants args = variants() )
overridevirtual

Implements fc::api_connection.

Definition at line 75 of file http_api.cpp.

78{
79 // HTTP has no way to do this, so do nothing
80 return;
81}

Member Data Documentation

◆ _rpc_state

fc::rpc::state fc::rpc::http_api_connection::_rpc_state

Definition at line 32 of file http_api.hpp.


The documentation for this class was generated from the following files: