This repository was archived by the owner on Apr 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGetBalance.cpp
More file actions
72 lines (59 loc) · 1.7 KB
/
GetBalance.cpp
File metadata and controls
72 lines (59 loc) · 1.7 KB
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
67
68
69
70
71
72
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/json.hpp>
#include <xapi/Xapi.hpp>
boost::asio::awaitable<void> printBalance(xapi::XStationClientStream &stream)
{
co_await stream.getBalance();
// Use getKeepAlive, to keep the connection alive
co_await stream.getKeepAlive();
int counter = 0;
while (counter < 5)
{
std::cout << "Processing " << counter << std::endl;
auto result = co_await stream.listen();
std::cout << boost::json::serialize(result) << std::endl;
counter += 1;
}
co_await stream.stopKeepAlive();
co_await stream.stopBalance();
co_return;
}
boost::asio::awaitable<void> run(boost::asio::io_context &context)
{
const boost::json::object accountCredentials = {
{"accountId", "accountId"},
{"password", "password"},
{"accountType", "demo"}
};
xapi::XStationClient user(context, accountCredentials);
try
{
co_await user.login();
auto userStream = user.getClientStream();
co_await userStream.open();
co_await printBalance(userStream);
co_await userStream.close();
co_await user.logout();
}
catch (xapi::exception::ConnectionClosed &e)
{
std::cout << "Connection failed: " << e.what() << std::endl;
}
catch (xapi::exception::LoginFailed &e)
{
std::cout << "Logging failed: " << e.what() << std::endl;
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
}
int main(int argc, char const *argv[])
{
boost::asio::io_context context;
boost::asio::co_spawn(context, run(context), boost::asio::detached);
context.run();
return 0;
}