-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_handler_interface.hpp
66 lines (61 loc) · 1.88 KB
/
rpc_handler_interface.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#pragma once
#include <functional>
#include <memory>
#include <sstream>
#include <string>
namespace vxldollar
{
class rpc;
/** Keeps information about http requests, and for v2+ includes path and header values of interest */
class rpc_handler_request_params final
{
public:
int rpc_version{ 1 };
std::string path;
std::string credentials;
std::string correlation_id;
/**
* If the path is non-empty, this wraps the body inside an IPC API compliant envelope.
* Otherwise the input string is returned unchanged.
* This allows HTTP clients to use a simplified request format by omitting the envelope.
* Envelope fields may still be specified through corresponding vxldollar- header fields.
*/
std::string json_envelope (std::string const & body_a) const
{
std::string body_l;
if (!path.empty ())
{
std::ostringstream json;
json << "{";
if (!credentials.empty ())
{
json << "\"credentials\": \"" << credentials << "\", ";
}
if (!correlation_id.empty ())
{
json << "\"correlation_id\": \"" << correlation_id << "\", ";
}
json << "\"message_type\": \"" << path << "\", ";
json << "\"message\": " << body_a;
json << "}";
body_l = json.str ();
}
else
{
body_l = body_a;
}
return body_l;
}
};
class rpc_handler_interface
{
public:
virtual ~rpc_handler_interface () = default;
/** Process RPC 1.0 request. */
virtual void process_request (std::string const & action, std::string const & body, std::function<void (std::string const &)> response) = 0;
/** Process RPC 2.0 request. This is called via the IPC API */
virtual void process_request_v2 (rpc_handler_request_params const & params_a, std::string const & body, std::function<void (std::shared_ptr<std::string> const &)> response) = 0;
virtual void stop () = 0;
virtual void rpc_instance (vxldollar::rpc & rpc) = 0;
};
}