summaryrefslogtreecommitdiff
path: root/include/tgbot/net
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/tgbot/net
parent7f388398bbcff916f5507770af727ef9ad59f33d (diff)
Implement CurlHttpClient.
Diffstat (limited to 'include/tgbot/net')
-rw-r--r--include/tgbot/net/HttpClient.h64
1 files changed, 61 insertions, 3 deletions
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