summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/curl_wrapper.hpp10
-rw-r--r--src/curl_wrapper.cpp45
2 files changed, 54 insertions, 1 deletions
diff --git a/include/curl_wrapper.hpp b/include/curl_wrapper.hpp
index 5e5b0fa..516af5b 100644
--- a/include/curl_wrapper.hpp
+++ b/include/curl_wrapper.hpp
@@ -20,11 +20,19 @@
#include "curl/curl.h"
#include <string>
+#include <string_view>
namespace mastodonpp
{
using std::string;
+using std::string_view;
+
+enum class http_method
+{
+ GET,
+ POST
+};
/*!
* @brief Handles the details of network connections.
@@ -70,6 +78,8 @@ public:
//! Move assignment operator
CURLWrapper& operator=(CURLWrapper &&other) noexcept = default;
+ string make_request(const http_method &meth, const string_view &uri);
+
private:
CURL *_connection;
char _curl_buffer_error[CURL_ERROR_SIZE];
diff --git a/src/curl_wrapper.cpp b/src/curl_wrapper.cpp
index 068f0ea..32d6056 100644
--- a/src/curl_wrapper.cpp
+++ b/src/curl_wrapper.cpp
@@ -33,7 +33,50 @@ CURLWrapper::~CURLWrapper() noexcept
curl_global_cleanup();
}
-int CURLWrapper::writer(char *data, size_t size, size_t nmemb, string *writerData)
+string CURLWrapper::make_request(const http_method &meth,
+ const string_view &uri)
+{
+ CURLcode code;
+ switch (meth)
+ {
+ case http_method::GET:
+ {
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
+ code = curl_easy_setopt(_connection, CURLOPT_HTTPGET, 1L);
+ break;
+ }
+ case http_method::POST:
+ {
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
+ code = curl_easy_setopt(_connection, CURLOPT_POST, 1L);
+ break;
+ }
+ }
+ if (code != CURLE_OK)
+ {
+ throw CURLException{code, "Failed to set HTTP method",
+ _curl_buffer_error};
+ }
+
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
+ code = curl_easy_setopt(_connection, CURLOPT_URL, uri.data());
+ if (code != CURLE_OK)
+ {
+ throw CURLException{code, "Failed to set URI", _curl_buffer_error};
+ }
+
+ code = curl_easy_perform(_connection);
+ if (code != CURLE_OK)
+ {
+ throw CURLException{code, "Failed to perform request",
+ _curl_buffer_error};
+ }
+
+ return _curl_buffer;
+}
+
+int CURLWrapper::writer(char *data, size_t size, size_t nmemb,
+ string *writerData)
{
if(writerData == nullptr)
{