summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorEgor Pugin <egor.pugin@gmail.com>2018-07-04 19:11:03 +0300
committerEgor Pugin <egor.pugin@gmail.com>2018-07-04 19:11:03 +0300
commit95f86f4ce18e65d45894d5a130c4bdaf8ecbaac5 (patch)
tree0235eaef9d2dcd9725c1eb9d24c8de0201ba6fcc /include
parent7f388398bbcff916f5507770af727ef9ad59f33d (diff)
Implement CurlHttpClient.
Diffstat (limited to 'include')
-rw-r--r--include/tgbot/Api.h4
-rw-r--r--include/tgbot/Bot.h5
-rw-r--r--include/tgbot/net/HttpClient.h64
3 files changed, 68 insertions, 5 deletions
diff --git a/include/tgbot/Api.h b/include/tgbot/Api.h
index 8dea56a..e73fbe1 100644
--- a/include/tgbot/Api.h
+++ b/include/tgbot/Api.h
@@ -28,6 +28,7 @@
#include <boost/property_tree/ptree.hpp>
+#include "tgbot/net/HttpClient.h"
#include "tgbot/net/HttpReqArg.h"
#include "tgbot/types/User.h"
#include "tgbot/types/Message.h"
@@ -62,7 +63,7 @@ typedef std::shared_ptr<std::vector<std::string>> StringArrayPtr;
friend class Bot;
public:
- Api(const std::string& token);
+ Api(const std::string& token, const HttpClient &httpClientDriver);
/**
* @brief A simple method for testing your bot's auth token.
@@ -890,6 +891,7 @@ private:
boost::property_tree::ptree sendRequest(const std::string& method, const std::vector<HttpReqArg>& args = std::vector<HttpReqArg>()) const;
const std::string _token;
+ const HttpClient &_httpClientDriver;
};
}
diff --git a/include/tgbot/Bot.h b/include/tgbot/Bot.h
index 8dd4c16..9bca05f 100644
--- a/include/tgbot/Bot.h
+++ b/include/tgbot/Bot.h
@@ -28,6 +28,7 @@
#include "tgbot/Api.h"
#include "tgbot/EventBroadcaster.h"
#include "tgbot/EventHandler.h"
+#include "tgbot/net/HttpClient.h"
namespace TgBot {
@@ -39,7 +40,8 @@ namespace TgBot {
class Bot {
public:
- explicit Bot(const std::string& token) : _token(token), _api(token), _eventHandler(&_eventBroadcaster) {
+ explicit Bot(const std::string& token, const HttpClient &httpClientDriver = BoostHttpClient::getInstance())
+ : _token(token), _api(token, httpClientDriver), _eventHandler(&_eventBroadcaster), _httpClientDriver(httpClientDriver) {
}
/**
@@ -75,6 +77,7 @@ private:
const Api _api;
EventBroadcaster _eventBroadcaster;
const EventHandler _eventHandler;
+ const HttpClient &_httpClientDriver;
};
}
diff --git a/include/tgbot/net/HttpClient.h b/include/tgbot/net/HttpClient.h
index 8d67891..1375785 100644
--- a/include/tgbot/net/HttpClient.h
+++ b/include/tgbot/net/HttpClient.h
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2015 Oleg Morozenkov
+ * Copyright (c) 2018 Egor Pugin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -26,6 +27,9 @@
#include <string>
#include <boost/asio.hpp>
+#ifdef HAVE_CURL
+#include <curl/curl.h>
+#endif
#include "tgbot/net/Url.h"
#include "tgbot/net/HttpReqArg.h"
@@ -41,10 +45,29 @@ namespace TgBot {
class HttpClient {
public:
+ virtual ~HttpClient() = default;
+
+ /**
+ * @brief Sends a request to the url.
+ *
+ * If there's no args specified, a GET request will be sent, otherwise a POST request will be sent.
+ * If at least 1 arg is marked as file, the content type of a request will be multipart/form-data, otherwise it will be application/x-www-form-urlencoded.
+ */
+ virtual std::string makeRequest(const Url& url, const std::vector<HttpReqArg>& args) const = 0;
+};
+
+/**
+ * @brief This class makes http requests via boost::asio.
+ *
+ * @ingroup net
+ */
+class BoostHttpClient : public HttpClient {
+
+public:
/**
* @brief Returns instance which lives during all application lifetime.
*/
- static HttpClient& getInstance();
+ static BoostHttpClient& getInstance();
/**
* @brief Sends a request to the url.
@@ -52,12 +75,47 @@ public:
* If there's no args specified, a GET request will be sent, otherwise a POST request will be sent.
* If at least 1 arg is marked as file, the content type of a request will be multipart/form-data, otherwise it will be application/x-www-form-urlencoded.
*/
- std::string makeRequest(const Url& url, const std::vector<HttpReqArg>& args);
+ std::string makeRequest(const Url& url, const std::vector<HttpReqArg>& args) const override;
private:
- boost::asio::io_service _ioService;
+ mutable boost::asio::io_service _ioService;
};
+#ifdef HAVE_CURL
+
+/**
+ * @brief This class makes http requests via libcurl.
+ *
+ * @ingroup net
+ */
+class CurlHttpClient : public HttpClient {
+
+public:
+
+ /**
+ * @brief Raw curl settings storage for fine tuning.
+ */
+ CURL* curlSettings;
+
+ CurlHttpClient();
+ ~CurlHttpClient();
+
+ /**
+ * @brief Returns instance which lives during all application lifetime.
+ */
+ static CurlHttpClient& getInstance();
+
+ /**
+ * @brief Sends a request to the url.
+ *
+ * If there's no args specified, a GET request will be sent, otherwise a POST request will be sent.
+ * If at least 1 arg is marked as file, the content type of a request will be multipart/form-data, otherwise it will be application/x-www-form-urlencoded.
+ */
+ std::string makeRequest(const Url& url, const std::vector<HttpReqArg>& args) const override;
+};
+
+#endif
+
}
#endif //TGBOT_HTTPCLIENT_H