forked from RayCui/cpp-api-streaming
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathstreaming.cpp
More file actions
86 lines (68 loc) · 2.33 KB
/
streaming.cpp
File metadata and controls
86 lines (68 loc) · 2.33 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
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <iterator>
#include <string>
#include <sstream>
#include <Poco/Net/HTTPSClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Net/SSLManager.h>
#include <Poco/StreamCopier.h>
#include <Poco/Path.h>
#include <Poco/URI.h>
#include <Poco/Exception.h>
using namespace Poco;
using namespace Poco::Net;
using namespace std;
// Edit these global variables with your own information
std::string account_id = "1234567";
std::string access_token = "ACCESS-TOKEN";
std::string instruments = "EUR_USD,USD_CAD,EUR_JPY";
std::string domain = "https://stream-fxpractice.oanda.com";
/*****************************
The domain variable should be:
For Sandbox -> https://stream-sandbox.oanda.com
For fxPractice -> https://stream-fxpractice.oanda.com
For fxTrade -> https://stream-fxtrade.oanda.com
******************************/
void handleStream(streambuf* stream_buffer)
{
std::istreambuf_iterator<char> eos; // end-of-range iterator
std::istreambuf_iterator<char> iit (stream_buffer); // stream iterator
string str;
while (iit!=eos) {
ostringstream oss;
while (*iit != '\n') {
oss << *iit++;
}
//print the tick
cout << oss.str() << endl;
*iit++;
}
}
int main ()
{
try {
const Context::Ptr context = new Context(Context::CLIENT_USE, "", "", "", Context::VERIFY_NONE, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
// prepare session
URI uri(domain + std::string("/v1/prices?accountId=") + account_id + std::string("&instruments=") + instruments);
HTTPSClientSession session(uri.getHost(), uri.getPort(), context);
session.setKeepAlive(true);
// prepare path
string path(uri.getPathAndQuery());
if (path.empty()) path = "/";
// send request
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
req.set("Authorization", std::string("Bearer ") + access_token);
session.sendRequest(req);
// get response
HTTPResponse res;
istream& rs = session.receiveResponse(res);
// handle response
ostringstream out_string_stream;
handleStream(rs.rdbuf());
}
catch (const Exception &e)
{
cerr << e.displayText() << endl;
}
}