From d285f5303b916b00211f3e77981190e80638fb0b Mon Sep 17 00:00:00 2001 From: wwu Date: Wed, 26 Mar 2014 16:36:58 -0400 Subject: [PATCH 1/5] use POCO http client, add makefile and license --- LICENSE.md | 9 +++++ Makefile | 16 ++++++++ README.md | 14 +++---- streaming.cpp | 102 ++++++++++++++++++++++++++++---------------------- 4 files changed, 88 insertions(+), 53 deletions(-) create mode 100644 LICENSE.md create mode 100644 Makefile diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..59275a7 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,9 @@ +The MIT License (MIT) + +Copyright (c) 2014 OANDA Corporation + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9a4a2b1 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +CXX = g++ +CXXFLAGS = -Wall -MMD -lPocoNetSSL + +EXEC = streaming +OBJECTS = streaming.o +DEPENDS = ${OBJECTS:.o=.d} + +${EXEC}: ${OBJECTS} + ${CXX} ${OBJECTS} ${CXXFLAGS} -o ${EXEC} + +-include ${DEPENDS} + +.PHONY : clean + +clean : + rm ${OBJECTS} ${EXEC} ${DEPENDS} diff --git a/README.md b/README.md index 7c1f0ed..408144d 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,23 @@ cpp-api-streaming ================= -A demo app in C++ for streaming rates using OANDA open api and libcurl +A demo app in C++ for streaming rates using OANDA REST api and the [POCO C++ library](http://pocoproject.org/download/index.html). +A JSON parser such as [libjson](http://sourceforge.net/projects/libjson/) can be used decode the JSON response. ### Setup Clone this repo to the location of your choice -Make sure you have libcurl installed - - apt-get install libcurl4-gnutls-dev - Update the following information in main() in streaming.cpp: - domain accountId access_token (Authorization) -Compile the file through g++ compiler. Link libcurl using the -lcurl flag. +Compile the file through g++ compiler. Link the poco networking component using the -lPocoNetSSL flag. + +`g++ streaming.cpp -lPocoNetSSL -o streaming` - g++ streaming.cpp -lcurl -o streaming +Alternatively, use the provided Makefile by running `make`. Run the output executable file diff --git a/streaming.cpp b/streaming.cpp index 38f7352..c782323 100644 --- a/streaming.cpp +++ b/streaming.cpp @@ -1,59 +1,71 @@ -/* - This is a demo app in C++ for streaming feature using OANDA open api and libcurl -*/ - -#include #include +#include +#include +#include -int main() -{ - CURL *connection; - CURLcode code; - - connection = curl_easy_init(); - if (connection) - { - // Please update the following parameters with proper value - // [:domain] : use either fxpractice.oanda.com or fxtrade.oanda.com - // [:instruments] : feel free to subscribe more instruments (at most 10 instruments) - // [:accountId] : your account ID - code = curl_easy_setopt(connection, CURLOPT_URL, "https:///v1/quote?instruments=EUR_USD&accountId="); - - if (code != CURLE_OK) - { - std::cout << "Failed to set URL" << std::endl; - } - - struct curl_slist *headers = NULL; +#include +#include +#include +#include +#include +#include +#include +#include - // Headers - // [:access_token] : "Authorization: Bearer " - headers = curl_slist_append(headers, "Authorization: Bearer "); +using namespace Poco; +using namespace Poco::Net; +using namespace std; - code = curl_easy_setopt(connection, CURLOPT_HTTPHEADER, headers); +void handleStream(streambuf* stream_buffer) +{ + std::istreambuf_iterator eos; // end-of-range iterator + std::istreambuf_iterator iit (stream_buffer); // stream iterator + string str; - if (code != CURLE_OK) - { - std::cout << "Failed to set headers" << std::endl; + while (iit!=eos) { + ostringstream oss; + while (*iit != '\n') { + oss << *iit++; } + + //print the tick + cout << oss.str() << endl; + + *iit++; + } +} - code = curl_easy_setopt(connection, CURLOPT_SSL_VERIFYPEER, 0); +int main () +{ + try { + const Context::Ptr context = new Context(Context::CLIENT_USE, "", "", "", Context::VERIFY_NONE, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); - if (code != CURLE_OK) - { - std::cout << "Failed to disable 'verify the peer'" << std::endl; - } + // prepare session + URI uri("https://stream-fxpractice.oanda.com/v1/quote?accountId=&instruments=EUR_USD"); + + HTTPSClientSession session(uri.getHost(), uri.getPort(), context); + session.setKeepAlive(true); - code = curl_easy_perform(connection); + // prepare path + string path(uri.getPathAndQuery()); + if (path.empty()) path = "/"; - if (code != CURLE_OK) - { - std::cout << "Failed to perform connection" << std::endl; - } + // send request + HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1); + req.set("Authorization", "Bearer "); + session.sendRequest(req); - curl_easy_cleanup(connection); + // get response + HTTPResponse res; + istream& rs = session.receiveResponse(res); - curl_slist_free_all(headers); + // handle response + ostringstream out_string_stream; + handleStream(rs.rdbuf()); + } + catch (const Exception &e) + { + cerr << e.displayText() << endl; } - return 0; } + From d7c203ef44714e7c9a7ca9eabc25976a50db2408 Mon Sep 17 00:00:00 2001 From: wwu Date: Wed, 26 Mar 2014 16:36:58 -0400 Subject: [PATCH 2/5] use POCO http client, add makefile and license --- LICENCE.md | 9 +++++ LICENSE.md | 9 +++++ Makefile | 16 ++++++++ README.md | 14 +++---- streaming.cpp | 102 ++++++++++++++++++++++++++++---------------------- 5 files changed, 97 insertions(+), 53 deletions(-) create mode 100644 LICENCE.md create mode 100644 LICENSE.md create mode 100644 Makefile diff --git a/LICENCE.md b/LICENCE.md new file mode 100644 index 0000000..59275a7 --- /dev/null +++ b/LICENCE.md @@ -0,0 +1,9 @@ +The MIT License (MIT) + +Copyright (c) 2014 OANDA Corporation + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..59275a7 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,9 @@ +The MIT License (MIT) + +Copyright (c) 2014 OANDA Corporation + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9a4a2b1 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +CXX = g++ +CXXFLAGS = -Wall -MMD -lPocoNetSSL + +EXEC = streaming +OBJECTS = streaming.o +DEPENDS = ${OBJECTS:.o=.d} + +${EXEC}: ${OBJECTS} + ${CXX} ${OBJECTS} ${CXXFLAGS} -o ${EXEC} + +-include ${DEPENDS} + +.PHONY : clean + +clean : + rm ${OBJECTS} ${EXEC} ${DEPENDS} diff --git a/README.md b/README.md index 7c1f0ed..408144d 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,23 @@ cpp-api-streaming ================= -A demo app in C++ for streaming rates using OANDA open api and libcurl +A demo app in C++ for streaming rates using OANDA REST api and the [POCO C++ library](http://pocoproject.org/download/index.html). +A JSON parser such as [libjson](http://sourceforge.net/projects/libjson/) can be used decode the JSON response. ### Setup Clone this repo to the location of your choice -Make sure you have libcurl installed - - apt-get install libcurl4-gnutls-dev - Update the following information in main() in streaming.cpp: - domain accountId access_token (Authorization) -Compile the file through g++ compiler. Link libcurl using the -lcurl flag. +Compile the file through g++ compiler. Link the poco networking component using the -lPocoNetSSL flag. + +`g++ streaming.cpp -lPocoNetSSL -o streaming` - g++ streaming.cpp -lcurl -o streaming +Alternatively, use the provided Makefile by running `make`. Run the output executable file diff --git a/streaming.cpp b/streaming.cpp index 38f7352..c782323 100644 --- a/streaming.cpp +++ b/streaming.cpp @@ -1,59 +1,71 @@ -/* - This is a demo app in C++ for streaming feature using OANDA open api and libcurl -*/ - -#include #include +#include +#include +#include -int main() -{ - CURL *connection; - CURLcode code; - - connection = curl_easy_init(); - if (connection) - { - // Please update the following parameters with proper value - // [:domain] : use either fxpractice.oanda.com or fxtrade.oanda.com - // [:instruments] : feel free to subscribe more instruments (at most 10 instruments) - // [:accountId] : your account ID - code = curl_easy_setopt(connection, CURLOPT_URL, "https:///v1/quote?instruments=EUR_USD&accountId="); - - if (code != CURLE_OK) - { - std::cout << "Failed to set URL" << std::endl; - } - - struct curl_slist *headers = NULL; +#include +#include +#include +#include +#include +#include +#include +#include - // Headers - // [:access_token] : "Authorization: Bearer " - headers = curl_slist_append(headers, "Authorization: Bearer "); +using namespace Poco; +using namespace Poco::Net; +using namespace std; - code = curl_easy_setopt(connection, CURLOPT_HTTPHEADER, headers); +void handleStream(streambuf* stream_buffer) +{ + std::istreambuf_iterator eos; // end-of-range iterator + std::istreambuf_iterator iit (stream_buffer); // stream iterator + string str; - if (code != CURLE_OK) - { - std::cout << "Failed to set headers" << std::endl; + while (iit!=eos) { + ostringstream oss; + while (*iit != '\n') { + oss << *iit++; } + + //print the tick + cout << oss.str() << endl; + + *iit++; + } +} - code = curl_easy_setopt(connection, CURLOPT_SSL_VERIFYPEER, 0); +int main () +{ + try { + const Context::Ptr context = new Context(Context::CLIENT_USE, "", "", "", Context::VERIFY_NONE, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); - if (code != CURLE_OK) - { - std::cout << "Failed to disable 'verify the peer'" << std::endl; - } + // prepare session + URI uri("https://stream-fxpractice.oanda.com/v1/quote?accountId=&instruments=EUR_USD"); + + HTTPSClientSession session(uri.getHost(), uri.getPort(), context); + session.setKeepAlive(true); - code = curl_easy_perform(connection); + // prepare path + string path(uri.getPathAndQuery()); + if (path.empty()) path = "/"; - if (code != CURLE_OK) - { - std::cout << "Failed to perform connection" << std::endl; - } + // send request + HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1); + req.set("Authorization", "Bearer "); + session.sendRequest(req); - curl_easy_cleanup(connection); + // get response + HTTPResponse res; + istream& rs = session.receiveResponse(res); - curl_slist_free_all(headers); + // handle response + ostringstream out_string_stream; + handleStream(rs.rdbuf()); + } + catch (const Exception &e) + { + cerr << e.displayText() << endl; } - return 0; } + From 1f420a783e8546b84f3bfabc5b7bc45910ae7df4 Mon Sep 17 00:00:00 2001 From: wwu Date: Thu, 27 Mar 2014 11:10:00 -0400 Subject: [PATCH 3/5] remove duplicate licence --- LICENSE.md | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 LICENSE.md diff --git a/LICENSE.md b/LICENSE.md deleted file mode 100644 index 59275a7..0000000 --- a/LICENSE.md +++ /dev/null @@ -1,9 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2014 OANDA Corporation - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file From fa3eef0e3d4bece51374ef459e3bab0f8c248b9f Mon Sep 17 00:00:00 2001 From: Christian Reich Date: Thu, 15 May 2014 18:52:15 +0200 Subject: [PATCH 4/5] Update streaming.cpp fixed sample url. "/quote" is not supported (anymore?) --- streaming.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/streaming.cpp b/streaming.cpp index c782323..f87a551 100644 --- a/streaming.cpp +++ b/streaming.cpp @@ -41,7 +41,7 @@ int main () const Context::Ptr context = new Context(Context::CLIENT_USE, "", "", "", Context::VERIFY_NONE, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); // prepare session - URI uri("https://stream-fxpractice.oanda.com/v1/quote?accountId=&instruments=EUR_USD"); + URI uri("https://stream-fxpractice.oanda.com/v1/prices?accountId=&instruments=EUR_USD"); HTTPSClientSession session(uri.getHost(), uri.getPort(), context); session.setKeepAlive(true); From b307eb2c58a23c172fa30111242f78436cf0c663 Mon Sep 17 00:00:00 2001 From: John Munnings Date: Fri, 4 Jul 2014 10:30:08 -0400 Subject: [PATCH 5/5] Pulled out variables, added libraries to compile line Added tick wrapping to README --- Makefile | 4 ++-- README.md | 10 +++++----- streaming.cpp | 19 +++++++++++++++++-- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 9a4a2b1..1ba3688 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CXX = g++ -CXXFLAGS = -Wall -MMD -lPocoNetSSL +CXXFLAGS = -Wall -MMD -lPocoNet -lPocoFoundation -lPocoNetSSL EXEC = streaming OBJECTS = streaming.o @@ -13,4 +13,4 @@ ${EXEC}: ${OBJECTS} .PHONY : clean clean : - rm ${OBJECTS} ${EXEC} ${DEPENDS} + rm -rf ${OBJECTS} ${EXEC} ${DEPENDS} diff --git a/README.md b/README.md index 408144d..0fabcc0 100644 --- a/README.md +++ b/README.md @@ -13,9 +13,9 @@ Update the following information in main() in streaming.cpp: accountId access_token (Authorization) -Compile the file through g++ compiler. Link the poco networking component using the -lPocoNetSSL flag. +Compile the file through g++ compiler. Link the poco networking component using the following -l flags. -`g++ streaming.cpp -lPocoNetSSL -o streaming` +`g++ streaming.cpp -lPocoNet -lPocoFoundation -lPocoNetSSL -o streaming` Alternatively, use the provided Makefile by running `make`. @@ -25,10 +25,10 @@ Run the output executable file ### Sameple Output - {"instrument":"EUR_USD","time":"2014-03-07T20:52:17.453618Z","bid":1.3867,"ask":1.38682} + {"tick":{"instrument":"EUR_USD","time":"2014-03-07T20:52:17.453618Z","bid":1.3867,"ask":1.38682}} {"heartbeat":{"time":"2014-03-07T20:52:29.452784Z"}} - {"instrument":"EUR_USD","time":"2014-03-07T20:52:31.430114Z","bid":1.38671,"ask":1.38685} - {"instrument":"EUR_USD","time":"2014-03-07T20:52:31.478110Z","bid":1.38673,"ask":1.38687} + {"tick":{"instrument":"EUR_USD","time":"2014-03-07T20:52:31.430114Z","bid":1.38671,"ask":1.38685}} + {"tick":{"instrument":"EUR_USD","time":"2014-03-07T20:52:31.478110Z","bid":1.38673,"ask":1.38687}} ### More Information diff --git a/streaming.cpp b/streaming.cpp index f87a551..e021fb3 100644 --- a/streaming.cpp +++ b/streaming.cpp @@ -16,6 +16,21 @@ 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 eos; // end-of-range iterator @@ -41,7 +56,7 @@ int main () const Context::Ptr context = new Context(Context::CLIENT_USE, "", "", "", Context::VERIFY_NONE, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); // prepare session - URI uri("https://stream-fxpractice.oanda.com/v1/prices?accountId=&instruments=EUR_USD"); + URI uri(domain + std::string("/v1/prices?accountId=") + account_id + std::string("&instruments=") + instruments); HTTPSClientSession session(uri.getHost(), uri.getPort(), context); session.setKeepAlive(true); @@ -52,7 +67,7 @@ int main () // send request HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1); - req.set("Authorization", "Bearer "); + req.set("Authorization", std::string("Bearer ") + access_token); session.sendRequest(req); // get response