From 6438fb8a8bf5ae9493b66ca7f29becab085f9d8d Mon Sep 17 00:00:00 2001 From: Kirill Kirilenko Date: Sat, 1 Oct 2016 00:33:11 +0300 Subject: [PATCH] curlpp replaced by Curl for People. --- .gitmodules | 6 +-- CMakeLists.txt | 30 +++++++++++++- ext/cpr | 1 + ext/curlpp | 1 - src/BotApi.cpp | 100 +++++++++++---------------------------------- src/CMakeLists.txt | 10 ++--- 6 files changed, 61 insertions(+), 87 deletions(-) create mode 160000 ext/cpr delete mode 160000 ext/curlpp diff --git a/.gitmodules b/.gitmodules index 8df21da..c19954b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ -[submodule "ext/curlpp"] - path = ext/curlpp - url = https://github.com/jpbarrette/curlpp.git +[submodule "ext/cpr"] + path = ext/cpr + url = https://github.com/whoshuu/cpr.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 85d2a9d..be3f316 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,11 +4,39 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake) project(telebotxx CXX) +message(STATUS "Checking compiler flags for C++14 support.") +# Set C++14 support flags for various compilers +include(CheckCXXCompilerFlag) +check_cxx_compiler_flag("-std=c++14" COMPILER_SUPPORTS_CXX14) +check_cxx_compiler_flag("-std=c++1y" COMPILER_SUPPORTS_CXX1Y) +if(COMPILER_SUPPORTS_CXX14) + message(STATUS "C++14 is supported.") + if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -stdlib=libc++") + else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") + endif() +elseif(COMPILER_SUPPORTS_CXX1Y) + message(STATUS "C++0x is supported.") + if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y -stdlib=libc++") + else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y") + endif() +else() + message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++14 support. Please use a different C++ compiler.") +endif() + option (TELEBOTXX_BUILD_TESTS "Build unit tests using Boost.Test" ON) option (TELEBOTXX_GENERATE_DOC "Generate API documentation with Doxygen" ON) INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/ext) -INCLUDE_DIRECTORIES(/usr/local/include) + +set(BUILD_CPR_TESTS OFF CACHE BOOL "Do not build cpr tests") +set(USE_SYSTEM_CURL ON CACHE BOOL "Use system libcurl") +set(BUILD_SHARED_LIBS ON) +add_subdirectory(${PROJECT_SOURCE_DIR}/ext/cpr) +include_directories(${CPR_INCLUDE_DIRS}) # Build library include_directories(include) diff --git a/ext/cpr b/ext/cpr new file mode 160000 index 0000000..0422291 --- /dev/null +++ b/ext/cpr @@ -0,0 +1 @@ +Subproject commit 04222915ece1850e54c7381eb55316a32016d93c diff --git a/ext/curlpp b/ext/curlpp deleted file mode 160000 index 9f35688..0000000 --- a/ext/curlpp +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 9f35688e806436067380220c35f2272a5321d224 diff --git a/src/BotApi.cpp b/src/BotApi.cpp index 11cf967..d573d31 100644 --- a/src/BotApi.cpp +++ b/src/BotApi.cpp @@ -5,11 +5,8 @@ #include #include -#include -#include -#include -#include +#include #include @@ -91,27 +88,21 @@ public: inline User getMe() { - curlpp::Easy request; - std::stringstream ss; + auto r = cpr::Get(cpr::Url{telegramMainUrl_ + "/getMe"}); + auto& response = r.text; - request.setOpt(new curlpp::Options::Url(telegramMainUrl_ + "/getMe")); - request.setOpt(new curlpp::Options::Verbose(false)); - request.setOpt(new curlpp::options::WriteStream(&ss)); - request.perform(); - - BOOST_LOG_TRIVIAL(debug) << ss.str(); + BOOST_LOG_TRIVIAL(debug) << response; using namespace rapidjson; - IStreamWrapper isw(ss); Document doc; - doc.ParseStream(isw); + doc.Parse(response.c_str()); return parseUser(parseResponse(doc)); } inline void sendMessage(const std::string& chat, const std::string& text) { - // Construct JSON body and istream + // Construct JSON body using namespace rapidjson; StringBuffer s; Writer writer(s); @@ -123,46 +114,19 @@ public: writer.String(text.c_str()); writer.EndObject(); - std::istringstream requestStream(s.GetString()); - BOOST_LOG_TRIVIAL(debug) << requestStream.str(); - auto size = requestStream.str().size(); + std::string request = s.GetString(); - std::stringstream responseStream; + auto r = cpr::Post(cpr::Url{telegramMainUrl_ + "/sendMessage"}, + cpr::Header{{"Content-Type", "application/json"}}, + cpr::Body{request} + ); + auto& response = r.text; - // Construct HTTP request - curlpp::Easy request; - std::list headers; - - // Content-Type - headers.push_back("Content-Type: application/json"); - - // Content-Length - { - std::ostringstream ss; - ss << "Content-Length: " << size; - headers.push_back(ss.str()); - } - - headers.push_back("Expect:"); - - // Set options - request.setOpt(new curlpp::Options::Url(telegramMainUrl_ + "/sendMessage")); - request.setOpt(new curlpp::Options::Verbose(true)); - request.setOpt(new curlpp::Options::ReadStream(&requestStream)); - request.setOpt(new curlpp::Options::WriteStream(&responseStream)); - request.setOpt(new curlpp::Options::InfileSize(size)); - request.setOpt(new curlpp::Options::HttpHeader(headers)); - request.setOpt(new curlpp::Options::Post(true)); - - // Perform request - request.perform(); - - BOOST_LOG_TRIVIAL(debug) << responseStream.str(); + BOOST_LOG_TRIVIAL(debug) << response; using namespace rapidjson; - IStreamWrapper isw(responseStream); Document doc; - doc.ParseStream(isw); + doc.Parse(response.c_str()); /// \todo Parse message parseResponse(doc); @@ -170,37 +134,19 @@ public: inline void sendPhoto(const std::string& chat, const std::string& filename, const std::string& caption) { - // Construct HTTP request - curlpp::Easy request; - std::stringstream responseStream; + auto r = cpr::Post(cpr::Url{telegramMainUrl_ + "/sendPhoto"}, + cpr::Multipart{{"chat_id", chat}, + {"photo", cpr::File{filename}}, + {"caption", caption} + } + ); + auto& response = r.text; - std::list headers; - headers.push_back("Expect:"); - request.setOpt(new curlpp::Options::HttpHeader(headers)); - - { - // Forms takes ownership of pointers! - curlpp::Forms formParts; - formParts.push_back(new curlpp::FormParts::Content("chat_id", chat)); - formParts.push_back(new curlpp::FormParts::File("photo", filename)); - formParts.push_back(new curlpp::FormParts::Content("caption", caption)); - request.setOpt(new curlpp::options::HttpPost(formParts)); - } - - // Set options - request.setOpt(new curlpp::Options::Url(telegramMainUrl_ + "/sendPhoto")); - request.setOpt(new curlpp::Options::Verbose(true)); - request.setOpt(new curlpp::options::WriteStream(&responseStream)); - - // Perform request - request.perform(); - - BOOST_LOG_TRIVIAL(debug) << responseStream.str(); + BOOST_LOG_TRIVIAL(debug) << response; using namespace rapidjson; - IStreamWrapper isw(responseStream); Document doc; - doc.ParseStream(isw); + doc.Parse(response.c_str()); /// \todo Parse message parseResponse(doc); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7774100..cd05c43 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,15 +5,15 @@ message(STATUS "Configuring telebotxx") find_package(Boost 1.54 REQUIRED log system) include_directories(${Boost_INCLUDE_DIRS}) -find_package(CURLpp REQUIRED) -include_directories(${CURLPP_INCLUDE_DIRS}) +#find_package(CURLpp REQUIRED) +#include_directories(${CURLPP_INCLUDE_DIRS}) # Add required gcc flags # For Windows we suppress all warnings because of Boost garbage :( if(NOT WIN32) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y -Wall") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") else(NOT WIN32) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y -w") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w") endif(NOT WIN32) # Put compiled library to 'lib' directory @@ -25,5 +25,5 @@ set(SOURCE_FILES BotApi.cpp ) add_library(telebotxx SHARED ${SOURCE_FILES}) -target_link_libraries(telebotxx curlpp ${Boost_LIBRARIES} ${CURL_LIBRARIES}) +target_link_libraries(telebotxx ${Boost_LIBRARIES} ${CPR_LIBRARIES}) set_property(TARGET telebotxx APPEND PROPERTY COMPILE_DEFINITIONS BOOST_LOG_DYN_LINK)