From 95f86f4ce18e65d45894d5a130c4bdaf8ecbaac5 Mon Sep 17 00:00:00 2001 From: Egor Pugin Date: Wed, 4 Jul 2018 19:11:03 +0300 Subject: Implement CurlHttpClient. --- include/tgbot/net/HttpClient.h | 64 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 3 deletions(-) (limited to 'include/tgbot/net') 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 #include +#ifdef HAVE_CURL +#include +#endif #include "tgbot/net/Url.h" #include "tgbot/net/HttpReqArg.h" @@ -40,11 +44,30 @@ 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& 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& args); + std::string makeRequest(const Url& url, const std::vector& 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& args) const override; +}; + +#endif + } #endif //TGBOT_HTTPCLIENT_H -- cgit v1.2.3