curlpp replaced by Curl for People.

This commit is contained in:
Kirill Kirilenko 2016-10-01 00:33:11 +03:00
parent 544b2d1512
commit 6438fb8a8b
6 changed files with 61 additions and 87 deletions

6
.gitmodules vendored
View file

@ -1,3 +1,3 @@
[submodule "ext/curlpp"] [submodule "ext/cpr"]
path = ext/curlpp path = ext/cpr
url = https://github.com/jpbarrette/curlpp.git url = https://github.com/whoshuu/cpr.git

View file

@ -4,11 +4,39 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
project(telebotxx CXX) 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_BUILD_TESTS "Build unit tests using Boost.Test" ON)
option (TELEBOTXX_GENERATE_DOC "Generate API documentation with Doxygen" ON) option (TELEBOTXX_GENERATE_DOC "Generate API documentation with Doxygen" ON)
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/ext) 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 # Build library
include_directories(include) include_directories(include)

1
ext/cpr Submodule

@ -0,0 +1 @@
Subproject commit 04222915ece1850e54c7381eb55316a32016d93c

@ -1 +0,0 @@
Subproject commit 9f35688e806436067380220c35f2272a5321d224

View file

@ -5,11 +5,8 @@
#include <rapidjson/document.h> #include <rapidjson/document.h>
#include <rapidjson/writer.h> #include <rapidjson/writer.h>
#include <rapidjson/istreamwrapper.h>
#include <curlpp/cURLpp.hpp> #include <cpr/cpr.h>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#include <boost/log/trivial.hpp> #include <boost/log/trivial.hpp>
@ -91,27 +88,21 @@ public:
inline User getMe() inline User getMe()
{ {
curlpp::Easy request; auto r = cpr::Get(cpr::Url{telegramMainUrl_ + "/getMe"});
std::stringstream ss; auto& response = r.text;
request.setOpt(new curlpp::Options::Url(telegramMainUrl_ + "/getMe")); BOOST_LOG_TRIVIAL(debug) << response;
request.setOpt(new curlpp::Options::Verbose(false));
request.setOpt(new curlpp::options::WriteStream(&ss));
request.perform();
BOOST_LOG_TRIVIAL(debug) << ss.str();
using namespace rapidjson; using namespace rapidjson;
IStreamWrapper isw(ss);
Document doc; Document doc;
doc.ParseStream(isw); doc.Parse(response.c_str());
return parseUser(parseResponse(doc)); return parseUser(parseResponse(doc));
} }
inline void sendMessage(const std::string& chat, const std::string& text) inline void sendMessage(const std::string& chat, const std::string& text)
{ {
// Construct JSON body and istream // Construct JSON body
using namespace rapidjson; using namespace rapidjson;
StringBuffer s; StringBuffer s;
Writer<StringBuffer> writer(s); Writer<StringBuffer> writer(s);
@ -123,46 +114,19 @@ public:
writer.String(text.c_str()); writer.String(text.c_str());
writer.EndObject(); writer.EndObject();
std::istringstream requestStream(s.GetString()); std::string request = s.GetString();
BOOST_LOG_TRIVIAL(debug) << requestStream.str();
auto size = requestStream.str().size();
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 BOOST_LOG_TRIVIAL(debug) << response;
curlpp::Easy request;
std::list<std::string> 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();
using namespace rapidjson; using namespace rapidjson;
IStreamWrapper isw(responseStream);
Document doc; Document doc;
doc.ParseStream(isw); doc.Parse(response.c_str());
/// \todo Parse message /// \todo Parse message
parseResponse(doc); parseResponse(doc);
@ -170,37 +134,19 @@ public:
inline void sendPhoto(const std::string& chat, const std::string& filename, const std::string& caption) inline void sendPhoto(const std::string& chat, const std::string& filename, const std::string& caption)
{ {
// Construct HTTP request auto r = cpr::Post(cpr::Url{telegramMainUrl_ + "/sendPhoto"},
curlpp::Easy request; cpr::Multipart{{"chat_id", chat},
std::stringstream responseStream; {"photo", cpr::File{filename}},
{"caption", caption}
std::list<std::string> 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));
} }
);
auto& response = r.text;
// Set options BOOST_LOG_TRIVIAL(debug) << response;
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();
using namespace rapidjson; using namespace rapidjson;
IStreamWrapper isw(responseStream);
Document doc; Document doc;
doc.ParseStream(isw); doc.Parse(response.c_str());
/// \todo Parse message /// \todo Parse message
parseResponse(doc); parseResponse(doc);

View file

@ -5,15 +5,15 @@ message(STATUS "Configuring telebotxx")
find_package(Boost 1.54 REQUIRED log system) find_package(Boost 1.54 REQUIRED log system)
include_directories(${Boost_INCLUDE_DIRS}) include_directories(${Boost_INCLUDE_DIRS})
find_package(CURLpp REQUIRED) #find_package(CURLpp REQUIRED)
include_directories(${CURLPP_INCLUDE_DIRS}) #include_directories(${CURLPP_INCLUDE_DIRS})
# Add required gcc flags # Add required gcc flags
# For Windows we suppress all warnings because of Boost garbage :( # For Windows we suppress all warnings because of Boost garbage :(
if(NOT WIN32) 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) 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) endif(NOT WIN32)
# Put compiled library to 'lib' directory # Put compiled library to 'lib' directory
@ -25,5 +25,5 @@ set(SOURCE_FILES BotApi.cpp
) )
add_library(telebotxx SHARED ${SOURCE_FILES}) 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) set_property(TARGET telebotxx APPEND PROPERTY COMPILE_DEFINITIONS BOOST_LOG_DYN_LINK)