-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_client.cpp
executable file
·51 lines (46 loc) · 1.58 KB
/
test_client.cpp
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
#include <iostream>
#include <jsonrpccpp/client.h>
#include <jsonrpccpp/client/connectors/httpclient.h>
class Client : public jsonrpc::Client {
public:
Client(jsonrpc::IClientConnector &conn,
jsonrpc::clientVersion_t type = jsonrpc::JSONRPC_CLIENT_V2)
: jsonrpc::Client(conn, type) {}
int balance(const std::string &address) {
Json::Value p;
p["address"] = address;
Json::Value result = CallMethod("balance", p);
if (result.isIntegral())
return result.asInt();
else
throw jsonrpc::JsonRpcException(
jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE,
result.toStyledString());
}
std::string transaction(int amount, const std::string &from_address,
const std::string &to_address) {
Json::Value p;
p["amount"] = amount;
p["from_address"] = from_address;
p["to_address"] = to_address;
Json::Value result = CallMethod("transaction", p);
if (result.isString())
return result.asString();
else
throw jsonrpc::JsonRpcException(
jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE,
result.toStyledString());
}
};
int main() {
jsonrpc::HttpClient httpclient("http://localhost:10001");
Client c(httpclient, jsonrpc::JSONRPC_CLIENT_V2);
try {
std::cout << "CLIENT | Received by balance: " << c.balance("address")
<< std::endl;
std::cout << "CLIENT | Received by transaction: "
<< c.transaction(1, "from_address", "to_address") << std::endl;
} catch (jsonrpc::JsonRpcException &e) {
std::cerr << e.what() << std::endl;
}
}